AP CSP Day 31: Procedure Abstraction

Big Idea 3: Algorithms & Programming
Cycle 2 • Day 31 Practice • Hard Difficulty
Focus: Variables & Assignment

Practice Question

What is displayed after the following code runs?

p ← 2
q ← 5
p ← p + q
q ← p - q
p ← p - q
DISPLAY(p)
DISPLAY(q)
Why This Answer?

This is the classic swap-without-a-temporary-variable algorithm. Trace: p=2, q=5. After p ← p+q: p=7. After q ← p-q: q=7-5=2. After p ← p-q: p=7-2=5. Final values: p=5, q=2. The values are swapped.

Why Not the Others?

A) The values are exchanged, not preserved. C) p is modified again in the third step — students who stop tracing after line 4 get stuck on p=7. D) q is updated in the second step to 2, not kept at 5.

Common Mistake
Watch Out!

Students stop tracing too early, reporting p=7 after the third line and forgetting that p is reassigned again. Every assignment must be traced to completion.

AP Exam Tip

When a variable is reassigned multiple times in sequence, always use the most recently updated value. Never skip a step or use a stale value.

Keep Practicing!

Consistent daily practice is the key to AP CSP success.

AP CSP Resources Get 1-on-1 Help
Back to blog

Leave a comment

Please note, comments need to be approved before they are published.