AP CSP Day 40: Data Privacy
Share
Practice Question
What is displayed after the following code runs?
data ← [10, 20, 30, 40]
i ← 1
REPEAT UNTIL i > LENGTH(data)
{
IF data[i] < 25
{
REMOVE(data, i)
}
ELSE
{
i ← i + 1
}
}
DISPLAY(LENGTH(data))Trace: data=[10,20,30,40], i=1. data[1]=10<25: REMOVE index 1 → data=[20,30,40], i stays 1. data[1]=20<25: REMOVE index 1 → data=[30,40], i stays 1. data[1]=30, not <25: i=2. data[2]=40, not <25: i=3. 3>LENGTH(data)=2: stop. LENGTH = 2.
B) Both 10 and 20 are removed, leaving 2 elements, not 3. C) Two elements are removed, so the list does not keep all 4. D) 30 and 40 remain because they are not less than 25.
Students assume i always increments, which would skip elements after a removal. The key insight is that i only increments in the ELSE branch — when no removal occurs. This prevents skipping.
Removing elements during list traversal requires careful index management. If i increments after every removal, the element that shifts into the removed position gets skipped.
Keep Practicing!
Consistent daily practice is the key to AP CSP success.
AP CSP Resources Get 1-on-1 Help