+ 2

What are the possible outcomes of following code? Explain.

a) 1 b) 2 c) 3 d) 0 System.out.print((int)(Math.random()*3));

29th Aug 2017, 6:53 AM
shadab gada
1 Resposta
+ 6
Math.random() returns a double from 0.0 to 0.9. This return value *3 ranges from 0.0 to 2.7. The cast to int simply cuts off the value without rounding, so values from 0 to 2 are possible. So a), b) and d) is correct.
29th Aug 2017, 7:14 AM
Tashi N
Tashi N - avatar