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

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.

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

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

posted

0

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?