ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
d = {'a': 1, 'b': 2, 'c': 3}
try:
for k in d:
if d[k] == 2:
del d[k]
except RuntimeError:
print('error')
print(len(d))- A. error | 3
- B. error | 2
- C. no error | 2
- D. error | 1
Correct answer: B. error | 2
Deleting from a dict while iterating over it makes the next step raise RuntimeError, so the except branch runs. The deletion of b has already happened, so the length is 2.
More Python Programming questions
- What is printed? python a = [1, 2, 2, 3, 4] for x in a: if x == 2: a.remove(x) print(a)
- 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))