Java OCA OCP Practice Question 2130

Question

Given the following snippets of resource bundles from when we compiled the application, what is the result of the following?

Main.java                   Main.properties 
getContents:                 color=white 
return new Object[][] 
{{ "wheels", "4" }}; 

Main_en.java                Main_en.properties 
getContents:                 wheels=6 
return                     color=black 
new Object[0][0]; 
 Locale.setDefault(new Locale("en", "US")); 
 ResourceBundle rb = ResourceBundle.getBundle("Main"); 
System.out.println(rb.getString("wheels")); 
  • A. null
  • B. 4
  • C. 6
  • D. The code throws an exception at runtime.


B.

Note

Since no locale is specified, the code tries to find a bundle matching the default locale.

While none of the resource bundles match English United States, two do match the language English.

The Java class one is used since it is present.

Since the Java resource bundle for English doesn't have a key wheels, we go up to the parent resource bundle.

The default Java resource bundle does have the key wheels with the value 4, so Option B is correct.




PreviousNext

Related