Practice Assignment in CS Thinking

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

Quick Recap

The operation of storing a value in a variable. The variable name goes on the left of the assignment operator, and the value or expression goes on the right. The right side is evaluated first, then the result is stored in the variable on the left.

Assignment is like putting a label on a box and putting something inside. The label is the variable name; the contents is the value.

Showing a random 20 of 50 problems.

Example 1

easy
After `flag = False`, what value is stored in `flag`?

Example 2

easy
Is `=` the assignment operator or the equality test?

Example 3

medium
Trace: `x = 5`, `y = 0`, `y += x`, `x += y`. Final `x`, `y`? (`+=` adds in place)

Example 4

hard
Trace: `x = 3`, `y = x + 2`, `x = y * y`, `y = x - y`. Final `x`, `y`?

Example 5

easy
Trace: `x = 6`, `y = x`. What is `y`?

Example 6

challenge
Trace without a temp swap attempt using arithmetic: `a = 6`, `b = 4`, `a = a + b`, `b = a - b`, `a = a - b`. Final `a`, `b`?

Example 7

medium
Trace: `x = 6`, `x *= 2`, `x -= 3`. What is `x`?

Example 8

hard
Given `a = 2`, `b = 5`, `c = 8`, run `a = c`, `c = b`, `b = a`. Final `a`, `b`, `c`?

Example 9

hard
Trace: `x = 2`. Run `x = x * x` four times. What is `x`?

Example 10

hard
Trace arithmetic swap: `a = 9`, `b = 2`, `a = a + b`, `b = a - b`, `a = a - b`. Final `a`, `b`?

Example 11

easy
Trace: `s = 4`, `s = s * 5`. What is `s`?

Example 12

easy
After `flag = True`, what value is stored in `flag`?

Example 13

medium
Trace the values of x and y after each line: SET x = 10 SET y = x + 5 SET x = y * 2 SET y = x - y What are the final values of x and y?

Example 14

challenge
Given `a = 1`, `b = 2`, `c = 3`, then `a = b`, `b = c`, `c = a`. Final `a`, `b`, `c`?

Example 15

easy
After `n = 9`, evaluate `n = n - 2`. What is `n`?

Example 16

easy
After `x = 5`, what is the value of `x`?

Example 17

easy
Trace: `a = 10`, `b = a - 4`. What is `b`?

Example 18

medium
Trace: `count = 0`. Run `count = count + 1` five times. What is `count`?

Example 19

easy
Trace: `x = 2`, `y = x`. What is `y`?

Example 20

easy
What is the output? SET a = 3. SET b = 4. SET a = a + b. SET b = a - b. OUTPUT a, b.