CSPIQ · AP Computer Science Principles · Lesson 7 of 25
CSPIQ · AP Computer Science Principles

Lesson 07: Variables, Assignments & Mathematical Expressions

Big Idea 3 (AAP) · Phase 3

Objectives

Warm-Up

What does this display?

a ← 5
b ← a
a ← 10
DISPLAY (b)

If you said 10, you read b ← a as algebra — a permanent link between b and a. It isn't. Assignment copies the current value, at that instant. When line 2 runs, a is 5, so b gets 5. Line 3 changes a; b doesn't care.

Answer: 5.

This single idea — assignment copies now, creates no link — separates students who trace code correctly from students who guess. Everything in Big Idea 3 builds on it.


Core Concept

Variables and assignment

A variable is a named storage location holding one value at a time. In AP pseudocode, assignment uses the left arrow:

score ← 82

Read it right-to-left: evaluate the right side, store the result in the left side. Rules that matter:

  1. The right side is evaluated first, using current values. Then the result is stored.
  2. Assignment replaces the variable's old value. One value at a time; the old one is gone.
  3. Copies, not links. b ← a copies a's value into b. Future changes to either leave the other alone.
  4. A variable can appear on both sides: count ← count + 1 means "new count = current count + 1" — the standard increment.

That last pattern breaks algebra-brains: as an equation, count = count + 1 is impossible. As an assignment, it's Tuesday. The arrow is an action, not a claim.

The swap problem — a classic

To exchange the values of x and y, this fails:

x ← y
y ← x

Trace it with x = 3, y = 7: line 1 makes x = 7 (the 3 is destroyed). Line 2 copies x — now 7 — into y. Result: both 7. The fix needs a temporary variable:

temp ← x     temp = 3
x ← y        x = 7
y ← temp     y = 3   ✓ swapped

The exam asks the swap both ways: "why does the two-line version fail?" and "which sequence correctly swaps?" You now know both.

Arithmetic expressions

AP pseudocode has the four operators you expect — +, , *, / — plus MOD:

Order of operations: parentheses first; then *, /, and MOD (equal precedence, left to right); then + and (left to right). MOD binds like multiplication — that's the detail students miss:

1 + 3 MOD 2     = 1 + (3 MOD 2) = 1 + 1 = 2      (not 4 MOD 2 = 0)
12 / 4 * 3      = (12 / 4) * 3 = 9               (left to right, not 12/12)

What MOD is for

MOD looks like trivia until you see the patterns, which the exam uses constantly:

Pattern Meaning
x MOD 2 = 0 x is even (= 1 → odd)
x MOD 10 last digit of x
x MOD n wraps x into the range 0 to n−1 — circular structures: days of the week (day MOD 7), clock hours, positions around a circle

Wrap-around example: it's day 5 of the week (0-indexed: 0–6); what day is it 10 days later? (5 + 10) MOD 7 = 15 MOD 7 = 1. Day 1.

The trace table — your universal tool

For any multi-line code, track every variable line by line:

x ← 4
y ← x * 2 + 1
x ← y MOD 3
y ← x + y
Line x y
1 4
2 4 9
3 0 9
4 0 9

Line 3: y is 9, 9 MOD 3 = 0 → x = 0. Line 4: 0 + 9 = 9 → y = 9 (unchanged, as it happens). Final: x = 0, y = 9.

Rules of the table: one row per line executed; evaluate right side with current row values; write every value even if unchanged. Slower than guessing, and that's the point — the exam's distractors are precisely the answers guessers produce.


Worked Examples

Example 1 (easy): Assignment replaces

Problem: What is displayed?

points ← 12
points ← points + 3
points ← points * 2
DISPLAY (points)

Strategy: Trace, one line at a time.

Solution: points: 12 → 15 → 30. Displays 30.

Interpretation: Each assignment consumes the current value and replaces it. No line is skippable.

Example 2 (medium): Copies, not links

Problem: What are the final values of a and b?

a ← 8
b ← a + 2
a ← b * 2
b ← a − b

Solution:

Line a b
1 8
2 8 10
3 20 10
4 20 10

Line 4: a − b = 20 − 10 = 10 → b = 10 (same value, freshly computed). Final: a = 20, b = 10.

Interpretation: Line 3 changed a; the b from line 2 stayed 10 because assignment copied, not linked.

Example 3 (medium): MOD fluency

Problem: Evaluate each: (i) 26 MOD 7 (ii) 4 MOD 9 (iii) 45 MOD 5 (iv) 2 + 10 MOD 4

Solution: (i) 26 = 3×7 + 5 → 5 (ii) 9 fits into 4 zero times, remainder 4 → 4 (iii) 45 = 9×5 exactly → 0 (iv) MOD before + : 10 MOD 4 = 2, then 2 + 2 = 4

Interpretation: (ii) is the one that trips people: when a < b, a MOD b = a. (iv) is precedence: MOD binds like ×.

Example 4 (AP-style): Which code computes it?

Problem: A program needs to determine whether ticketNumber ends in the digit 0. Which expression is true exactly when it does?

(A) ticketNumber MOD 2 = 0 (B) ticketNumber / 10 = 0 (C) ticketNumber MOD 10 = 0 (D) ticketNumber − 10 = 0

