nerdexam
Oracle

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

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 ResourceBundle messages = ResourceBundle.getBundle("MessagesBundle", currentLocale); System.out.println(messages.getString("inquiry")); Which two code fragments, when inserted at line 1 independently, enable the code to print "Wie geht's"?

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)
  • A
    3% (1)
  • B
    74% (23)
  • C
    13% (4)
  • D
    3% (1)
  • E
    6% (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 seeks MessagesBundle_de.properties (which doesn't exist) and falls back to the default, printing "How are you?" instead.
  • D is doubly wrong: Locale has no no-arg constructor in standard use here, and setLanguage()/setRegion() don't exist - Locale is immutable after construction.
  • E (Locale.getinstance(...)) is wrong because no public getInstance(Locale, Locale) overload exists on Locale, and the casing getinstance won'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.

Full 1Z0-809 Practice