ProgrammingModerateProduct-company placement · Java

What is printed?

What is printed?
int[] arr = {1, 2, 3};
for (int x : arr) x *= 2;
System.out.print(Arrays.toString(arr));
  1. A. [2, 4, 6]
  2. B. [1, 2, 3, 2, 4, 6]
  3. C. [1, 2, 3]
  4. D. Compile-time error: x is final

Correct answer: C. [1, 2, 3]

The enhanced for loop copies each element into the local variable x; changing x does not write back to the array.

More OOP with C++ and Java questions

Exams that ask this topic