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));
- A. one one true
- B. null one true
- C. null one false
- 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
- What is printed? java List<Character> l = new ArrayList<>(List.of('a', 'b')); System.out.print(l.contains('a') + " " + l.contains(97) + "…
- What is printed? java Function<Integer, Integer> f = x -> x + 1; Function<Integer, Integer> g = x -> x 2…
- What is printed? java List<String> l = new ArrayList<>(List.of("ccc", "ee", "a", "bb", "d"))…
- What is printed? java int[] c = {0}; Runnable r = () -> c[0]++; r.run(); r.run(); System.out.print(c[0]);
- What is printed? java List<Integer> a = new ArrayList<>(List.of(1, 2)); List<Integer> b = new LinkedList<>(List.of(1, 2))…
- What is printed? java Queue<Integer> q = new ArrayDeque<>(); q.add(1); q.add(2); q.offer(3); q.poll(); System.out.print(q.peek() + " " +…