ProgrammingHardProduct-company placement · Java

What is printed?

What is printed?
List<Integer> l = Arrays.asList(3, 1, 2);
Collections.sort(l);
System.out.print(l + " ");
l.clear();
  1. A. "[1, 2, 3] " is printed, then UnsupportedOperationException is thrown
  2. B. [1, 2, 3] []
  3. C. UnsupportedOperationException is thrown by sort
  4. D. "[3, 1, 2] " is printed, then UnsupportedOperationException is thrown

Correct answer: A. "[1, 2, 3] " is printed, then UnsupportedOperationException is thrown

The list from Arrays.asList is fixed-size but writable: sorting only sets elements, so it works. Any size change (add, remove, clear) throws UnsupportedOperationException.

More OOP with C++ and Java questions

Exams that ask this topic