What is the output of the following Python code snippet?
d = {'a': 1, 'b': 2}
try:
for k in d:
d['c'] = 3
except RuntimeError:
print('RuntimeError')
-
A
{'a': 1}
-
B
None
-
C
RuntimeError
Correct answer
-
D
KeyError
Correct answer
C. RuntimeError
6 views
Explanation
Changing a dictionary's size during iteration raises RuntimeError.
Doubts & discussion
Not sure why the answer is C? Ask here and get an explanation.
Ask a doubt about this MCQ