ProgrammingVery hardPython · GATE DA · Advanced
What is printed?
What is printed?
import copy a = [1, 2] b = [a, a] c = copy.deepcopy(b) d = copy.copy(b) c[0].append(3) print(b, c[0] is c[1], d[0] is a)
- A. [[1, 2, 3], [1, 2, 3]] True True
- B. [[1, 2], [1, 2]] True True
- C. [[1, 2, 3], [1, 2, 3]] False True
- D. [[1, 2], [1, 2]] False True
Correct answer: B. [[1, 2], [1, 2]] True True
deepcopy keeps one copy per original object through its memo, so c[0] is c[1] stays True while the original b is untouched; copy.copy shares the inner list a.
More Python Programming questions
- What is printed? python class M: def enter(self): print('in', end=' '); return self def exit(self, t, v, tb): print('exit', t.name if t…
- What is printed? python a = [0] 3 m = [a] 2 m[0][1] = 5 n = [[0] 3 for in range(2)] n[0][1] = 5 print(m, n, a)
- What is printed? python try: try: 1 / 0 except ZeroDivisionError as e: raise ValueError('bad') from None except ValueError as v…
- What is printed? python def deco(tag): def w(f): def g(a): return f'<{tag}>' + f(a) + f'</{tag}>' return g return w @deco('b') @deco('i')…
- What is printed? python g = (x 2 for x in range(3)) lst = [1, 2, 3] h = (y for y in lst if y > n) n = 1 lst = [5] print(list(g), list(h))
- What is printed? python class A: def new(cls, v): print('new', v) return super().new(B) if v else super().new(cls) def init(self, v)…