ProgrammingHardProduct-company placement · Java

What is printed?

What is printed?
List<Integer> l = new ArrayList<>(List.of(1, 2, 3, 4, 5, 6));
l.subList(1, 4).clear();
System.out.print(l);
  1. A. [1, 6]
  2. B. [5, 6]
  3. C. [1, 5, 6]
  4. D. [1, 2, 6]

Correct answer: C. [1, 5, 6]

subList(1, 4) is a view of indices 1, 2, 3 (end exclusive) = 2, 3, 4; clearing the view removes them from the backing list.

More OOP with C++ and Java questions

Exams that ask this topic