AP CSP Day 34: Machine Learning Bias
Share
Practice Question
What is displayed after the following code runs?
n ← 64
count ← 0
REPEAT UNTIL n < 2
{
n ← n / 2
count ← count + 1
}
DISPLAY(count)Trace: n=64→2=32(count=1), 32→2=16(count=2), 16→2=8(count=3), 8→2=4(count=4), 4→2=2(count=5), 2→2=1(count=6). Now n=1 < 2 is true, loop stops. count = 6.
A) 5 stops one iteration too early, when n=2 instead of halving once more. B) 7 counts one extra iteration that doesn't occur. D) 32 confuses 64/2 with the iteration count.
Students miscalculate the stopping point. The loop continues while n ≥ 2, so n=2 still triggers another iteration (n becomes 1, count becomes 6). The condition n < 2 is only checked before each iteration.
For REPEAT UNTIL loops, the condition is checked BEFORE each iteration. If the condition is false, the body executes. Trace carefully until the condition becomes true.
Keep Practicing!
Consistent daily practice is the key to AP CSP success.
AP CSP Resources Get 1-on-1 Help