Worksheet - Dict
Worksheet - Dict
WORKSHEET - DICTIONARY
Q1. Which of the following statement(s) would give an error after executing the
following code?
Stud = { “Murugan” : 100, “Mithun' : 95 } # Statement 1
print( Stud[95] ) # Statement 2
Stud [ “Murugan” ] = 99 # Statement 3
print( Stud.pop( ) ) # Statement 4
print( Stud ) # Statement 5
a) Statement 2 b) Statement 3 c) Statement 4 d) Statements 2 and 4
Ans d) Statements 2 and 4
Q2. Given the following dictionaries
dict_student = {"rno" : "53", "name" : ‘Rajveer Singh’}
dict_marks = {"Accts" : 87, "English" : 65}
Which statement will merge the contents of both dictionaries?
(A) dict_student + dict_marks (B) dict_student.add(dict_marks)
(C) dict_student.merge(dict_marks) (D) dict_student.update(dict_marks)
Ans (D) dict_student.update(dict_marks)
Q3.
Which of the following statement(s) would give an error after executing the
following code?
D={'rno':32,'name':'Ms.Archana','subject':['hindi','english','cs'],'marks':
(85,75,89)} #S1 print(D) #S2
D['subject'][2]='IP' #S3
D['marks'][2]=80 #S4
print(D) #S5
(A) S1 (B) S3 (C) S4 (D) S3 and S4
Ans
(C) S4
Q4. What will be the output of the following python dictionary operation?
data = {'A':2000, 'B':2500, 'C':3000, 'A':4000}
print(data)
a) {'A':2000, 'B':2500, 'C':3000, 'A':4000}
b) {'A':2000, 'B':2500, 'C':3000}
c) {'A':4000, 'B':2500, 'C':3000}
d) It will generate an error.
Ans c) {'A':4000, 'B':2500, 'C':3000}
Q5. What will the following code do?
dict={"Exam":"AISSCE", "Year":2022}
dict.update({"Year”:2023} )
a) It will create new dictionary dict={” Year”:2023} and the old dictionary
will be deleted.
b) It will throw an error and dictionary cannot updated
c) It will make the content of dictionary as dict={"Exam":"AISSCE",
"Year":2023}
d) It will throw an error and dictionary and do nothing
Ans It will make the content of dictionary as
dict={"Exam":"AISSCE", "Year":2023}
Q6. State True or False.
A dictionary key must be of a data type that is mutable.
Ans False
Q7. Which of the following statements would give an error after executing the
following code?
d={'A':1,'B':2,'C':3,'D':4} #Statement 1
sum_keys=0 #Statement 2
for val in d.keys(): #Statement 3
sum_keys=sum_keys+val #Statement 4
print(sum_keys)
a) Statement 1 b) Statement 2
c) Statement 3 d) Statement 4
Ans d) Statement 4
Q8. What will be output of the following code:
d1={1:2,3:4,5:6}
d2=d1.popitem()
print(d2)
a){1:2} b){5:6} c)(1,2) d) (5,6)
Ans d) (5,6)
Q9. Choose the most correct statement among the following
(a) A dictionary is a sequential set of elements
(b) A dictionary is a set of key-value pairs
(c) A dictionary is a sequential collection of elements key-value pairs
(d) A dictionary is a non-sequential collection of elements
Ans (a) A dictionary is a set of key-value pairs
Q10. Given the following list
place=[ (“State” , ”Sikkim”), (“Population”, ”8Lakh”) ]
which of the following method will turn the above list place into something
like { “State” : “Sikkim” , “Population” : “8 Lakh”}
when applied over it:
a)dictionary() b) to_dict() c) dict() d) Items( )
Ans (b) dict()
Q11. Given the following dictionary
Emp1={ "salary":10000,"dept":"sales","age":24,"name":"john"}
Emp1.keys() can give the output as
a. ("salary","dept","age","name")
b. ["salary","dept","age","name"]
c. [10000,"sales",24,"john"]
d. {"salary","dept","age","name"}
Ans b. ["salary","dept","age","name"]
Q12. What will be output of the following code:
d1={1:2,3:4,5:6}
d2=d1.get(3,5)
print(d2)
a) 3 b) 4 c) 5 d) Error
Ans a) 4
Q13. Given the following dictionaries
dict_fruit={"Kiwi":"Brown", "Cherry":"Red"}
dict_vegetable={"Tomato":"Red", "Brinjal":"Purple"}
Which statement will merge the contents of both dictionaries?
(a) dict_fruit.update(dict_vegetable) (b) dict_fruit + dict_vegetable
(c) dict_fruit.add(dict_vegetable) (d) dict_fruit.merge(dict_vegetable)
Ans (a) dict_fruit.update(dict_vegetable)
Q14. Identify the output of the following python code:
D={1:”one”,2:”two”, 3:”three”}
L=[]
for k,v in D.items():
if ‘o’ in v:
L.append(k)
print(L)
(a) [1,2] (b) [1,3] (c)[2,3] (d)[3,1]
Ans (a)[1,2]
Q15. Write the output of the following codes:
(a) >>> dl={1:10,2:20,3:30, 4:40,5:50}
>>> dl.items ().
>>> dl.keys ()
>>> dl.values()
Ans. dict_items[(1, 10),(2, 20),(3, 30),(4, 40), (5, 50)]
dict_keys[1,2,3,4,5] dict_values[10,20,30,40,50]
Q16. (b) d1={1:10,2:20,3:30,4:40}
d2={5:50,6:60,7:70}
d1.update (d2) print (d1)
Ans {1: 10, 2:20,3:30,4:40,5:50,6:50, 7: 70}
Q17. d1={1:10,2:20,3:30,4:40,5:50,6:60,7:70}
del d1 [3] print (d1)
Ans {1: 10, 2:20,4:40,5:50,6:60,7:70}
Q18. d1={1:10,2:20,3:30,4:40,5:50,6:60,7:70}
d1.pop (5) print (d1)
Ans { 5: 5 0 }
{1: 10, 2:20,3:30,4:40,6:60,7:70}
Q19. d1={1:10,2:20,3:30,4:40,5:50,6:60,7:70}
len(d1) d1.get (6, 60) d1[6]=65 d1[8]=35
print (d1) d1.clear() print (d1)
Ans 7 60
{1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 65, 7: 70, 8: 35}
{}
Q20.
Find errors and rewrite the same after correcting the following code: (a)
d1={1:10,2.5:20,3:30,4:40,5:50,6:60,7,70}
(b) d1(9)=90
(c) del d1(2)
(d) pop d1[4]
(e) d1. item()
(f) d1.key()
(g) d1.value()
(h) d1.gets (4,80)
(i) d1.len ()
(j) d1.clears ()
Q21. Suppose
d1={1: 'one', 2: 'two, 3: three', 4: four'} d2={5: five', 6: six}
Write the output of the following code:
(a) >>> d1. items ()
>>> d1.keys ()
>>> d1.values()
>>> d1.update (d2)
>>> len(d1)
(b) >>> del d1 [3]
>>> print (d1)
>>> d1.pop (4)
>>> print (dl)
>>> d1[8]='eight"
>>> print(d1)
>>> d1.clear()
>>> print (d1)
Q22.
Q23.