PrepGo

AP Computer Science Principles Practice Quiz: Random Values

Written by AP Content Team, Verified for 2026 AP Exams, Last updated: May 2026

Test your understanding with short quizzes. This quiz has 9 questions to check your progress.

Question 1 of 9

A program contains the expression `RANDOM(1, 10)`. Which of the following values could NOT be a result of this expression?

All Questions (9)

A program contains the expression `RANDOM(1, 10)`. Which of the following values could NOT be a result of this expression?

A) 1

B) 5

C) 10

D) 11

Correct Answer: D

The function `RANDOM(a, b)` generates a random integer from `a` to `b`, inclusive. The range of possible values is [1, 10]. The value 11 is outside this range.

Which of the following expressions generates a random integer from the set {0, 1, 2, 3}?

A) RANDOM(1, 4)

B) RANDOM(0, 3)

C) RANDOM(0, 4)

D) RANDOM(1, 3)

Correct Answer: B

`RANDOM(a, b)` includes both endpoints. `RANDOM(0, 3)` generates an integer from 0 to 3, inclusive, which corresponds to the set {0, 1, 2, 3}.

Consider the expression `5 + RANDOM(1, 3)`. What is the complete set of all possible values that can be generated?

A) {5, 6, 7}

B) {1, 2, 3}

C) {6, 7, 8}

D) {5, 8}

Correct Answer: C

The expression `RANDOM(1, 3)` can produce the values 1, 2, or 3. Adding 5 to each of these possibilities yields: 5 + 1 = 6, 5 + 2 = 7, and 5 + 3 = 8. The set of possible results is {6, 7, 8}.

Based on the provided information, what is a primary consequence of including `RANDOM(1, 100)` in a program?

A) The program will require more memory to run.

B) The program is guaranteed to produce a different output every time it is executed.

C) The program may produce a different result on separate executions.

D) The program will always execute in the same amount of time.

Correct Answer: C

The provided content explicitly states: 'Using random number generation in a program means each execution may produce a different result.' While it might produce the same result twice by chance, the key characteristic is that the result is not fixed.

Which of the following expressions generates a random integer from the set {10, 20, 30}?

A) RANDOM(10, 30)

B) 10 * RANDOM(1, 3)

C) 10 + RANDOM(0, 20)

D) RANDOM(1, 3) + 10

Correct Answer: B

`RANDOM(1, 3)` generates the values 1, 2, or 3. Multiplying each of these by 10 results in the set {10, 20, 30}. The other options generate different sets of values.

What is the minimum possible value that can be generated by the expression `RANDOM(1, 5) + RANDOM(1, 5)`?

A) 1

B) 2

C) 5

D) 10

Correct Answer: B

To find the minimum possible sum, you must find the minimum value for each independent `RANDOM` call and add them together. The minimum value for `RANDOM(1, 5)` is 1. Therefore, the minimum sum is 1 + 1 = 2.

Which expression generates a random odd integer from 1 to 9, inclusive (i.e., one of {1, 3, 5, 7, 9})?

A) RANDOM(1, 9)

B) 2 * RANDOM(1, 5) - 1

C) 2 * RANDOM(0, 4) + 1

D) 2 * RANDOM(1, 4) + 1

Correct Answer: C

`RANDOM(0, 4)` generates the set {0, 1, 2, 3, 4}. Multiplying by 2 gives {0, 2, 4, 6, 8}. Adding 1 to each value results in the desired set {1, 3, 5, 7, 9}.

Consider the expression `result <- RANDOM(0, 2) - RANDOM(0, 2)`. What is the complete set of possible values for `result`?

A) {0}

B) {-1, 0, 1}

C) {-2, -1, 0, 1, 2}

D) {0, 1, 2}

Correct Answer: C

Each `RANDOM(0, 2)` call can result in {0, 1, 2}. We must consider all possible combinations of subtraction. Max value: 2 - 0 = 2. Min value: 0 - 2 = -2. Other values include 1-0=1, 1-1=0, 1-2=-1, etc. The full range of integer results is from -2 to 2, inclusive.

A variable `x` is assigned a value using the statement `x <- RANDOM(10, 20)`. What is the maximum possible value of the expression `x - RANDOM(1, 5)`?

A) 15

B) 19

C) 20

D) 25

Correct Answer: B

To maximize the result of a subtraction (A - B), one must maximize the value of A and minimize the value of B. The maximum value for `x` from `RANDOM(10, 20)` is 20. The minimum value for the second term, `RANDOM(1, 5)`, is 1. Therefore, the maximum possible result of the expression is 20 - 1 = 19.