ProgrammingHardProduct-company placement · Java

What is printed?

What is printed?
TreeMap<String, Integer> m = new TreeMap<>();
for (String k : new String[]{"9", "10", "100", "2"}) m.put(k, k.length());
System.out.print(m.keySet());
  1. A. [2, 9, 10, 100]
  2. B. [9, 10, 100, 2]
  3. C. [10, 100, 2, 9]
  4. D. [100, 10, 9, 2]

Correct answer: C. [10, 100, 2, 9]

String keys are ordered lexicographically character by character, not numerically: "10" < "100" < "2" < "9" because '1' < '2' < '9'.

More OOP with C++ and Java questions

Exams that ask this topic