ProgrammingVery hardProduct-company placement · Java

What is printed?

What is printed?
List<Integer> src = List.of(1, 2);
List<Integer> dst = new ArrayList<>(5);
Collections.copy(dst, src);
System.out.print(dst);
  1. A. [1, 2]
  2. B. [1, 2, null, null, null]
  3. C. An IndexOutOfBoundsException is thrown
  4. D. []

Correct answer: C. An IndexOutOfBoundsException is thrown

Collections.copy overwrites existing elements and requires dst.size() ≥ src.size(). The constructor argument 5 is only a capacity, so dst has size 0.

More OOP with C++ and Java questions

Exams that ask this topic