ProgrammingVery hardProduct-company placement · Java

What is printed?

What is printed?
Map<Long, String> m = new HashMap<>();
m.put(1L, "one");
System.out.print(m.get(1) + " " + m.get(1L) + " " + m.containsKey((long) 1));
  1. A. one one true
  2. B. null one true
  3. C. null one false
  4. D. Compile-time error at m.get(1)

Correct answer: B. null one true

get takes an Object, so m.get(1) compiles but looks up an Integer key, which never equals the Long key 1L. Casting to long boxes to Long and matches.

More OOP with C++ and Java questions

Exams that ask this topic