ProgrammingVery hardGATE CS · Product-company placement

What is printed?

What is printed?
t = ([1],)
try:
    t[0] += [2]
except TypeError:
    pass
try:
    t[0] = t[0] + [3]
except TypeError:
    pass
print(t)
  1. A. ([1],)
  2. B. ([1, 2, 3],)
  3. C. ([1, 2],)
  4. D. ([1, 3],)

Correct answer: C. ([1, 2],)

t[0] += [2] first extends the list in place (succeeds) and only then fails on the tuple item assignment, so 2 sticks. t[0] = t[0] + [3] builds a new list and fails on assignment without touching the original, so 3 is lost.

More Python Programming questions

Exams that ask this topic