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());
  1. A. 30 30 null 3
  2. B. 20 30 null 2
  3. C. 20 40 null 2
  4. 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

Exams that ask this topic