CBSE Class 12 Computer Science Question 36 of 120

Review of Python Basics — Question 10

Back to all questions
10
Question

Question 10

What is the output of the following ?

d = {0: 'a', 1: 'b', 2: 'c'}
for i in d:
    print(i)

1.

0  
1  
2

2.

a  
b  
c

3.

0  
a  
1  
b  
2  
c  

4.

2  
a  
2  
b  
2  
c  
Answer
0  
1  
2

Reason — The code initializes a dictionary d with keys 0, 1, and 2, each mapped to the values 'a', 'b', and 'c' respectively. In Python, when we use a for loop with a dictionary using the in operator, like for i in d:, it iterates over the keys of the dictionary by default. During each iteration, the loop prints the current key i. Therefore, the output will be the keys of the dictionary d, which are 0, 1, and 2.