ProgrammingVery hardAmazon/Microsoft SDE MCQ · Java placement

What is printed?

What is printed?
Integer[] arr = {1, 2, 3};
List<Integer> l = Arrays.asList(arr);
arr[0] = 7;
l.set(2, 9);
System.out.print(l + " " + arr[2]);
  1. A. [1, 2, 9] 3
  2. B. [7, 2, 9] 3
  3. C. [1, 2, 9] 9
  4. D. [7, 2, 9] 9

Correct answer: D. [7, 2, 9] 9

Arrays.asList returns a fixed-size list backed by the array, so writes go both ways: arr[0] = 7 shows in the list and l.set(2, 9) changes arr[2]. Only structural changes like add are unsupported.

More OOP with C++ and Java questions

Exams that ask this topic