ProgrammingModerateTCS NQT · Wipro pattern

What is printed?

What is printed?
first, *mid, last = [1, 2, 3, 4, 5]
a, (b, *c) = 1, (2, 3, 4)
print(mid, last, b, c)
  1. A. [2, 3] 5 2 [3, 4]
  2. B. [2, 3, 4] 5 3 [4]
  3. C. [2, 3, 4] 5 2 [3]
  4. D. [2, 3, 4] 5 2 [3, 4]

Correct answer: D. [2, 3, 4] 5 2 [3, 4]

A starred target collects the leftover items into a list, so mid gets 2, 3 and 4 and last gets 5. Nested unpacking gives b = 2 and c = [3, 4].

More Python Programming questions

Exams that ask this topic