ProgrammingHardProduct-company placement · Java

What is printed?

What is printed?
Map<String, Integer> m = Map.of("a", 1, "a", 2);
System.out.print(m);
  1. A. {a=2}
  2. B. An IllegalArgumentException is thrown
  3. C. {a=1}
  4. D. {a=1, a=2}

Correct answer: B. An IllegalArgumentException is thrown

The Map.of factories reject duplicate keys with IllegalArgumentException instead of keeping the last value as put() would.

More OOP with C++ and Java questions

Exams that ask this topic