File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Mock Exam Errata and the fly likes RHE question re Math.round() and random() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "RHE question re Math.round() and random()" Watch "RHE question re Math.round() and random()" New topic
Author

RHE question re Math.round() and random()

sylvia weller
Greenhorn

Joined: Jan 07, 2002
Posts: 29
This is a question from the RHE final exam:

What is the value of the following expression?
Math.round(Math.random() + 2.50001);
A) 2
B) 3
C) It is impossible to say

The correct answer is B)3 and here's the explanation:
Math.random() returns a double greater than or equal to 0.0 and less than 1.0. Math.random() + 2.50001 is a double greater than 2.5 and less than 3.5. Math.round() of any number between but not including 2.5 and 3.5 is 3.

Couldn't Math.random() return, say, .999999, in which case 2.50001 + .999999 = 3.500009. And wouldn't that round up to 4? What am I getting wrong?
Thanks, Sylvia
Rick Reumann
Ranch Hand

Joined: Apr 03, 2001
Posts: 281
Hmmm...that's really weird. I'll be interested to hear a better explanation because I agree with you.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
Check the on-line errata for the book.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205

Corrected in the 2nd printing: The answer for Question #27 is in error. The correct answer is C, and the explanation is: Math.random() returns a number greater than or equal to zero and strictly less than one. If the value is greater than 0.99999, the full expression rounds to 4; otherwise the expression rounds to 3.


Good thinking sylvia! You are correct.


Rob
SCJP 1.4
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Moving this to mock exam errata forum.


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

As a side-note, I lost the battle on this particular question to have the answers re-written. The "correct answer" (It is impossible to say) is at least as bad as saying the answer must be 3; it is, after all, pre-determined what the range of possible answers are: it's either 3 or 4, and there's Nothing impossible about knowing that.
That's what happens when you get editors who don't know programming, though. Oh well, seems like enough people are happy with C) that it will stand.


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

Joined: Jan 30, 2000
Posts: 18671
Maybe you should have tried to get them to replace Math.random() with 0.99 * Math.random() instead. Ah, well...


"I'm not back." - Bill Harding, Twister
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

Doh! And I usually pride myself on finding the workaround that least taxes the editors. Darn, that's a good one...
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
Originally posted by Michael Ernest:
The "correct answer" (It is impossible to say) is at least as bad as saying the answer must be 3; it is, after all, pre-determined what the range of possible answers are: it's either 3 or 4, and there's Nothing impossible about knowing that.

Actually, "it is impossible to say" statement is valid for Java2K language.
"Java2K is not a deterministic language, but a probabilistic one. Every builtin function has two different implementations, one of which is randomly chosen by the sandbox at runtime."
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Java2K language...
An esoteric programming language is a programming language deliberately designed to be unusable.

What a waste of brain potential... What benefit does an esoteric language have?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: RHE question re Math.round() and random()