VOOZH about

URL: https://www.geeksforgeeks.org/python/output-python-program-set-14-dictionary/

⇱ Output of python program | Set 14 (Dictionary) - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Output of python program | Set 14 (Dictionary)

Last Updated : 23 Jul, 2025

Prerequisite: Dictionary 
Note: Output of all these programs is tested on Python3
1) What is the output of the following program? 
 

a) KeyError 
b) {0: 1, 7: 0, 1: 1, 8: 0} 
c) {0: 0, 7: 0, 1: 1, 8: 1} 
d) {1: 1, 7: 2, 0: 1, 8: 1} 
Ans. (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.
2) What is the output of the following program? 
 

a) 2 
b) 3 
c) '2' 
d) KeyError 
Ans. (b) 
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.
3) What is the output of the following program? 
 

a) D C 
b) E B 
c) D B 
d) E KeyError 
Ans. (d) 
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.
4) What is the output of the following program? 
 

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 
Ans. (b) 
Explanation: 1st loop will give 3 values to i 0, 1 and 2. In the empty dictionary, values are added and overwritten in j loop, for eg. D[0] = [0] becomes D[0] = 1, due to overwriting.
5) Which of the options below could possibly be the output of the following program? 
 

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) 
Ans. (b) 
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 to the new list 'L' then converted back to a tuple.
 
 

Comment
Article Tags:
Article Tags: