AP CSP Practice: Variable Tracing & Assignments
Share
Big Idea 3: Algorithms and Programming
Day 3 Practice • AP CSP Daily Question
🎯 Focus: Variables and Conditionals
Practice Question
Consider the following code segment:
x ← 10
y ← 5
x ← x + y
y ← x - y
DISPLAY(y)
What value is displayed as a result of executing this code segment?
What This Tests: Big Idea 3 covers how variables are assigned and updated. The key is understanding that assignment statements execute in order, and each line uses the CURRENT values of variables.
Step-by-Step Trace
| Line | Code | x | y |
|---|---|---|---|
| 1 | x ← 10 | 10 | — |
| 2 | y ← 5 | 10 | 5 |
| 3 | x ← x + y | 15 | 5 |
| 4 | y ← x - y | 15 | 10 |
Line 4 uses the NEW value of x (15), not the original (10).
y = 15 - 5 = 10
Key Concept: Sequential Execution
Variables are updated one line at a time. When you see:
x ← x + y // x is NOW 15
y ← x - y // Uses x=15, not x=10!
The second line uses the UPDATED value of x, not the original.
Common Mistakes
Mistake: Answer A (5) - Using original x
If you calculated y ← 10 - 5, you used the ORIGINAL value of x. But x was already changed to 15 on line 3.
Mistake: Answer C (15) - Displaying x instead of y
The code displays y, not x. After all operations, y = 10.
💡 AP Exam Tip
Always trace variables line by line. Make a table showing the value of each variable after each line executes. This technique prevents most tracing errors!
Difficulty: Medium • Time: 2-3 minutes • Topic: 3.1 Variables and Assignments
Want More AP CSP Practice?
Get personalized help from an experienced AP CS teacher
AP CSP Study Guide Schedule 1-on-1 Tutoring