ProgrammingVery hardGATE CS · Product-company placement
What is printed?
What is printed?
a = [1, [2, 3]] b = a[:] c = list(a) d = [*a] a[1] += [4] a[0] += 10 print(b, c is d, d[1] is a[1])
- A. [11, [2, 3, 4]] False True
- B. [1, [2, 3, 4]] False True
- C. [1, [2, 3]] False False
- D. [1, [2, 3, 4]] True True
Correct answer: B. [1, [2, 3, 4]] False True
Slicing, list() and [*a] all make shallow copies: new outer lists sharing the inner list. a[1] += [4] extends that shared inner list in place; a[0] += 10 just rebinds a slot in a.
More Python Programming questions
- What is printed? python s = "abc" it = iter(s) print(next(it), "".join(it), list(it), [c for c in s if c in "ab" or c == "z"])
- What is printed? python def f(n): if n == 0: return [] return [n] + f(n - 1) if n % 2 else f(n - 1) + [n] print(f(5))
- What is printed? python x = 1 def f(): print(x, end=" ") def g(): x = 2 f() g() h = lambda: x x = 3 print(h())
- What is printed? python def outer(): v = "a" def inner(): return v v = "b" r1 = inner() del v try: return r1, inner() except NameError as…
- What is printed? python print(eval("[i for i in range(3)]"), [x for x in "ab"] 2, list(map(str.upper, "ab")), {"aba"} == {"a", "b"})
- What is printed? python class A: def init(self): self.v = 1 def eq(self, o): return isinstance(o, A) and self.v == o.v hash = None class…