ProgrammingHardIBM · Deloitte pattern

What is printed?

What is printed?
import copy
t = ([1, 2], [3, 4])
t2 = copy.deepcopy(t)
t2[0].append(9)
print(t)
  1. A. ([1, 2, 9], [3, 4])
  2. B. ([1, 2], [3, 4])
  3. C. TypeError
  4. D. ([1, 2], [3, 4, 9])

Correct answer: B. ([1, 2], [3, 4])

Even though tuples are immutable, deepcopy still recursively copies the mutable lists inside them, producing fully independent inner lists in t2. So mutating t2[0] does not affect t.

More Python Programming questions

Exams that ask this topic