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
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
17 Sep, 2020
Like Article
Save Article