Practice Variable in CS Thinking

Use these practice problems to test your method after reviewing the concept explanation and worked examples.

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

Showing a random 20 of 50 problems.

Example 1

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

Example 2

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

Example 3

easy
A variable's ____ tells the program what kind of data it holds (e.g., int, string).

Example 4

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

Example 5

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

Example 6

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 7

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

Example 8

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

Example 9

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

Example 10

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 11

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

Example 12

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

Example 13

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 14

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 15

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

Example 16

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

Example 17

medium
Trace: x=3x = 3; y=xโˆ—2y = x * 2; x=10x = 10; z=x+yz = x + y. Final zz?

Example 18

medium
Trace: a=2a=2; b=3b=3; a=ba=b; b=ab=a. Final aa and bb โ€” does this swap them?

Example 19

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

Example 20

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?