ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
x = [1, 2, 3]
for i in x:
x.remove(i)
print(x)- A. []
- B. [2]
- C. [3]
- D. [1, 3]
Correct answer: B. [2]
Removing items while looping over the same list moves the iterator's index. 1 is removed at index 0, so 2 slides into index 0 and is skipped. The loop then removes 3 at index 1 and stops, leaving [2].
More Python Programming questions
- What is printed? python fs = [lambda: i for i in range(3)] print([f() for f in fs])
- What is printed? python def f(x, lst=[]): lst.append(x) return lst a = f(1) b = f(2, []) c = f(3) print(a, b, c)
- What is printed? python def f(): try: return 1 finally: return 2 print(f())
- What is printed? python print(1 < 2 > 1.5, 3 > 2 == 2, (3 > 2) == 2)
- What is printed? python a = [1, 2, 3] b = a a = a + [4] b += [5] print(a, b)
- What is printed? python print(-2 2, (-2) 2, 2 -1)