AP CSP 3.1 Exercise 2: Variables and Assignments Applied Challenge

Big Idea 3: Algorithms and Programming · Topic 3.1 · Exercise 2

Variables and Assignments: Applied Challenge

Six traces where the answer depends on remembering that assignment replaces rather than relates.

How this one is different

Nearly every wrong answer here comes from reading assignment as a permanent relationship instead of a one time copy. Trace the values in order.

Applied Practice

6 questions · scenario driven · every answer is recorded for your teacher

Question 1 of 6Trace
Predict first: what is stored in b when the last line runs?
What does this segment display?
a <- 5 b <- a a <- 9 DISPLAY b
Correct. The second line copies the value of a at that moment, which is 5. Changing a afterwards does not reach back into b.
Incorrect. 9 assumes b tracks a. Assignment copies a value once, it does not link the variables.
Incorrect. 14 adds the two values, but no addition appears in the segment.
Incorrect. DISPLAY b shows the value stored in b, not the name of another variable.
Question 2 of 6Trace
What does this segment display?
x <- 3 y <- 4 x <- y y <- x DISPLAY x + y
Incorrect. 6 would require both to end at 3, but the first swap step moved x to 4.
Incorrect. 3 is the original x, which was overwritten on the third line.
Incorrect. 7 is the sum of the original values, but both variables changed before the display.
Correct. x becomes 4, then y is set from the new x, which is also 4. The sum is 8.
Question 3 of 6Debug
A student wants to swap the values in p and q.
Why does this segment fail, and what is missing?
p <- q q <- p
Incorrect. It runs without error and leaves both variables holding the original q, which is not a swap.
Incorrect. Reversing the lines loses q instead of p. The order is not the defect.
Correct. The first line destroys the original p, so the second line copies the new value back. A temp holding one original value first is the standard fix.
Incorrect. A variable on both sides is entirely legal, and it is how accumulation works.
Question 4 of 6Apply
Which sequence correctly swaps the values of m and n?
Incorrect. This is the failed swap. The original m is gone after the first line.
Correct. Saving one value before overwriting it is the whole point of the temporary variable, and this order preserves both.
Incorrect. This copies m into n, then copies the new n back into m, leaving both holding the original m.
Incorrect. The second line copies the combined sum rather than the original m.
Question 5 of 6Trace
What does this segment display?
total <- 0 total <- total + 5 total <- total + 5 DISPLAY total
Correct. Each line reads the current total, adds 5, and stores the result back, so two updates give 10.
Incorrect. 55 would come from joining the digits as text rather than adding numbers.
Incorrect. 0 is the starting value, before either update runs.
Incorrect. 5 is the value after the first update only.
Question 6 of 6Analyze
A program uses a variable named x for a student count, a temperature, and a list index in different sections.
Which is the strongest specific objection?
Incorrect. Reusing a name is legal. It is a clarity problem, not a rule violation.
Incorrect. Nothing here causes an error. That is what makes it easy to leave in place.
Incorrect. Variable naming has no effect on execution speed.
Correct. Names are how a reader tracks meaning. One name carrying three unrelated meanings destroys that, and the cost lands on whoever debugs it.

Where to go next

Get in Touch

Whether you're a student, parent, or teacher — I'd love to hear from you.

Just want free AP CS resources?

Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]