ProgrammingHardGATE CS · Product-company placement
What is printed?
What is printed?
x = [1, 2, 3] y = [x, x] y[0] = y[0] + [4] y[1] += [5] print(x, y)
- A. [1, 2, 3, 4, 5] [[1, 2, 3, 4, 5], [1, 2, 3, 4, 5]]
- B. [1, 2, 3, 5] [[1, 2, 3, 4], [1, 2, 3, 5]]
- C. [1, 2, 3] [[1, 2, 3, 4], [1, 2, 3, 5]]
- D. [1, 2, 3, 5] [[1, 2, 3, 4, 5], [1, 2, 3, 5]]
Correct answer: B. [1, 2, 3, 5] [[1, 2, 3, 4], [1, 2, 3, 5]]
y[0] + [4] makes a new list and stores it in slot 0, detaching that slot from x. y[1] += [5] extends the object in slot 1, which is still x itself.
More Python Programming questions
- What is printed? python def f(a, b=[]): b = b + [a] f.defaults = (b,) return b print(f(1), f(2), f(3, []), f(4))
- What is printed? python it = (x x for x in range(5)) first = next(it) rest = [v for v in it if v % 2 == 0] again = sum(1 for in it)…
- What is printed? python def g(): for i in range(3): try: yield i finally: print(f"f{i}", end=" ") for v in g(): if v == 1: break…
- What is printed? python def deco(cls): orig = cls.init def init(self, a): orig(self, a) self.ok = True cls.init = init return cls @deco…
- What is printed? python import functools @functools.singledispatch def show(x): return "obj" @show.register def (x: int): return "int"…
- What is printed? python def f(): x = 0 def inc(): nonlocal x x += 1 return x return [inc() for in range(3)], x print(f())