AP CSP Topic 3.1 Coding Practice - Variables and Assignments

Big Idea 3: Algorithms and Programming · Topic 3.1 · Coding Practice

Variables and Assignments — Code It Yourself

Write real code and check it against the expected output. Pick Python or JavaScript; the AP pseudocode reference is there when you need it.

You warmed these up on paper in the Trace the Log and Pick the Type exercises. Now type and run them for real — the problems get harder as you go, ending with one you build from scratch. Predict the output first, then check yourself.

Problem 1 of 4

Create a variable name holding "Ada" and a variable age holding 15, then print exactly: Ada is 15

Expected output: Ada is 15

Show AP pseudocode reference
name ← "Ada"
age ← 15
DISPLAY(name + " is " + age)
Store each value in its own variable, then join them. In Python, str(age) turns the number 15 into text so it can be joined with +.

Problem 2 of 4

Predict first, then verify the classic trace. Set a to 1, set b to a, set a to 2, then print b. What single value does it print?

Expected output: 1

Show AP pseudocode reference
a ← 1
b ← a
a ← 2
DISPLAY(b)
b ← a copies a's value (1) at that moment; changing a to 2 afterward does not touch b. A variable keeps its most recent assigned value.

Problem 3 of 4

x is 4 and y is 9. Swap their values using a temporary variable, then print them on one line as: 9 4

Expected output: 9 4

Show AP pseudocode reference
temp ← x
x ← y
y ← temp
DISPLAY(x + " " + y)
Save x in temp before you overwrite it: temp gets x, then x gets y, then y gets temp. Without temp, x's original value is lost.

Problem 4 of 4

Spot the bug: this program should print 8 (total starts at 5, then 3 more is added), but it prints the wrong value. Fix it.

Expected output: 8

Show AP pseudocode reference
total ← 5
total ← total + 3
DISPLAY(total)
A variable keeps only its most recent value, so plain total = 3 throws away the 5. To ADD instead of replace, put total's current value on the right side of the assignment and add 3 to it.

Your code runs on a secure external service. Answers are checked automatically — nothing is stored.

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]