ProgrammingVery hardAmazon/Microsoft SDE MCQ · Java placement

What is printed?

What is printed?
Map<String, Integer> m = new HashMap<>();
m.merge("k", 5, Integer::sum);
m.merge("k", 5, Integer::sum);
m.merge("k", 1, (a, b) -> null);
System.out.print(m.containsKey("k") + " " + m.size());
  1. A. true 1
  2. B. false 1
  3. C. true 0
  4. D. false 0

Correct answer: D. false 0

The first merge inserts 5 and the second combines to 10. When the remapping function returns null, merge REMOVES the key, so the map ends up empty.

More OOP with C++ and Java questions

Exams that ask this topic