Python Dictionary Quiz

  • Last Updated : 28 Sep, 2023

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) 

Cross
{0: 1, 7: 0, 1: 1, 8: 0}
Cross
{1: 1, 7: 2, 0: 1, 8: 1}
Tick
{0: 0, 7: 0, 1: 1, 8: 1}
Cross
KeyError


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])]]]) 

Cross
2
Tick
3
Cross
'2'
Cross
KeyError


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]]) 


Cross
C B
Tick
E Key Error
Cross
B D
Cross
D B


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) 

Cross
{0: 0, 1: 0, 2: 0}
Tick
{0: 1, 1: 1, 2: 1}
Cross
{0: 0, 1: 0, 2: 0, 0: 1, 1: 1, 2: 1}
Cross
TypeError: Immutable object


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]) 

Tick
[1, 2, 3, 4] ((4, 6, 8), 10)
Cross
[1, 2, 3, 4] (4, 6, 8, 10)
Cross
[1, 2, 3, 4] TypeError: tuples are immutable
Cross
[1, 2, 3, 4] [4, 6, 8, 10]


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) 

Cross
Dictionary comprehension doesn’t exist
Cross
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6:36}
Cross
{0: 0, 1: 1, 4: 4, 9: 9, 16: 16, 25: 25}
Tick
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25}


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) 

Cross
Syntax error
Cross
{‘a’:98, ‘b’:98, ‘c’:98, ‘d’:98}
Cross
{‘a’:None, ‘b’:None, ‘c’:None.’d’:None}
Tick
{ }


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)) 

Cross
{ }
Cross
False
Tick
True
Cross
An exception is thrown


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) 

Cross
True
Tick
False
Cross
Error
Cross
None


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?
Cross
The values of a dictionary can be accessed using keys
Tick
The keys of a dictionary can be accessed using values
Cross
Dictionaries may or may not be ordered
Cross
None of the above


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.