ProgrammingHardProduct-company placement · Java
What is printed?
What is printed?
TreeMap<Integer, String> m = new TreeMap<>();
for (int k : new int[]{10, 20, 30, 40}) m.put(k, "v" + k);
System.out.print(m.floorKey(25) + " " + m.ceilingKey(30) + " "
+ m.higherKey(40) + " " + m.headMap(30).size());- A. 30 30 null 3
- B. 20 30 null 2
- C. 20 40 null 2
- D. 20 30 40 3
Correct answer: B. 20 30 null 2
floorKey is the greatest key ≤ 25 (20), ceilingKey the least key ≥ 30 (30 itself), higherKey(40) has no strictly larger key (null), and headMap(30) is EXCLUSIVE of 30: {10, 20}.
More OOP with C++ and Java questions
- What is printed? java Map<String, Integer> h = new HashMap<>(); h.put(null, 1); h.put(null, 2); System.out.print(h.size() + " " +…
- What is printed? java Map<String, Integer> m = new HashMap<>(); m.put("a", 1); int x = m.getOrDefault("b", 0) + m.get("a"); int y =…
- What is printed? java Map<Character, Integer> f = new TreeMap<>(); for (char c : "banana".toCharArray()) f.merge(c, 1, Integer::sum)…
- What is printed? java Map<String, Integer> m = new HashMap<>(); System.out.print(m.putIfAbsent("k", 1) + " ")…
- What is printed? java List<Integer> l = new ArrayList<>(List.of(1, 2, 3, 4, 5, 6)); l.subList(1, 4).clear(); System.out.print(l);
- What is printed? java List<String> a = Arrays.asList("x", null); System.out.print(a.size() + " "); List<String> b = List.of("x", null)…