ProgrammingModerateProduct-company placement · Java
What is printed?
What is printed?
Map<String, Integer> m = new TreeMap<>(Map.of("a", 1, "b", 2));
for (Map.Entry<String, Integer> e : m.entrySet()) e.setValue(e.getValue() * 10);
System.out.print(m);- A. {a=1, b=2}
- B. {a=10, b=20}
- C. A ConcurrentModificationException is thrown
- D. An UnsupportedOperationException is thrown
Correct answer: B. {a=10, b=20}
Map.Entry.setValue on an entry obtained from a mutable map's entrySet writes through to the map, and it is not a structural modification, so iteration continues normally.
More OOP with C++ and Java questions
- What is printed? java Map<String, Integer> m = new TreeMap<>(Map.of("a", 1, "b", 2, "c", 3)); m.keySet().remove("b")…
- Which statement about the protected modifier is true in Java but NOT in C++?
- What is printed? java // file a/P.java package a; public class P { void f() { System.out.print("P"); } // package-private public void…
- What is printed? java String r = Stream.of(3, 1, 3, 2).distinct().sorted() .map(String::valueOf).collect(Collectors.joining("-"))…
- What is printed? java System.out.print(Optional.ofNullable(null).map(Object::toString).orElse("none") + " ")…
- What happens when this is compiled? java interface I { void a(); void b(); } abstract class P implements I { public void a() { } } class C…