Cs Dictionary Worksheet
Cs Dictionary Worksheet
a) 40
b) 45
c) “john”
d) “peter”
Q7. Suppose d = {“john”:40, “peter”:45}, to delete the entry for “john” what command do we
use?
a) d.delete(“john”:40)
b) d.delete(“john”)
c) del d[“john”]
d) del d(“john”:40)
Q8. Suppose d = {“john”:40, “peter”:45}. To obtain the number of entries in dictionary which
command do we use?
a) d.size()
b) len(d)
c) size(d)
d) d.len()
Q9 d = {"john":40, "peter":45}
print(list(d.keys()))
a) [“john”, “peter”]
b) [“john”:40, “peter”:45]
c) (“john”, “peter”)
d) (“john”:40, “peter”:45)
Q10. Suppose d = {“john”:40, “peter”:45}, what happens when we try to retrieve a value using
the expression d[“susan”]?
a) Since “susan” is not a value in the set, Python raises a KeyError exception
b) It is executed fine and no exception is raised, and it returns None
c) Since “susan” is not a key in the set, Python raises a KeyError exception
d) Since “susan” is not a key in the set, Python raises a syntax error
Q11. Which of the following is correct with respect to above Python code?
d={"a":3,"b":7}
A. a dictionary d is created.
B. a and b are the keys of dictionary d.
C. 3 and 7 are the values of dictionary d
D. All of the above
Q12. Which one of the following is correct?
A. In python, a dictionary can have two same keys with different values.
B. In python, a dictionary can have two same values with different keys
C. In python, a dictionary can have two same keys or same values but cannot have two
same key-value pair
D. In python, a dictionary can neither have two same keys nor two same values.
Q13. What will be the output of above Python code?
d1={"abc":5,"def":6,"ghi":7}
print(d1[0])
A. abc
B. 5
C. {"abc":5}
D. Error
Q14. What will the above Python code do?
dict={"Phy":94,"Che":70,"Bio":82,"Eng":95}
dict.update({"Che":72,"Bio":80})
A. It will create new dictionary as dict={"Che":72,"Bio":80} and old dict will be
deleted.
B. It will throw an error as dictionary cannot be updated.
C. It will simply update the dictionary as dict={"Phy":94,"Che":72,"Bio":80,"Eng":95}
D. It will not throw any error but it will not do any changes in dict
Q15. Which of the following is False regarding dictionary in Python?
dict={"Joey":1,"Rachel":2}
dict.update({"Phoebe":2})
print(dict)
A. Values of a dictionary can be string,integers or combination of both.
B. Keys of a dictionary can be string,integers or combination of both
C. The value of a dictionary can be accessed with the help of indices.
D. None of the above
Q16. Which of the following will delete key_value pair for key="tiger" in dictionary?
dic={"lion":"wild","tiger":"wild","cat":"domestic","dog":"domestic"}
A. del dic["tiger"]
B. dic["tiger"].delete()
C. delete(dic.["tiger"])
D. del(dic.["tiger"])
Q17. Which of the following will give error?
dict1={"a":1,"b":2,"c":3}
A. print(len(dict1))
B. print(dict1.get("b"))
C. dict1["a"]=5
D. None of these.
Q18. Write the output of the following codes:
d1={1:10,2:20,3:30,4:40,5:50}
del d1[3]
print(d1)
Q19. Write the output of the following codes:
d1={1:10,2:20,3:30,4:40,5:50}
d1.pop(5)
print(d1)
Q20. Write the output of the following codes:
d1={1:10,2:20,3:30,4:40,5:50}
d1.clear()
print(d1)
Q21. Rewrite the correct code after underlining the errors.
dic1 = {'age': 25, 'name': 'xyz', 'salary': 23450.5}
val = dic1['age']
if val in dic1:
print("This is member of the dictionary")
else :
print("This is not a member of the dictionary")
Q22. Find the output of the following code.
myDict = {'a' : 27, 'b' : 43, 'c' : 25, 'd' : 30}
valA =””
for i in myDict :
if i > valA :
valA = i
valB = myDict[i]
print(valA) #Line1
print(valB) #Line2
print(30 in myDict) #Line3
myLst = list(myDict.items())
myLst.sort() #Line4
print(myLst[-1]) #Line5
Q23. Find the output of the following code.
d1 = { 5 : "number", "a" : "string", (1, 2): "tuple" }
print("Dictionary contents")
for x in d1.keys():
print (x, ':' , d1[x], end = ' ')
print (d1[x] * 3)
print ( )
Q24. Find the output of the following code.
d = dict()
d['left'] = '<'
d['right'] = '>'
print('{left} and {right} or {right} and {left}')
print(d)
Q25. Find the output of the following code.
Text = “abracadabraaabbccrr”
counts = {}
ct = 0
lst = []
for word in text:
if word not in lst:
lst.append(word)
counts[word] = 0
ct = ct + 1
counts[word] = counts[word] + 1
print(counts)
print(lst)
Q26. Find the error in the following code. After correcting print the output.
box = {}
jars = {'Jam' :4}
crates = {}
box['biscuit'] = 1
box['cake'] = 3
crates['box'] = box
crates['jars'] = jars
print(len(crates[box]))
Q27. Find the output of the following code.
dct = {}
dct[1] = 1
dct ['1'] = 2
dct[1.0] = 4
sum = 0
for k in dct:
print(k, sum)
sum += dct[k]
print(sum)