Open In App

Python-Quizzes | Python Dictionary Quiz | Question 1

Question 1:Find the output of the following program:




D = dict() 
for x in enumerate(range(2)): 
    D[x[0]] = x[1
    D[x[1]+7] = x[0
print(D) 

(A) {0: 1, 7: 0, 1: 1, 8: 0}
(B) {1: 1, 7: 2, 0: 1, 8: 1}
(C) {0: 0, 7: 0, 1: 1, 8: 1}
(D) KeyError

Answer: (C)
Explanation: enumerate() will return a tuple, the loop will have x = (0, 0), (1, 1). Thus D[0] = 0, D[1] = 1, D[0 + 7] = D[7] = 0 and D[1 + 7] = D[8] = 1.
Note: Dictionary is unordered, so the sequence of the key-value pair may differ in each output.
Quiz of this Question

Article Tags :