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);- A. {a=1, b=2, c=3}
- B. {a=1, c=3}
- C. {a=1}
- 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
- 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…
- Which line fails to compile? cpp struct P { int x = 3; ostream& operator<<(ostream& os) { return os << x; } }; int main() { P p; cout <<…