ProgrammingHardProduct-company placement · Java
What is printed?
What is printed?
Map<String, Integer> m = Map.of("a", 1, "a", 2);
System.out.print(m);- A. {a=2}
- B. An IllegalArgumentException is thrown
- C. {a=1}
- 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
- What is printed? java interface A { default String hi() { return "A"; } } interface B extends A { default String hi() { return "B"; } }…
- What is printed? java // static methods of the main class: static void f(Object o) { System.out.print("O"); } static void f(Number n) {…
- A base type must hold common fields, have a constructor that validates them, and share some method bodies, while subclasses form a strict…
- java.io.BufferedReader wraps another Reader, is itself a Reader, and adds buffering on top of it; such wrappers can be stacked at run…
- Collections.sort(list, comparator) lets the caller pass the comparison algorithm as an object, and the same sort code works with any…
- Square extends Rectangle and overrides setWidth and setHeight so that both always change together. Client code written for Rectangle does…