ProgrammingHardProduct-company placement · Java

What is printed?

What is printed?
Set<List<Integer>> a = new HashSet<>();
a.add(List.of(1, 2));
a.add(new ArrayList<>(List.of(1, 2)));
Set<int[]> b = new HashSet<>();
b.add(new int[]{1, 2});
b.add(new int[]{1, 2});
System.out.print(a.size() + " " + b.size());
  1. A. 2 2
  2. B. 1 1
  3. C. 2 1
  4. D. 1 2

Correct answer: D. 1 2

All List implementations define equals/hashCode by content, so the immutable list and the ArrayList are equal. Arrays use identity equals and hashCode, so two separate arrays are distinct.

More OOP with C++ and Java questions

Exams that ask this topic