Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!

# RHE final exam question

Kathy Wang
Greenhorn
Posts: 28
Hello,
In the final exam Q27:
What is the value of the following expression?
Math.round(Math.random()+2.50001);
A. 2;
B. 3;
C. It is impossible to say.
Ans is B.
To my opinion, the answer is C. Because Math.random() returns double that greater than or equal to 0.0 and less than 1.0, and Math.round() return (long)Math.floor(a + 0.5d). The a is stuff in the Math.round(a). Thus if the returned double from Math.random() is 0.99999, then a in the Math.round is 3.5, at last the Math.round() returns 4, not 3. Where is my problem on understanding this question? Can anyone please help me out? Thanks.

ashok khetan
Ranch Hand
Posts: 153
hi!
i too was just going to post the same qu.....i also thought the possibility of returning 0.99999 by Math.random()...so the right answer should be C.
ashok.

Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
Yep me too I'm not convinced that B is the right answer...
I ran some tests and the values produced range from 2.50001 to some double primitive slightly bigger than 3.5
So I too think that C is the right answer
Val

Amit Agrawal
Ranch Hand
Posts: 282
I don't know as i don't have any logical solution to it but I ran this line in a loop for 10000 times and printed its value and every time the output was 3 only (but than its random number generation so i might have never got 0.99999).
personally i think the correct answer should have been c.

Kathy Wang
Greenhorn
Posts: 28
I hope what we thought is right, but just in case,I tried to find this book's errata, but failed. Does anyone know if there is a correspond errata?

Jane Griscti
Ranch Hand
Posts: 3141
Hi guys,
See this earlier post on same topic.
The post also contains a link to the errata.
------------------
Jane Griscti
Sun Certified Programmer for the Javaï¿½ 2 Platform

Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
That's a pretty lazy answer on the Sybex site, too.
For the record, I proposed that we re-word the question altogether. It's of course entirely possible to say what the resulting value could be. The problem is with the set of answers themselves; a correct answer is missing from the choice list.
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide