Question 1
What does this print? ```java import java.util.*; public class Main { public static void main(String[] args) { LinkedHashMap<String, Integer> m = new LinkedHashMap<>(); m.put("a", 1); m.put("b", 2); m.put("c", 3); System.out.println(m.reversed()); } } ```
A. {c=3, b=2, a=1}Correct answer
LinkedHashMap implements SequencedMap in Java 21, and reversed() returns a view in the opposite encounter order; insertion order a, b, c reverses to c, b, a.
B. {a=1, b=2, c=3}
The original order: assumes reversed() mutates the receiver, or that map order is not something you can flip. reversed() returns a reversed view directly.
C. {3=c, 2=b, 1=a}
Reads reversed as inverted, swapping keys with values. reversed() never touches the entries themselves, only the order they are visited in.
D. Compilation fails: reversed() is not declared on Map
The right rule applied to the wrong static type. Map has no reversed(), but the variable is declared LinkedHashMap, which implements SequencedMap, so the method resolves.
Explanation
Trace: `LinkedHashMap` implements `SequencedMap` in Java 21, and `SequencedMap.reversed()` returns a view of the same entries in the opposite encounter order. The insertion order is `a`, `b`, `c`, so the reversed view iterates `c`, `b`, `a` and `AbstractMap.toString` renders it as `{c=3, b=2, a=1}`. Why the others are wrong: `{a=1, b=2, c=3}` is the original order — it assumes `reversed()` mutates the receiver and returns something you then have to re-read, or that map order is not a real thing you can flip. `{3=c, 2=b, 1=a}` reads "reversed" as *inverted*: keys swapped with values. `reversed()` never touches the entries themselves, only the order they are visited in. `Compilation fails: reversed() is not declared on Map` is the right rule applied to the wrong static type. `Map` genuinely has no `reversed()` — but the variable here is declared `LinkedHashMap`, which implements `SequencedMap`, so the method resolves. Exam tip: the sequenced methods hang off `SequencedMap`, not `Map`. Widen the same object to `Map<String, Integer> m` and this very line stops compiling — the exam loves changing only the declared type and asking again. `HashMap` is not sequenced at all, at any static type.