Variable Examples in CS Thinking

Start with the recap, study the fully worked examples, then use the practice problems to check your understanding of Variable.

This page combines explanation, solved examples, and follow-up practice so you can move from recognition to confident problem-solving in CS Thinking.

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 labeled box you can put things in, take things out, or change what's inside.

Read the full concept explanation โ†’

How to Use These Examples

  • Read the first worked example with the solution open so the structure is clear.
  • Try the practice problems before revealing each solution.
  • Use the related concepts and background knowledge badges if you feel stuck.

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).

Sense of Study hint: When using variables, choose descriptive names that explain what the data represents (e.g., 'total_score' not 'x'). Initialize variables before using them, and keep track of how each variable's value changes as the program runs.

Worked Examples

Example 1

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

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 these problems on your own first, 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=5x = 5; x=x+2x = x + 2. What is xx?

Example 4

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

Example 5

easy
What three things does a variable typically have?

Example 6

easy
Trace: count=0count = 0; count=count+1count = count + 1; count=count+1count = 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=s = 'hi'; s=s+โ€ฒ!โ€ฒs = s + '!'. What is ss?

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=2a=2; b=3b=3; a=ba=b; b=ab=a. Final aa and bb โ€” does this swap them?

Example 12

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

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=3x = 3; y=xโˆ—2y = x * 2; x=10x = 10; z=x+yz = x + y. Final zz?

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 = [10, 20]; list[0]=99list[0] = 99. What is the list now?

Example 17

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

Example 18

medium
Trace: sum=0sum=0; for vv in [3,โˆ’1,4][3, -1, 4]: sum=sum+vsum = sum + v. Final sum?

Example 19

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

Example 20

challenge
Trace: a=1a=1; b=2b=2; c=3c=3; a=ba=b; b=cb=c; c=ac=a. Final a,b,ca,b,c?

Example 21

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

Example 22

challenge
Trace: x=2x=2; for ii in range(3): x=xโˆ—xx = x * x. Final xx (range(3) is 3 passes)?

Example 23

easy
Trace: a=10a = 10; a=aโˆ’3a = a - 3. Final aa?

Example 24

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

Example 25

easy
Trace: n=4n = 4; n=nโˆ—nn = n * n. Final nn?

Example 26

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

Example 27

easy
Trace: s=s = 'cat'; s=s+โ€ฒsโ€ฒs = s + 's'. Final ss?

Example 28

easy
What is wrong with: print(x)\text{print}(x); x=5x = 5?

Example 29

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

Example 30

medium
Trace swap without a temp using arithmetic: a=7a=7; b=3b=3; a=a+ba=a+b; b=aโˆ’bb=a-b; a=aโˆ’ba=a-b. Final a,ba, b?

Example 31

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

Example 32

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

Example 33

medium
Trace: x=โ€ฒAโ€ฒx = 'A'; x=xโˆ—3x = x * 3 (string repeat). Final xx?

Example 34

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

Example 35

medium
Trace: best=โˆ’โˆžbest = -\infty; for vv in [3,โˆ’1,4,1][3, -1, 4, 1]: if v>bestv > best: best=vbest = v. Final bestbest?

Example 36

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

Example 37

medium
In ifย x=5\text{if } x = 5 (single ==) versus ifย x==5\text{if } x == 5 (double ==), which one performs comparison?

Example 38

medium
Trace: sum=0sum=0; i=1i=1; while iโ‰ค5i \le 5: sum=sum+isum = sum + i; i=i+1i = i + 1. Final sumsum?

Example 39

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

Example 40

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

Example 41

hard
Trace: a=1a = 1; b=2b = 2; c=3c = 3; a=a+b+ca = a + b + c; b=aโˆ’bโˆ’cb = a - b - c; c=aโˆ’bโˆ’cc = a - b - c. Final a,b,ca, b, c?

Example 42

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

Example 43

hard
Trace: n=5n=5; f=1f=1; while n>1n>1: f=fโˆ—nf = f * n; n=nโˆ’1n = n - 1. Final ff?

Example 44

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

Example 45

challenge
Trace: a=2a = 2; for ii in range(4): a=aโˆ—aa = a * a. Final aa? (range(4) = 4 passes)

Example 46

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