ProgrammingModerateTCS NQT · Infosys

What is printed?

What is printed?
t = (1, [2])
t[1].append(3)
print(t, len(t))
try:
    t[0] = 5
except TypeError as e:
    print(type(e).__name__)
  1. A. (1, [2, 3]) 2 then ValueError
  2. B. (1, [2]) 2 then TypeError
  3. C. (1, [2, 3]) 3 then TypeError
  4. D. (1, [2, 3]) 2 then TypeError

Correct answer: D. (1, [2, 3]) 2 then TypeError

A tuple itself is immutable, so item assignment raises TypeError, but the list inside it can still be changed. The tuple's contents therefore show [2, 3] after the append.

More Python Programming questions

Exams that ask this topic