nerdexam
Oracle

1Z0-829 · Question #41

Given: Captions.properties file: user = UserName Captions_en.properties file: user = User name (EN) Captions_US.properties file: message = User name (US) Captions_en_US.properties file: message =…

The correct answer is B. The program throws a MissingResourceException. ResourceBundle.getBundle() expects a base name without the .properties extension, but the code passes "Captions.properties" as the base name. Java then searches for files like Captions.properties_en.properties and ultimately Captions.properties.properties - none of which exist…

Implementing Localization

Question

Given: Captions.properties file: user = UserName Captions_en.properties file: user = User name (EN) Captions_US.properties file: message = User name (US) Captions_en_US.properties file: message = User name (EN - US) and the code fragment: Locale.setDefault(Locale.US); Locale currentLocale = new Locale.Builder().setLanguage("en").build(); ResourceBundle captions = ResourceBundle.getBundle("Captions.properties", currentLocale); System.out.println(captions.getString("user")); What is the result?

Options

  • AUser name (US)
  • BThe program throws a MissingResourceException.
  • CUser name (EN - US)
  • DUserName
  • EUser name (EN)

How the community answered

(58 responses)
  • A
    2% (1)
  • B
    74% (43)
  • C
    7% (4)
  • D
    12% (7)
  • E
    5% (3)

Explanation

ResourceBundle.getBundle() expects a base name without the .properties extension, but the code passes "Captions.properties" as the base name. Java then searches for files like Captions.properties_en.properties and ultimately Captions.properties.properties - none of which exist - so it throws a MissingResourceException rather than falling back to the actual Captions.properties file.

Why the distractors fail:

  • D (UserName) - looks correct since Captions.properties has that key, but the wrong base name means Java never resolves to it as a fallback
  • E (User name EN) - would require resolving Captions_en.properties, which only happens with base name "Captions", not "Captions.properties"
  • A / C - both US-related bundles (Captions_US / Captions_en_US) are similarly unreachable for the same reason, and neither even contains the "user" key

Memory tip: Think of the base name as a class name, not a file name - just as you write MyClass, not MyClass.java, you write "Captions", not "Captions.properties". Including the extension is a common trap that breaks bundle resolution entirely.

Topics

#ResourceBundle#Locale fallback#naming conventions#localization

Community Discussion

No community discussion yet for this question.

Full 1Z0-829 Practice