ProgrammingHardProduct-company placement · Java

What is printed?

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

Correct answer: D. [p, y, q] p

Arrays.asList returns a fixed-size list BACKED by the array: writes through the list change the array and writes to the array show up in the list.

More OOP with C++ and Java questions

Exams that ask this topic