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= 'cat'; s=s+′s′. Final s?

Example 5

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

Example 6

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 7

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

Example 8

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

Example 9

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

Example 10

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 11

challenge
Trace: x=2; for i in range(3): x=x∗x. Final x (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=1; b=2; c=3; a=b; b=c; c=a. Final a,b,c?

Example 14

medium
Trace: a=5; b=a+1; a=b+1; b=a+1. Final a,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=3; y=x; x=99. Final y?

Example 17

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

Example 18

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

Example 19

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

Example 20

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