ProgrammingVery hardProduct-company placement · Java

What is printed?

What is printed?
Map<String, Integer> m = new LinkedHashMap<>(16, 0.75f, true);
m.put("a", 1); m.put("b", 2); m.put("c", 3);
m.get("a");
m.put("b", 20);
System.out.print(m.keySet());
  1. A. [a, b, c]
  2. B. [c, a, b]
  3. C. [b, a, c]
  4. D. [c, b, a]

Correct answer: B. [c, a, b]

The third constructor argument true selects ACCESS order: each get or put of an existing key moves it to the end. After get(a): b, c, a; after put(b): c, a, b. (This is the basis of an LRU cache.)

More OOP with C++ and Java questions

Exams that ask this topic