ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
t = (1, [2, 3], 4)
try:
    t[1] += [5]
except TypeError as e:
    print("error", end=" ")
print(t)
  1. A. (1, [2, 3, 5], 4)
  2. B. error (1, [2, 3], 4)
  3. C. error (1, [2, 3, 5], 4)
  4. D. (1, [2, 3], 4)

Correct answer: C. error (1, [2, 3, 5], 4)

t[1] += [5] first extends the list in place (lists support +=), then tries to store the result back into the tuple slot, which raises TypeError. The list was already mutated, so the tuple shows [2, 3, 5] despite the error.

More Python Programming questions

Exams that ask this topic