ProgrammingHardGATE CS · Product-company placement
What is printed?
What is printed?
a = [1, 2, 3]
for i, v in enumerate(a):
a[i] = v * 2
if i == 0:
a.append(9)
if len(a) > 4:
break
print(a)- A. [2, 4, 6, 18]
- B. [2, 4, 6, 9]
- C. [2, 4, 6, 18, 9]
- D. [2, 2, 3, 9]
Correct answer: A. [2, 4, 6, 18]
Iterating a list uses a live index, so the appended 9 is visited too and doubled to 18; the list length never exceeds 4, so the loop finishes normally.
More Python Programming questions
- What is printed? python x = 0 def f(): global x x += 1 return x d = {f(): f(), f(): f()} L = [f(), f()] L[f() - 7] = f() print(d, L)
- What is printed? python def f(): return f.n f.n = 5 g = f f = lambda: 0 try: print(g()) except AttributeError: print("AE", g.n)
- What is printed? python words = ["kiwi", "fig", "apple", "date"] print(sorted(words, key=len), max(words, key=len), min(words))
- What is printed? python import copy class Node: def init(self): self.kids = [] a = Node() a.kids.append(a) b = copy.deepcopy(a)…
- What is printed? python 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])
- 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"])