ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
import copy a = [[1, 2], [3]] b = copy.copy(a) c = copy.deepcopy(a) a[0].append(9) a.append([4]) print(len(b), b[0], c[0])
- A. 2 [1, 2] [1, 2]
- B. 3 [1, 2, 9] [1, 2, 9]
- C. 2 [1, 2, 9] [1, 2]
- D. 3 [1, 2, 9] [1, 2]
Correct answer: C. 2 [1, 2, 9] [1, 2]
copy.copy makes a new outer list that shares the inner lists. a.append([4]) does not change b's length (still 2), but a[0].append(9) changes the shared inner list, so b[0] shows 9. deepcopy copied the inner lists too, so c[0] is still [1, 2].
More Python Programming questions
- What is printed? python x = [1] 3 for i in range(3): x[i] = x[i - 1] + i print(x)
- What is printed? python x = "g" def outer(): x = "o" def inner(): global x x = "i" inner() return x print(outer(), x)
- What is printed? python s = " Py Thon " print(s.strip().lower().replace(" ", "").count("o"), s.split(" ").count(""))
- What is printed? python def f(x): try: r = 10 // x except ZeroDivisionError: return "Z" else: return r finally: print("F", end=" ")…
- What is printed? python class P: def repr(self): return "R" def str(self): return "S" print(P(), [P()], f"{P()!r}")
- What is printed? python class K: def eq(self, other): return True try: print({K(): 1}) except TypeError: print("TypeError")