Variable Examples in CS Thinking: 48 Problems with Answers

Worked examples of Variable to try before you open each solution, then practice problems with answers.

Worked Examples

Example 1

easy
Trace: SET x = 7. SET y = x + 3. SET x = y * 2. OUTPUT x, y.
Show solution

Answer

x = 20, y = 10.

First step

1.
Step 1: x = 7.

Full solution

  1. 2.
    Step 2: y = 7 + 3 = 10.
  2. 3.
    Step 3: x = 10 * 2 = 20. Output: x = 20, y = 10.
Variables are named containers for storing values. When we reassign a variable, the old value is replaced. Other variables that used the old value are not affected.

Example 2

medium
Show how to swap the values of two variables a and b using a temporary variable. Trace with a = 5, b = 9.

Practice Problems

Try each one, then open the solution to compare your method.

Example 1

easy
What is the output? SET score = 0. SET score = score + 10. SET score = score + 5. OUTPUT score.

Example 2

medium
Trace this swap algorithm: SET A = 3. SET B = 8. SET temp = A. SET A = B. SET B = temp. What are A and B at the end, and why is temp needed?

Example 3

easy
Trace: x=5; x=x+2. What is x?

Example 4

easy
After a=3; b=a; a=9, what is b?

Example 5

easy
What three things does a variable typically have?

Example 6

easy
Trace: count=0; count=count+1; count=count+1. Final count?

Example 7

easy
Which is a clearer variable name for a person's age: 'x' or 'age'?

Example 8

easy
Trace: s= 'hi'; s=s+′!′. What is s?

Example 9

easy
Using a variable before assigning it any value typically causes what kind of error?

Example 10

easy
In 'total = price', which side receives the value: total or price?

Example 11

medium
Trace: a=2; b=3; a=b; b=a. Final a and b — does this swap them?

Example 12

medium
Trace: x=4; y=x; x=x+1; y=y+10. Final x and y?

Example 13

medium
A counter starts at 0 and is incremented inside a loop running 6 times, but is reset to 0 each pass. Final value?

Example 14

medium
Trace: x=3; y=x∗2; x=10; z=x+y. Final z?

Example 15

medium
In a language where '=' assigns and '==' compares, what does 'x == 5' evaluate to when x is 5?

Example 16

medium
Trace: list=[10,20]; list[0]=99. What is the list now?

Example 17

medium
A program: n= '5' (a string); n=n+3. Why might this error or misbehave?

Example 18

medium
Trace: sum=0; for v in [3,−1,4]: sum=sum+v. Final sum?

Example 19

medium
Trace: flag= true; flag= NOT flag; flag= NOT flag. Final flag?

Example 20

challenge
Trace: a=1; b=2; c=3; a=b; b=c; c=a. Final a,b,c?

Example 21

challenge
A loop intends to track a maximum but initializes best=0. For input [−5,−2,−9], what wrong answer results and what is the fix?

Example 22

challenge
Trace: x=2; for i in range(3): x=x∗x. Final x (range(3) is 3 passes)?

Example 23

easy
Trace: a=10; a=a−3. Final a?

Example 24

easy
Trace: x=3; y=x; x=99. Final y?

Example 25

easy
Trace: n=4; n=n∗n. Final n?

Example 26

easy
Which variable name is clearer for a student's grade: 'g' or 'grade'?

Example 27

easy
Trace: s= 'cat'; s=s+′s′. Final s?

Example 28

easy
What is wrong with: print(x); x=5?

Example 29

medium
Trace: a=5; b=a+1; a=b+1; b=a+1. Final a,b?

Example 30

medium
Trace swap without a temp using arithmetic: a=7; b=3; a=a+b; b=a−b; a=a−b. Final a,b?

Example 31

medium
Trace: count=0; for v in [1,2,3,4,5]: if v%2==1: count=count+1. Final count?

Example 32

medium
Trace: total=0; items=[10,20,30]; for i in items: total=total+i. Final total?

Example 33

medium
Trace: x=′A′; x=x∗3 (string repeat). Final x?

Example 34

medium
Trace: a=[1,2]; b=a; a.append(3). Final b?

Example 35

medium
Trace: best=−∞; for v in [3,−1,4,1]: if v>best: best=v. Final best?

Example 36

medium
Trace: x=1; y=x; x=x+y; y=x+y. Final x,y?

Example 37

medium
In if x=5 (single =) versus if x==5 (double =), which one performs comparison?

Example 38

medium
Trace: sum=0; i=1; while i≤5: sum=sum+i; i=i+1. Final sum?

Example 39

medium
Trace: flag= true; count=0; while flag: count=count+1; if count==3: flag= false. Final count?

Example 40

medium
Trace: a,b=3,5; a,b=b,a. Final a,b?

Example 41

hard
Trace: a=1; b=2; c=3; a=a+b+c; b=a−b−c; c=a−b−c. Final a,b,c?

Example 42

hard
In a scope-aware language, x=1 is defined globally and a function sets x=2 locally. After the function returns, what is the global x?

Example 43

hard
Trace: n=5; f=1; while n>1: f=f∗n; n=n−1. Final f?

Example 44

hard
Trace: a=[1,2,3]; b=a[:] (copy); a.append(4). Final b?

Example 45

challenge
Trace: a=2; for i in range(4): a=a∗a. Final a? (range(4) = 4 passes)

Example 46

challenge
Trace: a=0; b=1; for i in range(6): a,b=b,a+b. Final a?

What to focus on

Core idea: Variables give meaningful names to data values so programs can store, retrieve, and modify them.

Common stuck point: = means 'assign' not 'equals'. x = x + 1 is valid (not a paradox).

Ask yourself: Does the program need to remember a value under a name and change it later?

Concept recap

A named container in a program that stores a value, which can be read, updated, or replaced. Variables have a name (identifier), a value (the data stored), and in many languages a type (what kind of data it holds).

A variable is like a whiteboard with a name written across the top. You can read what is on it, or wipe it and write something new, but there is room for only one thing at a time. Writing a new value erases the old one for good, unless you first copied it onto another whiteboard.

Read the full lesson on Variable