Previous Index Next

Review Questions

1. Which is the correct form of declaration of dictionary?
a) day = {1: 'm', 2: 't', 3: 'w'}
b) day = (1; 'm', 2; 't', 3; 'w')
c) day = [1: 'm', 2: 't', 3: 'w']
d) day = {1 'm', 2 't', 3 'w'}


2. Write a statement in Python to declare a dictionary whose keys are 1, 2, 3 and values are Jan, Feb and Mar respectively.


3. Write the two ways to construct an empty dictionary.


4. Identify the valid declaration of L:

L = {1:[0, 1], 2:[2, 3], 3:[4, 5]}

a) tuple
b) string
c) dictionary
d) list


5. Write the output of following code:

sales = {'Audi':45, 'BMW':32, 'Ferrari':12}
for x in sales:
    print(x)


6. Suppose a dictionary days is declared as:

days={1:"Sun", 2:"Mon", 3:"Wed"}

Write a statement in Python to change Wed to Tue.


7. Which of the following is correct declaration of empty dictionary A?
a) A = []
b) A = ()
c) A = {}
d) A = <>


8. Which of the following is a mutable object?
a) tuple
b) string
c) dictionary
d) None of these


9. Write the output of following code:

x = {1:10, 2:20, 3:30}
x[2]=25
print(x)


10. Write the output of following code:

x = {1:10, 2:20, 3:30}
x[4] = 20
print(x)


11. Write the output of following code:

d = {'x': 1, 'y': 2, 'z': 3} 
for k in d:
    print (k, '=', d[k])


12. Write the output of following code:

d = {'x': 1, 'y': 2, 'z': 3} 
for k in d.keys():
    print (k, '=', d[k])


13. Write the output of following code:

d = {'x': 1, 'y': 2, 'z': 3}
for v in d.values():
    print (v, end=" ")


14. Write the output of following code:

d = {'x': 1, 'y': 2, 'z': 3}
for v in d.items():
    print (v[0], ":", v[1])


15. Write the output of following code:

x = {1:10}
d = {2:20, 3:30, 4:40}
x.update(d)
print(x)


16. Write the output of following code:

x = {1:10, 2:20}
d = {2:25, 3:30, 4:40}
x.update(d)
print(x)


17. Write the output of following code:

d = {'x': 1, 'y': 2, 'z': 3}
a = d.setdefault('z',4)
print(a)
a = d.setdefault('w',0)
print(a)


18. Write the output of following code:

d = {'x': 1, 'y': 2, 'z': 3}
a = d.pop('y')
print(a)
print(d)


Programming Exercises

For Lab Programming Questions click here.



Previous Index Next