i am in preparation for ocjp and stumbled accross this question (from study Guide 6):
Which are true? (Choose all that apply.)
A. The output is 987.12345 987.12345
B. The output is 987.12346 987.12345
C. The output is 987.12345 987.123456
D. The output is 987.12346 987.123456
E. The try/catch block is unnecessary
F. The code compiles and runs without exception
G. The invocation of parse() must be placed within a try/catch block
The Problem is that A,B,C,D are locale aware in and will look different in different countries. But the "correct" answers are D,F and G. Does this kind of problem still persist and every must "think2 in US-Locale?
The answer would be correct with: NumberFormat nf = NumberFormat.getInstance(Locale.US);
In Germany the output look like this:
two lines - the second problem with some answers: sometimes line breaks are printed and some times not (there are answers that only differ in those line breaks)
The code runs and prints these "expected" values and I'm in the US -
William Butler Yeats: All life is a preparation for something that probably will never happen. Unless you make it happen.
Joined: Jan 21, 2010
And this is the Problem the output is not locale independent. The problem is that this question silently implies that the code runs in a us locale and this is a question from a chapter that checks your knowledge about locale aware code. Other question from other chapters are expressing environment dependent question more precisly: "What are possible outputs", "What are true for locale us" ...
I guess from the answers supplied you've just got to assume that the question isn't testing your ability to know locales but is purely testing your ability to know how and when rounding occurs which will be the same (you'd hope) in most locales that do use full stops.
As for your point about lack of locale, I've just read that chapter again and as typical with authors of an American stance the assumption is you are American too as there's pleny of mention of results without mentioning locale.
Joined: Jan 21, 2010
the problem is that this question is related to the chapter that teaches you about locales and their use (to get instances of Date- and NumberFormat) and it teaches you that the jvm has a default locale based on your location. if this knowledge is taken into account the question or answer is wrong. there are many other question that explicitly set and use other locales.
subject: problem/ failure in question - question is not locale aware (Study Guide)