This week's book giveaway is in the Jobs Discussion forum.We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!See this thread for details.
Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login Win a copy of Java Interview Guide this week in the Jobs Discussion forum!

# RHE final exam question

Kathy Wang
Greenhorn

Joined: Sep 06, 2001
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

Joined: Jul 29, 2001
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

Joined: Aug 26, 2001
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

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Amit Agrawal
Ranch Hand

Joined: Aug 23, 2001
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

Joined: Sep 06, 2001
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

Joined: Aug 30, 2000
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

Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
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

Make visible what, without you, might perhaps never have been seen.
- Robert Bresson

I agree. Here's the link: http://aspose.com/file-tools

subject: RHE final exam question