Python Dictionary Quiz


123
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
Python Data Type    Python Dictionary Quiz    
Discuss it


Question 1 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.
Question 2
Question 2:Find the output of the following program:
D = {1 : 1, 2 : '2', '1' : 1, '2' : 3} 
D['1'] = 2
print(D[D[D[str(D[1])]]]) 

A
2
B
3
C
'2'
D
KeyError
Python Dictionary Quiz    
Discuss it


Question 2 Explanation: 
Simple key-value pair is used recursively, D[1] = 1, str(1) = ‘1’. So, D[str(D[1])] = D[‘1’] = 2, D[2] = ‘2’ and D[‘2’] = 3.
Question 3
Question 3:Find the output of the following program:
D = {1 : {'A' : {1 : "A"}, 2 : "B"}, 3 :"C", 'B' : "D", "D": 'E'} 
print(D[D[D[1][2]]], end = " ") 
print(D[D[1]["A"][2]]) 


A
C B
B
E Key Error
C
B D
D
D B
Python Dictionary Quiz    
Discuss it


Question 3 Explanation: 
Key-Value Indexing is used in the example above. D[1] = {‘A’ : {1 : “A”}, 2 : “B”}, D[1][2] = “B”, D[D[1][2]] = D[“B”] = “D” and D[“D”] = “E”. D[1] = {‘A’ : {1 : “A”}, 2 : “B”}, D[1][“A”] = {1 : “A”} and D[1][“A”][2] doesn’t exists, thus KeyError.
Question 4
Question 4:Find the output of the following program:
D = dict() 
for i in range (3): 
	for j in range(2): 
		D[i] = j 
print(D) 

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
Python Dictionary Quiz    
Discuss it


Question 4 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.
Question 5
Question 5:Find the output of the following program:
D = {1 : [1, 2, 3], 2: (4, 6, 8)} 
D[1].append(4) 
print(D[1], end = " ") 
L = [D[2]]
L.append(10) 
D[2] = tuple(L) 
print(D[2]) 

A
[1, 2, 3, 4] ((4, 6, 8), 10)
B
[1, 2, 3, 4] (4, 6, 8, 10)
C
[1, 2, 3, 4] TypeError: tuples are immutable
D
[1, 2, 3, 4] [4, 6, 8, 10]
Python Dictionary Quiz    
Discuss it


Question 5 Explanation: 
In the first part, key-value indexing is used and 4 is appended into the list. As tuples are immutable, in the second part the tuple is converted into a list, and value 10 is added finally then converted back to tuple.
Question 6
Question 6:Find the output of the following program:
a = {i: i * i for i in range(6)} 
print (a) 

A
Dictionary comprehension doesn’t exist
B
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6:36}
C
{0: 0, 1: 1, 4: 4, 9: 9, 16: 16, 25: 25}
D
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
Python Data Type    Python Dictionary Quiz    
Discuss it


Question 6 Explanation: 
The above piece of code written in curly braces generate the whole Dictionary.
Question 7
Question 7:Find the output of the following program:
a ={} 
a.fromkeys(['a', 'b', 'c', 'd'], 98) 
print (a) 

A
Syntax error
B
{‘a’:98, ‘b’:98, ‘c’:98, ‘d’:98}
C
{‘a’:None, ‘b’:None, ‘c’:None.’d’:None}
D
{ }
Python Dictionary Quiz    
Discuss it


Question 7 Explanation: 
fromkeys() create a new dictionary with keys from the list given to it as an argument and set values of the key, the default value given in it as an argument.
Question 8
Question 8:Find the output of the following program:
dict ={} 
print (all(dict)) 

A
{ }
B
False
C
True
D
An exception is thrown
Python Dictionary Quiz    
Discuss it


Question 8 Explanation: 
The all() method returns:
  • True – If all elements in an iterable are true ot iterable is empty.
  • False – If any element in an iterable is false.
Question 9
Question 9:Find the output of the following program:
a = {'geeks' : 1, 'gfg' : 2} 
b = {'geeks' : 2, 'gfg' : 1} 
print (a == b) 

A
True
B
False
C
Error
D
None
Python Dictionary Quiz    
Discuss it


Question 9 Explanation: 
If two dictionary are the same it returns true, otherwise it returns false.
Question 10
Question 10:Find the output of the following program: Which of these about a dictionary is false?
A
The values of a dictionary can be accessed using keys
B
The keys of a dictionary can be accessed using values
C
Dictionaries may or may not be ordered
D
None of the above
Python Dictionary Quiz    
Discuss it


Question 10 Explanation: 
The values of a dictionary can be accessed using keys but the keys of a dictionary can’t be accessed using values.
There are 25 questions to complete.
123
My Personal Notes arrow_drop_up


Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.