In RHE Final Exam Q#27 asks:
What is the value of the following expression?
Math.round(Math.random() + 2.50001);
Possible answers are
a) 2
b) 3
c) It is impossible to say.
The correct answer in the book is b) and the following explanation is provided:
" 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."
However, what happens if Math.random() returns 0.999999? If I am not mistaken, 0.999999 + 2.50001 = 3.500009 which should round up to 4, not 3. Am I missing something?
Thanks,
Milena
What is the value of the following expression?
Math.round(Math.random() + 2.50001);
Possible answers are
a) 2
b) 3
c) It is impossible to say.
The correct answer in the book is b) and the following explanation is provided:
" 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."
However, what happens if Math.random() returns 0.999999? If I am not mistaken, 0.999999 + 2.50001 = 3.500009 which should round up to 4, not 3. Am I missing something?
Thanks,
Milena