Multiple choice

What values can be generated by the following code: (int) (Math.random() * 3)

  1. Floating point numbers between 0 and 3.

  2. Integers between 0 and 2.

  3. Integers between 0 and 3.

  4. Doubles from 0 to 3, including 0 but not including 3.

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

Math.random() returns a double value greater than or equal to 0.0 and strictly less than 1.0. Multiplying this by 3 yields a value in the range [0.0, 3.0), and casting this result to an integer truncates the decimal portion, resulting in the integers 0, 1, or 2.