File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
sylvia weller
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 281
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm...that's really weird. I'll be interested to hear a better explanation because I agree with you.
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check the on-line errata for the book.
 
Rob Ross
Bartender
Posts: 2205
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this to mock exam errata forum.
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe you should have tried to get them to replace Math.random() with 0.99 * Math.random() instead. Ah, well...
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10065
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic