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)
- A. ([1, 2, 9], [3, 4])
- B. ([1, 2], [3, 4])
- C. TypeError
- 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
- What is printed? python import copy a = [1, 2, 3] b = a.copy() c = list(a) d = a[:] e = copy.copy(a) a.append(4) print(b, c, d, e)
- What is printed? python try: print("a") except Exception: print("b") else: print("c") finally: print("d")
- What is printed? python def h(): try: x = 1 / 0 except ZeroDivisionError: return "caught" else: return "no error" finally: print("always")…
- What is printed? python def k(): try: return "try" except Exception: return "except" else: return "else" print(k())
- What is printed? python s = "placement" print(s[-100:5])
- What is printed? python s = "abcdef" print(s[10:20])