31.8k views
5 votes
What does the following loop print?int[] a = {6, 1, 9, 5, 12, 3};int len = a.length;int x = a[0];for (int i = 1; i < len; i++)if (a[i] < x) x = a[i];System.out.println(x);1. 36

2. 12
3. 1
4. 6

User Pankaj Jha
by
8.1k points

1 Answer

3 votes

Answer:

The answer is "Option 3".

Step-by-step explanation:

In the given code an array a[] is already initialized then a variable "len" is defined that contains the length of the array and an integer variable x is defined that contains the value 6 that is defined in the array. In the for loop the if block is defined that check the array element value is less then variable x if this condition is true the array variable holds value in x variable and compares other values. In the code value of x variable is 1 and other options are not correct that can be defined as follows:

  • In option 1, In array, element 36 is not available that's why it is not correct.
  • In option 2 and option 4, the value of x variable is 1 and it is less than 12 and 6. that's why it not correct.
User Dmytroy
by
8.8k points