ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
a = [1, 2, 3]
for x in a:
a.remove(x)
print(a)- A. [2]
- B. []
- C. [1, 2, 3]
- D. [3]
Correct answer: A. [2]
Removing items during iteration shifts the list. After removing 1 the list is [2, 3] and the loop moves to index 1, which holds 3. Removing 3 leaves [2], and the loop ends there.
More Python Programming questions
- What is printed? python print(10 -2, 10 2.0, 10 // 3.0)
- What is printed? python print(all([]), any([]), all(x > 0 for x in [1, 0]))
- What is printed? python print(list(map(lambda x, y: x y, [1, 2, 3], [4, 5])))
- What is printed? python print(sum([[1], [2, 3]], []), sum([[1], [2]], [0]) if False else 'skip')
- What is printed? python print(''.join(sorted(set('hello'), key=ord)))
- What is printed? python print(len({0.0, -0.0, 1}), hash(1) == hash(1.0) == hash(True))