479,329 views
20 votes
20 votes
Consider the following code segment. Assume that a is greater than zero.

int a /* value not shown */; int b = a + (int) (Math.random() * a);
Which of the following best describes the value assigned to b when the code segment is executed? . A. a B. 2 * a C. A random integer between 0 and a - 1, inclusive D. A random integer between a and 2 * a, inclusive .E A random integer between a and 2 * a - 1, inclusive ,

User PradyJord
by
2.6k points

1 Answer

9 votes
9 votes

Final answer:

The value of variable b will be a random integer between a and 2 * a - 1, inclusive, when the given code segment is executed.

Step-by-step explanation:

The student's question concerns the value assigned to variable b when the code segment is executed. Given that variable a is greater than zero and the use of Math.random(), the value of b will be a plus a random integer between 0 and a - 1, inclusive.

Doing the math, we can figure out that the value of b will be a random integer between a and 2 * a - 1, inclusive. This corresponds to option E.

User Toshik Langade
by
3.2k points