Computer Science - Informatics Practices Class XI/XII Python Dictionary Solved Test Part 2
Q1. Name a function which is used to create an empty dictionary.
Q2. What empty curly braces({}) shows in python?
Q3. Write the output of the following:
a = {}
a[1]='A'
a[2]='B'
a[3]='C'
print(a)
Q4. Write the output of the following:
a = {'Class':'VI','Sec':'B','Rollno' : 1}
print(a)
print(a['Class'])
print(a['Sec'])
print(a['roll'])
if any of the above print statement return an error
than identify the error type.
Q5. a = {'Class':'VI','Sec':'B','Rollno' : 1}
for i in a:
print(i)
Q6. a = {'Class':'VI','Sec':'B','Rollno' : 1}
for i in a:
print(a)
Q7. a = {'Class':'VI','Sec':'B','Rollno' : 1}
for i in a:
print(a[i])
Q8. a = {'Class':'VI','Sec':'B','Rollno' : 1}
for i in a:
print(a[i])
print(a[i])
Q9. What type of error shows by the following program:
a = {'Class':'VI','Sec':'B','Rollno' : 1}
for i in len(a):
print(a[i])
Q10. What do you mean by traversing a dictionary?
Solution:
Q1. Name a function which is used to create an empty dictionary.
Ans. dict()
Q2. What empty curly braces({}) shows in python?
Ans. Curly braces shows empty dictionary in python.
Q3. Write the output of the following:
a = {}
a[1]='A'
a[2]='B'
a[3]='C'
print(a)
Ans. {1 : 'A', 2 : 'B', 3 : 'C'}
Q4. Write the output of the following:
a = {'Class':'VI','Sec':'B','Rollno' : 1}
print(a)
print(a['Class'])
print(a['Sec'])
print(a['roll'])
if any of the above print statement return an error
than identify the error type.
Ans. {'Class': 'VI', 'Sec': 'B', 'Rollno': 1}
VI
B
Last print statement will show "key error" as 'roll' key
is not there in dictionary
Q5. a = {'Class':'VI','Sec':'B','Rollno' : 1}
for i in a:
print(i)
Ans. Class
Sec
Rollno
Q6. a = {'Class':'VI','Sec':'B','Rollno' : 1}
for i in a:
print(a)
Ans. {'Class':'VI','Sec':'B','Rollno' : 1}
{'Class':'VI','Sec':'B','Rollno' : 1}
{'Class':'VI','Sec':'B','Rollno' : 1}
Q7. a = {'Class':'VI','Sec':'B','Rollno' : 1}
for i in a:
print(a[i])
Ans. VI
B
1
Q8. a = {'Class':'VI','Sec':'B','Rollno' : 1}
for i in a:
print(a[i])
print(a[i])
Ans. VI
B
1
1
Q9. What type of error shows by the following program:
a = {'Class':'VI','Sec':'B','Rollno' : 1}
for i in len(a):
print(a[i])
Ans. TypeError: 'int' object is not iterable
Q10. What do you mean by traversing a dictionary?
Ans. Traversing a dictionary means accessing each element of
dictionary. This can be done by using for loop.
No comments:
Post a Comment