Win a copy of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 this week in the Programmer Certification forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

Errata question 32 chapter 22 p.381 - OCA OCP Practice Test Jeanne and Scott

 
Pablo Napoli
Ranch Hand
Posts: 77
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys. My answer was option B but in the book says D, Whereby I coded it and see that I was right. I will attach the diagram from question 31 where this exercise is based.

32. Given the snippets of resource bundles in question 31 from when we compiled the application, what is the result of the following?

Locale.setDefault(new Locale("en"));
ResourceBundle rb = ResourceBundle.getBundle("Buggy");
System.out.println(rb.getString("color"));

A. null
B. black
C. white
D. The code throws an exception at runtime.

Book's answer: D. Since no locale is specified, the code tries to find a bundle matching the default locale. Two resource bundles match the language English. The Java class one is used since it is present. However, it does not contain a key color , nor does its parent. Java does not allow looking in a properties file resource bundle once it has matched a Java
class resource bundle. Therefore, it throws a MissingResourceBundleException , and Option D is the answer.

Right answer: Option B.
20191105_202921.jpg
[Thumbnail for 20191105_202921.jpg]
 
Campbell Ritchie
Marshal
Posts: 66236
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you run the code?
 
Pablo Napoli
Ranch Hand
Posts: 77
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ritchie. Yes, for both exercises, 31 and 32.

Test.java


TestProp_en.java


TestProp_en.properties


TestProp.java


TestProp.properties




 
Campbell Ritchie
Marshal
Posts: 66236
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...and it printed, “black”, didn't it? I got the same result. Have you looked on the book's website? I didn't find anything for page 381. Have you run the code with Java8?
I did and still got, “black” with Java8.
 
Pablo Napoli
Ranch Hand
Posts: 77
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I got "black" too and I'm compiling with Java8. Maybe it does not appear on the website cause still nobody found this errata. But also is apparently wrong exercise 31.
 
Jeanne Boyarsky
author & internet detective
Posts: 39602
781
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not convinced this is an errata. I'm also a bit puzzled how both of you are getting "black." I think there is some subtle detail that is different in the example posted here than the one in the book.

My trying it
I created three classes and one property file in the default package. (I din't bother creating Buggy.properties since it isn't relevant to the problem we are discussing.

Buggy_en.properties








Running PlayTest prints
Exception in thread "main" java.util.MissingResourceException: Can't find resource for bundle Buggy, key color
at java.base/java.util.ResourceBundle.getObject(ResourceBundle.java:564)
at java.base/java.util.ResourceBundle.getObject(ResourceBundle.java:558)
at java.base/java.util.ResourceBundle.getString(ResourceBundle.java:521)
at jb.PlayTest.main(PlayTest.java:12)

Testing for typos
I then renamed Buggy_en.java to Buggy_english.java. When re-running, i did get "black". This shows the property file was in my classpath.

Conceptually
Further, the example is *supposed* to throw an exception. There's two lookups that go on. The first one is to find the resource bundle to use. I think we both agree that is Buggy_en.java (due to it being the default Locale and java/list resource bundles having a high precedence than property file ones.)

The second is for lookjng up the keys. Java uses the parent resource bundle in the hierarchy if they key isn't found.A property file is not in the parent hierarchy of a java/list bundle. The Javadoc describes this as "hiding" the resource bundle:

The file MyResources_fr_CH.properties is never used because it is hidden by the MyResources_fr_CH.class.


 
If I had asked people what they wanted, they would have said faster horses - Ford. Tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!