aspose file tools*
The moose likes Mock Exam Errata and the fly likes Error in Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "Error in "final" test on cd for RHE book" Watch "Error in "final" test on cd for RHE book" New topic
Author

Error in "final" test on cd for RHE book

Charles Earwood
Greenhorn

Joined: Jun 04, 2002
Posts: 6
I found an error with question #27 on a test in the RHE book's cd. The name of the test is "final".
The problem is below:
Math.round(Math.random() + 2.50001)
Answers
A. 2
B. 3
C. It is impossible to say.

RHE says that the answer is B. I chose C. I wrote a simple program below to test this out and had the output that it could be either 3 or 4. This confirmed my suspicion. What do yo think?
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
The answer should be B. 3. Math.random() always returns a value x where 0 <= x < 1, so the argument of Math.round() is always strictly greater than 2.5 which means it always returns 3.
I found your code difficult to decipher so I wrote my own. I got ten 3's outputted to the screen several times.

[ June 21, 2002: Message edited by: Anthony Villanueva ]
Charles Earwood
Greenhorn

Joined: Jun 04, 2002
Posts: 6
Simply add the following:

2.50001 + .9999999 = 3.5000099
The reason you got 3's is because most of the time the return value of .random() is less than .99999 . In my code, I filter out all values that are less than the value necessary to get a 4.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
to Anthony,
However imagine random returns 0.99999 or anything bigger. The result is 4.
[ June 21, 2002: Message edited by: Jose Botella ]

SCJP2. Please Indent your code using UBB Code
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Whoooops! (so much for buying those lottery tickets....)
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

moving this to Mock Exam Errata now that it's been confirmed as a goof...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Error in "final" test on cd for RHE book