240,776 views
31 votes
31 votes
Assume rand has been declared as an object of the random class. Consider the following statement:

int randomNum = rand. NextInt(5);

Which set or sets of integers represent possible values for randomNum? (3 points)

0, 1, 2, 3, 4

0, 1, 2, 3, 4, 5

0, 5

a. I only

b. II only

c. III only

d. I and III only

e. II and III only

User Betty Mock
by
2.9k points

1 Answer

19 votes
19 votes

Final answer:

The correct answer is (a) I only, as the method rand.nextInt(5) in Java will produce a random number between 0 and 4, both inclusive.

Step-by-step explanation:

The question asks about the possible values that the randomNum variable may take on after being assigned a value from rand.nextInt(5). In Java, the rand.nextInt(int n) method returns a pseudo-random number between 0 (inclusive) and the specified value n (exclusive). Therefore, the possible values that randomNum could be are 0, 1, 2, 3, and 4.

From the given options, (a) I only correctly represents the set of integers that randomNum might be after the execution of the statement given that n = 5 in rand.nextInt(n).

User Sherrod
by
3.0k points