ProgrammingHardAmazon/Microsoft SDE MCQ · Java placement

What is printed?

What is printed?
TreeMap<Integer, String> t = new TreeMap<>();
for (int k : new int[]{40, 10, 30, 20}) t.put(k, "v");
System.out.print(t.floorKey(25) + " " + t.ceilingKey(25) + " "
    + t.headMap(30).size() + " " + t.tailMap(30).size());
  1. A. 20 30 3 2
  2. B. 20 30 2 1
  3. C. 20 30 2 2
  4. D. 30 20 2 2

Correct answer: C. 20 30 2 2

floorKey gives the greatest key ≤ 25 (20) and ceilingKey the least key ≥ 25 (30). headMap(30) is exclusive ({10, 20}, size 2) while tailMap(30) is inclusive ({30, 40}, size 2).

More OOP with C++ and Java questions

Exams that ask this topic