ProgrammingHardProduct-company placement · Java

What is printed?

What is printed?
Map<String, Integer> m = new TreeMap<>(Map.of("a", 1, "b", 2, "c", 3));
m.keySet().remove("b");
m.values().remove(3);
System.out.print(m);
  1. A. {a=1, b=2, c=3}
  2. B. {a=1, c=3}
  3. C. {a=1}
  4. D. An UnsupportedOperationException is thrown

Correct answer: C. {a=1}

keySet() and values() are live views of the map: removing a key from the key set, or a value from the values collection, removes the whole mapping.

More OOP with C++ and Java questions

Exams that ask this topic