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.
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
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.
posted 19 years ago
Thanks you guys' reply. 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?
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