ProgrammingVery hardAmazon OA · Flipkart coding round
What is printed?
What is printed?
a = [1, 2, 2, 3, 4]
for x in a:
if x == 2:
a.remove(x)
print(a)- A. [1, 2, 2, 4]
- B. [1, 2, 3]
- C. [1, 2, 3, 4]
- D. [1, 2, 2, 3, 4]
Correct answer: C. [1, 2, 3, 4]
Removing an item shifts the rest left, so the iterator skips the second 2 that moved into the current position. One 2 remains in the result.
More Python Programming questions
- What is printed? python it = iter(range(7)) print(list(zip(it, it)), list(it))
- What is printed? python print(sum([0.1] 10) == 1.0, sum([0.1] 10), round(sum([0.1] 10), 2))
- What is printed? python x = (5) y = (5,) print(type(x).name, type(y).name, len(y), ((5),) == (5,))
- What is printed? python a = {1, 2, 3} b = {3, 4} print(a ^ b, a.issubset({1, 2, 3, 4}), sorted(a - b, reverse=True), b > {3})
- What is printed? python import math print(math.floor(-2.5), math.trunc(-2.5), -7.5 // 2, divmod(7.5, 2))
- What is printed? python def dfs(g, s): seen, out, st = set(), [], [s] while st: u = st.pop() if u in seen: continue seen.add(u)…