+ 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));
1 Answer
+ 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.