AP CSP Day 36: Program Testing
Share
Practice Question
Consider the following code:
scores ← [88, 92, 75, 95, 81, 90]
count ← 0
FOR EACH score IN scores
{
IF score ≥ 90
{
count ← count + 1
}
}
DISPLAY(count)Which of the following changes would cause the displayed value to be DIFFERENT from the original?
I. Changing the condition to score > 90
II. Changing the condition to score ≥ 85
III. Reversing the order of elements in scores
Original (score ≥ 90): 92, 95, 90 pass → count = 3. Change I (score > 90): 92, 95 pass (90 is excluded) → count = 2 (different). Change II (score ≥ 85): 88, 92, 95, 90 pass → count = 4 (different). Change III (reverse order): same elements, same count = 3 (no change).
B) Change II also produces a different result, not just change I. C) Change III does not affect the count because all elements are still checked regardless of order. D) Change III has no effect on the final count.
Students think reversing list order changes the count. FOR EACH visits every element regardless of order, so the count of elements meeting a condition is the same in any order.
FOR EACH always processes every element. Changing the order of elements affects the order of processing but not which elements satisfy a condition or how many do.
Keep Practicing!
Consistent daily practice is the key to AP CSP success.
AP CSP Resources Get 1-on-1 Help