25.4k views
1 vote
Assume that rand is an object of the Random class.

Which of the following ranges is generated by this statement:
int number = rand.nextInt(250) - 125;
(A) [-125..125]
(B) [-124..124]
(C) [-125..124]
(D) [-125..126]
(E) [-125..250]

User Hyunjoo
by
8.7k points

1 Answer

3 votes

Final answer:

The correct range generated by the statement is (C) [-125..124], shifting the range of the nextInt method by subtracting 125.

Step-by-step explanation:

The range generated by int number = rand.nextInt(250) - 125; is (C) [-125..124]. The nextInt(250) method generates a random integer between 0 (inclusive) and 250 (exclusive). By subtracting 125 from the result, we shift this range by 125 units to the left, resulting in the range starting at -125 (0 - 125) and ending at 124 (249 - 125).

User Arbuz
by
8.3k points