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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# RHE final exam question

Greenhorn
Posts: 28
• Number of slices to send:
Optional 'thank-you' note:
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.

Ranch Hand
Posts: 153
• Number of slices to send:
Optional 'thank-you' note:
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.

Author & Gold Digger
Posts: 7617
6
• Number of slices to send:
Optional 'thank-you' note:
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

Ranch Hand
Posts: 282
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
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?

Ranch Hand
Posts: 3141
• Number of slices to send:
Optional 'thank-you' note:
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

High Plains Drifter
Posts: 7289
• Number of slices to send:
Optional 'thank-you' note:
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