ProgrammingHardIBM · Deloitte pattern
What is printed?
What is printed?
a = {1, 2, 3}
b = a
b.add(4)
c = a.copy()
c.add(5)
print(a, c)- A. {1, 2, 3} {1, 2, 3, 4, 5}
- B. {1, 2, 3, 4} {1, 2, 3, 4, 5}
- C. {1, 2, 3, 4} {1, 2, 3, 5}
- D. {1, 2, 3} {1, 2, 3, 5}
Correct answer: B. {1, 2, 3, 4} {1, 2, 3, 4, 5}
b = a is an alias, so b.add(4) mutates the same set a refers to, making a {1,2,3,4}. c = a.copy() makes an independent shallow copy of a's current state; adding 5 to c does not affect a.
More Python Programming questions
- What is printed? python class Meta: shared = [] def init(self, x): self.shared.append(x) m1 = Meta(1) m2 = Meta(2) print(m1.shared…
- What is printed? python def outer(): data = [1, 2, 3] def modify(): data.append(4) modify() return data print(outer())
- What is printed? python a, b = 1, 2 a, b = b, a + b print(a, b)
- What is printed? python from dataclasses import dataclass @dataclass class Point: x: int y: int = 0 p1 = Point(1, 2) p2 = Point(1, 2)…
- What is printed? python x, y = "hello" print(x, y)
- What is printed? python print([x for x in range(20) if x % 2 == 0 if x % 3 == 0])