ProgrammingHardGATE CS · Product-company placement

What is printed?

What is printed?
import copy
class Node:
    def __init__(self): self.kids = []
a = Node()
a.kids.append(a)
b = copy.deepcopy(a)
print(b.kids[0] is b, b.kids[0] is a, copy.copy(a).kids is a.kids)
  1. A. False True True
  2. B. True False False
  3. C. True False True
  4. D. RecursionError

Correct answer: C. True False True

deepcopy keeps a memo of objects already copied, so a self-reference is reproduced as a reference to the new copy (no infinite recursion). A shallow copy shares the kids list.

More Python Programming questions

Exams that ask this topic