ProgrammingHardProduct-company placement · Java

What is printed?

What is printed?
// Java 10 or later
List<Integer> base = new ArrayList<>(List.of(1, 2));
List<Integer> view = Collections.unmodifiableList(base);
List<Integer> copy = List.copyOf(base);
base.add(3);
System.out.print(view.size() + " " + copy.size());
  1. A. 2 2
  2. B. 3 3
  3. C. 3 2
  4. D. 2 3

Correct answer: C. 3 2

unmodifiableList is a read-only VIEW: it cannot be changed through itself but reflects changes to the underlying list. List.copyOf makes an independent immutable copy, so it stays at 2.

More OOP with C++ and Java questions

Exams that ask this topic