ProgrammingHardCAT · MBA placement
What is printed?
What is printed?
import copy a = [[1], [2]] b = a[:] c = copy.deepcopy(a) a[0].append(9) print(b, c)
- A. [[1, 9], [2]] [[1], [2]]
- B. [[1], [2]] [[1, 9], [2]]
- C. [[1, 9], [2]] [[1, 9], [2]]
- D. [[1], [2]] [[1], [2]]
Correct answer: A. [[1, 9], [2]] [[1], [2]]
a[:] copies only the outer list, so b shares the inner list that receives 9. deepcopy copies the nested lists, so c is unaffected.
More Python Programming questions
- What is printed? python def f(x, acc=[]): acc.append(x) return acc f(1) f(2) print(f(3, []), f(4))
- What is printed? python def gridpaths(r, c, memo={}): if r == 0 or c == 0: return 1 if (r, c) not in memo: memo[(r, c)] = gridpaths(r - 1…
- What is printed? python def most2(s): cnt = {} l = best = 0 for r, ch in enumerate(s): cnt[ch] = cnt.get(ch, 0) + 1 while len(cnt) > 2…
- What is printed? python class Bag: items = [] def add(self, x): self.items.append(x) a, b = Bag(), Bag() a.add(1) b.add(2) print(a.items…
- What is printed? python print(max('bca'), min('bca', key=lambda c: (c, -ord(c))))
- What is printed? python from collections import deque d = deque([1, 2, 3]) d.extendleft([4, 5]) print(list(d))