ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
a = [1, 2, 3] b = a a = a + [4] b += [5] print(a, b)
- A. [1, 2, 3, 4, 5] [1, 2, 3, 4, 5]
- B. [1, 2, 3, 4] [1, 2, 3, 5]
- C. [1, 2, 3, 4] [1, 2, 3, 4, 5]
- D. [1, 2, 3, 5] [1, 2, 3, 5]
Correct answer: B. [1, 2, 3, 4] [1, 2, 3, 5]
a = a + [4] builds a NEW list and rebinds a, so b still refers to the old [1, 2, 3]. b += [5] then extends that old list in place. The two names end up with different lists.
More Python Programming questions
- What is printed? python print(-2 2, (-2) 2, 2 -1)
- What is printed? python print(0 or [] or "x", 1 and 0 and 5, None or 0)
- What is printed? python x = [1, 2] g = (n 10 for n in x) x.append(3) x = [7] print(list(g))
- What is printed? python class Bag: items = [] def add(self, v): self.items.append(v) a, b = Bag(), Bag() a.add(1); b.add(2) print(a.items…
- What is printed? python class A: def who(self): return "A" class B(A): def who(self): return "B" + super().who() class C(A): def…
- What is printed? python try: try: raise ValueError except TypeError: print("T", end=" ") finally: print("F1", end=" ") except ValueError…