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)- A. False True True
- B. True False False
- C. True False True
- 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
- What is printed? python a = [1, [2, 3]] b = a[:] c = list(a) d = [a] a[1] += [4] a[0] += 10 print(b, c is d, d[1] is a[1])
- What is printed? python s = "abc" it = iter(s) print(next(it), "".join(it), list(it), [c for c in s if c in "ab" or c == "z"])
- What is printed? python def f(n): if n == 0: return [] return [n] + f(n - 1) if n % 2 else f(n - 1) + [n] print(f(5))
- What is printed? python x = 1 def f(): print(x, end=" ") def g(): x = 2 f() g() h = lambda: x x = 3 print(h())
- What is printed? python def outer(): v = "a" def inner(): return v v = "b" r1 = inner() del v try: return r1, inner() except NameError as…
- What is printed? python print(eval("[i for i in range(3)]"), [x for x in "ab"] 2, list(map(str.upper, "ab")), {"aba"} == {"a", "b"})