Java OCA OCP Practice Question 2132

Question

If the key red is in all three of these files, which file will the following code use for the resource bundle?

Locale loc = new Locale("zh", "CN"); 
Locale.setDefault(new Locale("en", "US")); 
ResourceBundle rb = ResourceBundle.getBundle("Colors", loc); 
rb.getString("red"); 
  • A. Colors_en.properties
  • B. Colors.properties
  • C. Red_en.properties
  • D. None of the above. It will throw a MissingResourceException.


A.

Note

Since a locale is passed when requesting the ResourceBundle, that locale is used first when looking for bundles.

Since there isn't a bundle for that locale, Java checks for the language zh.

There still isn't a match, so Java goes on to check for the default locale.

Still no match.

Next Java checks the language of the default locale and finally finds a match in Option A.

If that match wasn't found, Java would match on Option B, which is the default bundle.

Option C would never be checked since the base name is Red rather than the requested Color.




PreviousNext

Related