Vidyalelo
Java Programming · Q35

Array

Programming · Java Programming · question 35

Q35

What is the output of the following code? public class Test public static void main(String args[]) double[] myList = 1, 5, 5, 5, 5, 1; double max = myList[0]; int indexOfMax = 0; for(int i = 1; i max) max = myList[i]; indexOfMax = i; System.out.println(indexOfMax);

A.
0
B.
1
Answer
C.
2
D.
3
E.
4

Answer: Option B

Solution

Answer: Option B
Solution:
In the given program.
Line 7 : if(myList[i] > max) execute only on time when i =1;
when i = 1 then myList[i] = 5 and max = 1(so the statement is true and if block will be executed).
Then, max = myList[i] = 5 and indexOfMax = i = 1.
After that if statement always false. so indexOfMax value remain 1.

Therefore the value of indexOfMax is 1 at end of the for loop.