Variables and Assignments MCQ Practice

Question 1 of 10

A program executes the assignments below in order.

x ← 4
y ← 9
x ← y
DISPLAY (x)

What is displayed?

  1. A. 9
  2. B. 13
  3. C. Nothing is displayed.
  4. D. 4

Question 2 of 10

After the segment below runs, which pair of values is stored?

a ← 2
b ← 5
a ← a + 1
b ← a
  1. A. a is 2 and b is 5
  2. B. a is 3 and b is 3
  3. C. a is 3 and b is 5
  4. D. a is 2 and b is 3

Question 3 of 10

A student wants to swap the values in p and q. Which attempt correctly swaps them?

Attempt Code
1 p ← q then q ← p
2 temp ← p then p ← q then q ← temp
3 p ← p then q ← q
4 q ← p then p ← q
  1. A. Attempts 2 and 3
  2. B. Attempt 1 only
  3. C. Attempt 2 only
  4. D. Attempts 1 and 4

Question 4 of 10

Consider the segment below.

m ← 7
n ← 3
m ← n
n ← m
DISPLAY (m)
DISPLAY (n)

What is displayed, in order?

  1. A. 7 then 7
  2. B. 7 then 3
  3. C. 3 then 7
  4. D. 3 then 3

Question 5 of 10

The assignments below run in order. What are the final values of x and y?

x ← 1
y ← 2
x ← x + y
y ← x - y
x ← x - y
  1. A. x is 2 and y is 1
  2. B. x is 1 and y is 2
  3. C. x is 3 and y is 1
  4. D. x is 2 and y is 2

Question 6 of 10

Which statement about assignment is accurate?

  1. A. A variable can hold many different values at the same moment.
  2. B. Assignment replaces the previous value stored in a variable with a new value.
  3. C. The left side of ← is evaluated before the right side.
  4. D. DISPLAY changes the value stored in a variable.

Question 7 of 10

What does the following segment display?

score ← 10
bonus ← 4
score ← score + bonus
bonus ← 0
DISPLAY (score)
  1. A. 4
  2. B. 10
  3. C. 14
  4. D. 0

Question 8 of 10

A robot stores two sensor readings in left and right. A programmer runs:

left ← 8
right ← 5
temp ← left
left ← right
right ← temp

Which statement is true afterward?

  1. A. left is 5 and right is 5
  2. B. left is 8 and right is 8
  3. C. left is 8 and right is 5
  4. D. left is 5 and right is 8

Question 9 of 10

After these assignments, what is the value of total?

a ← 6
b ← 2
c ← 1
total ← a
total ← total + b
total ← total + c
  1. A. 9
  2. B. 3
  3. C. 6
  4. D. 8

Question 10 of 10

Which sequence leaves first holding the original value of second and second holding the original value of first?

  1. A. first ← first + 1 then second ← second - 1
  2. B. temp ← first then first ← second then second ← temp
  3. C. first ← first then second ← second
  4. D. first ← second then second ← first