PrepGo

AP Computer Science A Practice Quiz: Implementing Selection and Iteration Algorithms

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

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

Question 1 of 7

Consider the following code segment. count ← 0 FOR i FROM 1 TO 50 INCLUSIVE { IF (i MOD 5 = 0) { count ← count + 1 } } DISPLAY(count) What is the final value displayed?

All Questions (7)

Consider the following code segment. count ← 0 FOR i FROM 1 TO 50 INCLUSIVE { IF (i MOD 5 = 0) { count ← count + 1 } } DISPLAY(count) What is the final value displayed?

A) 5

B) 10

C) 49

D) 50

Correct Answer: B

This algorithm determines the frequency with which a specific criterion is met. The code iterates through integers from 1 to 50. The counter `count` is incremented each time an integer `i` is evenly divisible by 5 (i.e., `i MOD 5 = 0`). The numbers that satisfy this condition are 5, 10, 15, 20, 25, 30, 35, 40, 45, and 50. There are 10 such numbers, so the final value of `count` is 10.

The following algorithm is intended to compute the sum of the individual digits of a positive integer `num`. sum ← 0 REPEAT UNTIL (num = 0) { digit ← num MOD 10 sum ← sum + digit num ← num / 10 // integer division } DISPLAY(sum) If the initial value of `num` is 384, what value is displayed?

A) 384

B) 12

C) 15

D) 96

Correct Answer: C

This is a standard algorithm for identifying and processing individual digits in an integer. Let's trace the execution with num = 384: 1. Loop 1: digit = 384 % 10 = 4. sum = 0 + 4 = 4. num = 384 / 10 = 38. 2. Loop 2: digit = 38 % 10 = 8. sum = 4 + 8 = 12. num = 38 / 10 = 3. 3. Loop 3: digit = 3 % 10 = 3. sum = 12 + 3 = 15. num = 3 / 10 = 0. The loop terminates because num is now 0. The final value of `sum`, which is 15, is displayed.

Consider the following algorithm, which processes a sequence of input values: 25, 18, 32, 12, 21. // Assume getNextInput() returns the next value in the sequence. minVal ← 100 REPEAT 5 TIMES { currentVal ← getNextInput() IF (currentVal < minVal) { minVal ← currentVal } } DISPLAY(minVal) What is the final value displayed?

A) 100

B) 25

C) 21

D) 12

Correct Answer: D

This algorithm is designed to determine a minimum value from a sequence. The variable `minVal` is initialized to 100. The algorithm then iterates through the sequence, updating `minVal` whenever a smaller value is encountered. 1. `currentVal` is 25. 25 < 100 is true. `minVal` becomes 25. 2. `currentVal` is 18. 18 < 25 is true. `minVal` becomes 18. 3. `currentVal` is 32. 32 < 18 is false. 4. `currentVal` is 12. 12 < 18 is true. `minVal` becomes 12. 5. `currentVal` is 21. 21 < 12 is false. After all inputs are processed, the final value of `minVal` is 12.

Which of the following best describes the result of the given code segment, assuming `num` is a positive integer? count ← 0 REPEAT UNTIL (num = 0) { digit ← num MOD 10 IF (digit > 5) { count ← count + 1 } num ← num / 10 // integer division } RETURN count

A) It computes the sum of the digits in `num`.

B) It determines the largest digit in `num`.

C) It determines the number of digits in `num` that are greater than 5.

D) It determines if `num` is evenly divisible by 5.

Correct Answer: C

This algorithm iterates through the individual digits of `num` using the standard `MOD 10` and `/ 10` technique. Inside the loop, it uses a selection statement (`IF`) to check if the current `digit` meets a specific criterion, which is being greater than 5. A counter, `count`, is incremented only when this criterion is met. Therefore, the algorithm determines the frequency of digits that are greater than 5.

The following code is intended to find the largest even integer from a sequence of inputs. The sequence to be processed is: 17, 24, 9, 30, 21, 16. // Assume getNextInput() returns the next value in the sequence. maxEven ← -1 REPEAT 6 TIMES { currentVal ← getNextInput() IF (currentVal MOD 2 = 0) { IF (currentVal > maxEven) { maxEven ← currentVal } } } DISPLAY(maxEven) What value is displayed?

A) 16

B) 24

C) 30

D) 21

Correct Answer: C

This algorithm combines finding a maximum value with a selection criterion (the number must be even). `maxEven` is initialized to -1. 1. Input: 17 (odd). No change. 2. Input: 24 (even). 24 > -1 is true. `maxEven` becomes 24. 3. Input: 9 (odd). No change. 4. Input: 30 (even). 30 > 24 is true. `maxEven` becomes 30. 5. Input: 21 (odd). No change. 6. Input: 16 (even). 16 > 30 is false. No change. The final value of `maxEven` is 30.

Consider the following code segment designed to compute the average of all numbers between 1 and 20 (inclusive) that are divisible by 4. sum ← 0 count ← 0 FOR i FROM 1 TO 20 INCLUSIVE { IF (i MOD 4 = 0) { sum ← sum + i count ← count + 1 } } average ← sum / count DISPLAY(average) What is the final value displayed?

A) 10

B) 12

C) 5

D) 60

Correct Answer: B

This algorithm computes an average for values meeting a specific criterion. First, it identifies all numbers between 1 and 20 that are divisible by 4: 4, 8, 12, 16, 20. The `sum` variable accumulates these values: 4 + 8 + 12 + 16 + 20 = 60. The `count` variable tracks how many such numbers there are, which is 5. Finally, the average is computed as `sum / count`, which is 60 / 5 = 12.

An algorithm is needed to determine if a positive integer `n` is a 'perfect number', meaning it is equal to the sum of its proper positive divisors (divisors excluding the number itself). For example, 6 is a perfect number because its proper divisors are 1, 2, and 3, and 1 + 2 + 3 = 6. Which of the following code segments correctly determines if `n` is a perfect number? A) divisorSum ← 1 FOR i FROM 2 TO n { IF (n MOD i = 0) { divisorSum ← divisorSum + i } } RETURN (divisorSum = n) B) divisorSum ← 0 FOR i FROM 1 TO n-1 { IF (n MOD i = 0) { divisorSum ← divisorSum + i } } RETURN (divisorSum = n) C) divisorSum ← 0 FOR i FROM 1 TO n { IF (n MOD i = 0) { divisorSum ← divisorSum + i } } RETURN (divisorSum = n) D) divisorSum ← 0 FOR i FROM 1 TO n/2 { IF (i MOD n = 0) { divisorSum ← divisorSum + i } } RETURN (divisorSum = n)

A) Segment A

B) Segment B

C) Segment C

D) Segment D

Correct Answer: B

This question requires developing an original algorithm based on a definition. To find the sum of proper divisors, we must iterate through numbers less than `n` and check for divisibility. - Segment A is incorrect because its loop goes up to `n`, incorrectly including `n` itself in the sum. - Segment B is correct. It initializes a sum to 0, iterates from 1 up to `n-1`, adds `i` to the sum if `i` is a divisor, and finally compares the sum to `n`. - Segment C is incorrect because it includes `n` in the sum of its divisors, so the sum will always be greater than `n` (for n>1). - Segment D is incorrect because the condition `i MOD n = 0` will only be true for `i=0` (if the loop started there), not for any positive `i` less than `n`.