ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
a, *b, c = range(6)
x, y = y, x = 1, 2
print(a, b, c, x, y)
  1. A. 0 [1, 2, 3, 4] 5 2 1
  2. B. 0 [1, 2, 3, 4] 5 1 2
  3. C. 0 (1, 2, 3, 4) 5 2 1
  4. D. 0 [1, 2, 3, 4, 5] 5 2 1

Correct answer: A. 0 [1, 2, 3, 4] 5 2 1

Starred unpacking collects the middle values into a LIST, not a tuple. In a chained assignment the right-hand value (1, 2) is assigned to each target from left to right: first x, y = 1, 2, then y, x = 1, 2, which overwrites them to y = 1, x = 2. It is not a swap.

More Python Programming questions

Exams that ask this topic