ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
List<Integer> l = new ArrayList<>(List.of(1, 2, 3));
l.remove(1);
System.out.print(l + " ");
l.remove(Integer.valueOf(1));
System.out.print(l);
  1. A. [2, 3] [3]
  2. B. [2, 3] [2, 3]
  3. C. [1, 3] [3]
  4. D. [1, 3] [1, 3]

Correct answer: C. [1, 3] [3]

remove(1) with an int literal matches remove(int index), removing the element at index 1 (the value 2). remove(Integer.valueOf(1)) matches remove(Object) and removes the value 1.

More OOP with C++ and Java questions

Exams that ask this topic