AP CSP Day 58: Simulation Models
Share
Practice Question
What is displayed after the following code runs?
PROCEDURE process(list, threshold)
{
count ← 0
FOR EACH item IN list
{
IF item ≥ threshold
{
count ← count + 1
}
}
RETURN count
}
data ← [15, 22, 8, 30, 12, 25]
high ← process(data, 20)
low ← process(data, 10)
DISPLAY(high)
DISPLAY(low)First call process(data, 20): elements ≥20 are 22, 30, 25 → count=3. Second call process(data, 10): elements ≥10 are 15, 22, 30, 12, 25 → count=5 (only 8 is excluded). high=3, low=5.
B) Five elements are ≥10, not four (12 ≥ 10 is true). C) Three elements are ≥20, not two. D) Not all six elements are ≥10 because 8 < 10.
Students often miss boundary values. For threshold=10, the element 12 satisfies 12 ≥ 10. For threshold=20, the element 22 satisfies 22 ≥ 20. Carefully check each element against the threshold.
When a procedure is called multiple times with different arguments, trace each call independently. Do not carry state from one call to the next — local variables reset with each call.
Keep Practicing!
Consistent daily practice is the key to AP CSP success.
AP CSP Resources Get 1-on-1 Help