53.1k views
4 votes
Assume that rand is an object of the Random class.

Which of the following statements generates a random number in the [41..101] range?
(A) int number = rand.nextInt(41) + 101;
(B) int number = rand.nextInt(101) + 41;
(C) int number = rand.nextInt(61) + 41;
(D) int number = rand.nextInt(60) + 41;

1 Answer

4 votes

Final answer:

The correct Java statement to generate a random number in the range [41..101] is 'int number = rand.nextInt(61) + 41;' as it creates a range from 41 to 101 inclusive.

Step-by-step explanation:

The student is asking which statement generates a random number in the range of [41..101]. The correct option is (C) int number = rand.nextInt(61) + 41; This line of code will generate a random integer between 0 and 60 (since nextInt(61) generates a number in the range [0..60]) and then adds 41 to shift the range to [41..101].

Options A, B, and D will not give the desired range. Option A adds 101 to a number between 0 and 40, which results in numbers outside the desired range. Option B adds 41 to a number between 0 and 100, which could result in numbers higher than 101. Option D adds 41 to a number between 0 and 59, leading to a maximum of 100, which does not cover the entire desired range.

User Tgriesser
by
8.0k points