Java OCA OCP Practice Question 2118

Question

Given the 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", "CA")); 
System.out.println(rb.getString("engine") + " " 
    + rb.getString("horses")); 
A.   engine 123
B.   moteur 123
C.   moteur null
D.   The code throws an exception at runtime.


D.

Note

The getBundle() method matches Cars_fr.properties.

Since the requested locale of French Canada is not available, it uses the language-specific locale of French.

The engine key is in that properties file directly, so moteur is retrieved as the value.

However, we have a problem getting the horses key.

It is not in the hierarchy of Cars_fr.properties.

It is in the English properties file, but Java cannot look at the default locale if it found a match with the requested locale.

As a result, the code throws a MissingResourceException, making Option D the answer.




PreviousNext

Related