Java OCA OCP Practice Question 2124

Question

If the key red is in all four 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_CN.properties
  • B. Colors_en.properties
  • C. Colors_US.properties
  • D. Colors_zh.properties


D.

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 called Colors_zh_CN.properties, Java goes on to check for the language.

Option D provides a match on language.

If this was not found, Java would go on to the default locale, eventually matching Option B.

Since country is not used without language, Options A and C would not be considered as options.




PreviousNext

Related