Machine-translated by Google. Questions are authored in English.
Top

Discussion

Which statement is true given the following?

Double d = Math.random();

  • A.
    0.0 < d>
  • B.0.0 <= d < 1.0
  • C.Compilation fail
  • D.Cannot say.

Answer: B

Correct answer: 0.0 <= d < 1.0

Math.random() returns a double in the half-open interval from zero inclusive to one exclusive, and the assignment to a Double compiles because autoboxing converts the primitive to its wrapper. Zero is therefore a possible value and one is not - which matters whenever the result is scaled, since (int)(Math.random()*6)+1 gives 1 to 6 precisely because the upper bound is never reached.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.