ProgrammingModerateProduct-company placement · Java

What is printed?

What is printed?
List<Integer> e = Collections.emptyList();
System.out.print(e.size() + " ");
e.add(1);
  1. A. 0 then [1] is stored
  2. B. "0 " is printed, then UnsupportedOperationException is thrown
  3. C. Compile-time error: emptyList cannot be assigned to List<Integer>
  4. D. "0 " is printed, then NullPointerException is thrown

Correct answer: B. "0 " is printed, then UnsupportedOperationException is thrown

Collections.emptyList() returns a shared immutable empty list; any modification throws UnsupportedOperationException.

More OOP with C++ and Java questions

Exams that ask this topic