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.
Each variable is a small whiteboard with its name written at the top. Assigning means erasing the board and writing the new value, so it only ever shows the latest one, and copying from one board to another copies what is written right now. The picture holds for numbers and text; for lists, many languages copy a link to the same list instead.
Showing a random 20 of 50 problems.
Example 1
easyAfter `flag = False`, what value is stored in `flag`?
Example 2
easyIs `=` the assignment operator or the equality test?
Example 3
mediumTrace: `x = 5`, `y = 0`, `y += x`, `x += y`. Final `x`, `y`? (`+=` adds in place)
Example 4
hardTrace: `x = 3`, `y = x + 2`, `x = y * y`, `y = x - y`. Final `x`, `y`?
Example 5
easyTrace: `x = 6`, `y = x`. What is `y`?
Example 6
challengeTrace 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
mediumTrace: `x = 6`, `x *= 2`, `x -= 3`. What is `x`?
Example 8
hardGiven `a = 2`, `b = 5`, `c = 8`, run `a = c`, `c = b`, `b = a`. Final `a`, `b`, `c`?
Example 9
hardTrace: `x = 2`. Run `x = x * x` four times. What is `x`?
Example 10
hardTrace arithmetic swap: `a = 9`, `b = 2`, `a = a + b`, `b = a - b`, `a = a - b`. Final `a`, `b`?
Example 11
easyTrace: `s = 4`, `s = s * 5`. What is `s`?
Example 12
easyAfter `flag = True`, what value is stored in `flag`?
Example 13
mediumTrace 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?
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
challengeGiven `a = 1`, `b = 2`, `c = 3`, then `a = b`, `b = c`, `c = a`. Final `a`, `b`, `c`?
Example 15
easyAfter `n = 9`, evaluate `n = n - 2`. What is `n`?
Example 16
easyAfter `x = 5`, what is the value of `x`?
Example 17
easyTrace: `a = 10`, `b = a - 4`. What is `b`?
Example 18
mediumTrace: `count = 0`. Run `count = count + 1` five times. What is `count`?
Example 19
easyTrace: `x = 2`, `y = x`. What is `y`?
Example 20
easyWhat is the output? SET a = 3. SET b = 4. SET a = a + b. SET b = a - b. OUTPUT a, b.