ProgrammingEasyInfosys · Wipro
What does this Python code print? a = [1, 2, 3] b = a a += [4] a = a + [5] print(b, a)
- A. [1, 2, 3, 4] [1, 2, 3, 4, 5]
- B. [1, 2, 3] [1, 2, 3, 4, 5]
- C. [1, 2, 3, 4, 5] [1, 2, 3, 4, 5]
- D. [1, 2, 3, 4] [1, 2, 3, 5]
Correct answer: A. [1, 2, 3, 4] [1, 2, 3, 4, 5]
a += [4] extends the shared list in place, so b sees it. a = a + [5] builds a new list and rebinds a, leaving b unchanged.
More Python Programming questions
- What does this Python code print? it = iter([1, 2, 3, 4, 5]) print(any(x > 2 for x in it), next(it))
- What does this Python code print? def f(x, acc=[]): acc.append(x) return len(acc) print(f(1), f(2), f(3, []), f(4))
- What is the output of this Python code? t = ([1], 5) try: t[0] += [2] except TypeError: print("err", end=" ") print(t)
- What does this Python code print? class A: items = [] n = 0 def add(self, x): self.items += [x] self.n += 1 a, b = A(), A() a.add(1)…
- What is printed? a = [1, 2, 3]; b = [2, 3, 4] g = (x for x in a if x in b) a = [9, 2, 3]; b = [1, 9, 3] print(list(g))
- What does this Python code print? def deco(tag): def wrap(fn): def inner(x): return "<" + tag + ">" + fn(x) + "</" + tag + ">" return…