Question 1
What does this print? ```java import java.io.*; import java.util.*; public class Main { static class Child extends PropertyResourceBundle { Child(Reader reader, ResourceBundle parent) throws IOException { super(reader); setParent(parent); } } public static void main(String[] args) throws IOException { ResourceBundle base = new PropertyResourceBundle( new StringReader("greeting=Hello\nfarewell=Bye\n")); ResourceBundle fr = new Child(new StringReader("greeting=Bonjour\n"), base); System.out.println(fr.getString("greeting") + " " + fr.getString("farewell") + " " + fr.containsKey("farewell") + " " + fr.keySet().size()); } } ```
A. Bonjour Bye false 2
Assumes containsKey is a local check while getString searches the parent chain; both consult this bundle and its parents, so containsKey("farewell") is true, not false.
B. Bonjour Bye true 1
Gets the lookups right but assumes keySet() reports only the child's own keys; keySet() is specified over this bundle and its parents, so the union has size 2, not 1.
C. Bonjour Bye true 2Correct answer
The child overrides greeting (Bonjour) while farewell falls through to the parent (Bye); containsKey and keySet both walk the parent chain, so containsKey is true and the key union size is 2.
D. A MissingResourceException is thrown for the key farewell
Assumes a bundle can only serve keys it declares itself; MissingResourceException is thrown only when a key is absent from the child and every parent, and farewell exists in the parent.
Explanation
Trace: this is the parent chain that `ResourceBundle.getBundle` normally builds for you, assembled by hand so you can see the mechanism. The child bundle defines only `greeting`; its parent defines `greeting` and `farewell`. - `getString("greeting")` finds `Bonjour` in the child and stops — the child wins over the parent, which is why a country bundle can override just a few keys. - `getString("farewell")` misses in the child, so `getObject` walks to the parent and returns `Bye`. This is the fallback that makes partial translations work. - `containsKey` is documented to consult *this bundle and its parents*, so it is `true`. - `keySet()` is likewise documented to return the keys of *this bundle and its parents* — the union `{greeting, farewell}` — so the size is 2. Why the others are wrong: `Bonjour Bye true 1` gets the lookups right but assumes `keySet()` reports only the keys physically in the child. That describes the protected `handleKeySet()`, not the public `keySet()`; the child on its own really does hold one key, which is exactly why this distractor is tempting. `Bonjour Bye false 2` assumes `containsKey` is a local check while `getString` searches the chain. That would be an incoherent API — both walk the same parent chain. `A MissingResourceException is thrown for the key farewell` assumes a bundle can only serve keys it declares itself. `MissingResourceException` is thrown only when the key is absent from the child *and* from every parent. Exam tip: for `ResourceBundle`, "most specific wins, missing keys fall through to the parent" governs `getObject`, `getString`, `containsKey`, `getKeys` and `keySet` alike. The trap in the other direction: the parent chain is built from the *base name*, so it never fills a gap from an unrelated bundle — only from `Msg_fr` up to `Msg`.