Solution: (C). MOD 10 isolates the last digit; last digit 0 ⟺ remainder 0. (A) tests evenness — 24 is even but ends in 4. (B) is true only for numbers 0–9... actually only for values less than 10 (integer thinking aside, it's not a last-digit test). (D) is true only for exactly 10.

Interpretation: Test candidate expressions with concrete numbers — try 30 (should be true) and 24 (should be false) against each choice. Two test values usually kill three distractors.


Common Mistakes

  1. Reading as algebra. b ← a creates no ongoing relationship. Trace with values, never with "b equals a from now on."
  2. Forgetting the right side evaluates FIRST. In x ← x + y, the old x is used to compute, then destroyed by the store.
  3. The broken swap. x ← y then y ← x makes both equal. If an answer choice swaps without a temp variable, it's the trap.
  4. MOD precedence. 1 + 7 MOD 2 = 1 + 1 = 2, not 8 MOD 2 = 0. MOD binds like multiplication.
  5. a MOD b when a < b. The answer is a itself (e.g., 3 MOD 8 = 3), not 0, not b. Zero comes only from exact divisibility.

Practice Problems

Question 1

After this code runs, what is the value of y?

x ← 6
y ← x
x ← x + 4
Question 2
What is 38 MOD 6?
Question 3
What is 5 MOD 12?
Question 4

What is displayed?

n ← 3
n ← n * n
n ← n + 1
DISPLAY (n MOD 5)
Question 5
Which sequence correctly swaps the values of a and b?
Question 6
Evaluate: 20 − 12 / 4 + 2 * 3
Question 7
A parking meter shows time on a 12-hour clock (hours 0–11). It is hour 9; a car pays for 7 hours. Which expression gives the hour when time expires?
Question 8

After this code, what are x and y?

x ← 2
y ← 5
x ← x + y
y ← x − y
Question 9
Which expression is true exactly when n is an odd number?
Question 10
(Select two answers.) Which statements about the assignment total ← total + score are true?
Question 11

What is displayed?

a ← 4
b ← 7
c ← a
a ← b
b ← c
DISPLAY (a)
DISPLAY (b)
Question 12
A program numbers 60 photo slides 0 through 59 and displays them in a repeating loop. Which expression gives the slide shown at step s (steps also start at 0)?

Create PT Connection

Variables are where your Create PT program stores its state — the score, the running total, the user's choice. Two habits from this lesson carry straight into PT quality (and into Written Response 2(c)'s "how does your program manage data" territory):

Mini practice (WR 2(a) style): In two sentences, explain what count ← count + 1 does and why the same statement can run many times with different effects. Model: The statement adds 1 to the current value of count and stores the result back in count. Each time it executes, count's current value is different, so the statement's effect builds — which is how a program counts events over time.


Show answer key & explanations

(g) Answer Key

1. (D). Line 2 copies x's current value (6) into y; line 3's change to x doesn't touch y. (A) falls for the "link" misconception. (C) — a variable holds a value, not a name.

2. (D). 38 = 6×6 + 2 → remainder 2. (B) is the division answer; (C) is 38−6.

3. (D). 12 fits into 5 zero times; remainder is all of 5. Not 0 — zero requires exact divisibility. This is the a < b case from Common Mistake 5.

4. (D). n: 3 → 9 → 10. 10 MOD 5 = 0. (A) comes from skipping the +1 line (9 MOD 5 = 4... no — from 12 MOD 5); regardless: trace every line and the answer is 0.

5. (D). Save a first (temp ← a), overwrite a with b, restore the saved value into b. (A)/(B) are the broken two-line swaps (both variables end equal). (C) saves b but then destroys a with b before... trace it: temp=b; a←b (a and b now equal, old a lost); b←temp (b unchanged). Result: a = b = original b. Broken.

6. (B). Precedence first: 12/4 = 3 and 2*3 = 6. Then left to right: 20 − 3 + 6 = 23. (C) is what pure left-to-right evaluation with no precedence produces (20−12 = 8, ÷4 = 2, +2 = 4, ×3 = 12). (A) and (D) come from other precedence slips — e.g., subtracting the entire remaining expression, or multiplying the whole sum by 3. If your answer isn't 23, re-do the two precedence operations first and add/subtract last.

7. (B). Add, then wrap: (9+7) MOD 12 = 16 MOD 12 = 4 (expires at hour 4). (A) evaluates MOD first — 9 + (7 MOD 12) = 9 + 7 = 16, not a clock hour. Parentheses are the whole question.

8. (B). Trace: x ← 2+5 = 7; y ← 7−5 = 2. Final x = 7, y = 2. (This is the "swap using arithmetic" half-trick — fun, but use a temp variable in real code.) (A) forgets line 4; (D) uses the new x for both... which is exactly what the code does — but correctly: y ← x − y uses new x (7) and old y (5) = 2 ✓.

9. (A). Odd ⟺ remainder 1 when divided by 2. (D) is impossible — a MOD 2 is only ever 0 or 1. (B) is backwards and nonsensical (n MOD 1 is always 0).

10. (B) and (C). The increment pattern: evaluate right side with current values (B), effect = add score to total (C). (A) denies the most common statement in programming; (D) is the link misconception again.

11. (C). The correct three-step swap: c saves 4; a becomes 7; b gets the saved 4. Displays 7 then 4. (A) is "no swap happened"; (B)/(D) are broken-swap outcomes.

12. (A). Wrap step number into 0–59: s MOD 60. Step 61 → slide 1 ✓. (B) reverses the operands (60 MOD 61 = 60 — nonsense as a slide pattern); (C) grows without wrapping; (D) goes negative for s < 60.

Answer letter distribution check: D, D, D, D, D, B, B, B, A, B+C, C, A — singles: A×2, B×4, C×1, D×5 + multi (B,C). Deliberately D-heavy to offset L1–L6's B-lean; cumulative spread through L7 now roughly A 21%, B 33%, C 24%, D 21% — within tolerance, keep nudging toward 25/25/25/25.


Exam tip: Draw the trace table for every code question, even ones that look easy — the 20 seconds it costs is exactly what the distractors are priced at. On the real exam, students who trace get code questions right at very high rates; students who eyeball score barely above chance on the same items.

← All lessons
Lesson 8 ›
Score: 0/0 correct