Python-Quizzes | Python Dictionary Quiz | Question 4

Question 4:Find the output of the following program:

filter_none

edit
close

play_arrow

link
brightness_4
code

D = dict() 
for i in range (3): 
    for j in range(2): 
        D[i] =
print(D) 

chevron_right


(A) {0: 0, 1: 0, 2: 0}
(B) {0: 1, 1: 1, 2: 1}
(C) {0: 0, 1: 0, 2: 0, 0: 1, 1: 1, 2: 1}
(D) TypeError: Immutable object


Answer: (B)

Explanation: 1st loop will give 3 values to i 0, 1 and 2. In the empty dictionary, valued are added and overwrited in j loop, for eg. D[0] = [0] becomes D[0] = 1, due to overwriting.

Quiz of this Question

My Personal Notes arrow_drop_up
Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.