1Z0-809 · Question #235
Given the content: MessagesBundle.properties file: inquiry = How are you? MessagesBundle_de_DE.properties file: inquiry = Wie geht's? and given the code fragment: Locale currentLocale; // Line 1…
The correct answer is B. currentLocale = new Locale.Builder().setLanguage("de").setRegion("DE").build(). Option B works because Locale.Builder is the modern, preferred way to construct a Locale with both a language ("de") and a region ("DE"), producing a locale that exactly matches the MessagesBundle_de_DE.properties filename pattern that ResourceBundle.getBundle() uses for…
Question
Options
- AcurrentLocale = new Locale ("de", "DE");
- BcurrentLocale = new Locale.Builder().setLanguage("de").setRegion("DE").build();
- CcurrentLocale = Locale.GERMAN;
- DcurrentLocale = new Locale(); currentLocale.setLanguage("de"); currentLocale.setRegion ("DE");
- EcurrentLocale = Locale.getinstance(Locale.GERMAN,Locale.GERMANY);
How the community answered
(31 responses)- A3% (1)
- B74% (23)
- C13% (4)
- D3% (1)
- E6% (2)
Explanation
Option B works because Locale.Builder is the modern, preferred way to construct a Locale with both a language ("de") and a region ("DE"), producing a locale that exactly matches the MessagesBundle_de_DE.properties filename pattern that ResourceBundle.getBundle() uses for lookup.
Option A is also correct (the question asks for two): new Locale("de", "DE") produces the identical locale - language de, country DE - so ResourceBundle resolves to the same German properties file. The listed "Correct Answer: B only" appears to be a transcription error.
Why the others fail:
- C (
Locale.GERMAN) is language-only (de, no country), so the lookup seeksMessagesBundle_de.properties(which doesn't exist) and falls back to the default, printing "How are you?" instead. - D is doubly wrong:
Localehas no no-arg constructor in standard use here, andsetLanguage()/setRegion()don't exist -Localeis immutable after construction. - E (
Locale.getinstance(...)) is wrong because no publicgetInstance(Locale, Locale)overload exists onLocale, and the casinggetinstancewon't even compile.
Memory tip: Think "locale = language + country." If the properties file is named _de_DE, your Locale must carry both parts - a language-only constant like Locale.GERMAN gives you only half the key. When in doubt, use the Builder (explicit and readable) or the two-arg constructor new Locale("de", "DE").
Community Discussion
No community discussion yet for this question.