Java OCA OCP Practice Question 2116

Question

Given the following four properties files, what does this code print?

Cars_en.properties        Cars_fr.properties 
 engine=engine             engine=moteur 
 horses=123 

Cars_en_US.properties     Cars_fr_FR.properties 
 country=US                country=France 
Locale.setDefault(new Locale("en", "US")); 
ResourceBundle rb = ResourceBundle.getBundle( 
   "Cars", new Locale("fr", "FR")); 
System.out.println(rb.getString("country") + " " 
   + rb.getString("engine")); 
A.   France engine
B.   France moteur
C.   France null
D.   The code throws an exception at runtime.


B.

Note

The getBundle() method matches Cars_fr_FR.

properties since that is the requested locale.

The country key is in that properties file directly, so France is used as the value.

The engine key is not, so Java looks higher up in the hierarchy and finds it in the language-specific Cars_fr.

properties file and uses moteur as the value.

Therefore, Option B is correct.

Note that the default locale isn't used.




PreviousNext

Related