ProgrammingHardProduct-company placement · Java
What is printed?
What is printed?
Map<String, String> h = new HashMap<>();
h.put("k", null);
System.out.print(h.size() + " ");
Map<String, String> c = new ConcurrentHashMap<>();
c.put("k", null);
System.out.print(c.size());- A. 1 1
- B. 0 0
- C. "1 " is printed, then a NullPointerException is thrown
- D. 1 0
Correct answer: C. "1 " is printed, then a NullPointerException is thrown
HashMap allows null keys and values. ConcurrentHashMap forbids both, because get() returning null must unambiguously mean 'absent' under concurrency.
More OOP with C++ and Java questions
- What is printed? java List<Integer> e = Collections.emptyList(); System.out.print(e.size() + " "); e.add(1);
- What is printed? java System.out.print(String.valueOf((Object) null) + " "); System.out.print(String.valueOf(null));
- What is printed? java // static methods of the main class: static void f(int... a) { System.out.print("V"); } static void f(Integer a…
- What is printed? java // static methods of the main class: static void f(double d) { System.out.print("D"); } static void f(Object o) {…
- Which line fails to compile? java // static methods of the main class: static void g(long l) { } static void h(Long l) { } // in main…
- Which lines fail to compile? java String s = "x"; Number n = 1; Runnable r = () -> { }; Object o = s; boolean a = s instanceof Integer; //…