Chapter Dictionary Worksheet
Chapter Dictionary Worksheet
CHAPTER : DICTIONARY
WORKSHEET
1. What is the syntax to create an empty dictionary in Python?
a) dict = {} b) dict = [] c) dict = () d) dict = None
2. Which method is used to add a new key-value pair to a dictionary?
a) append() b) update() c) insert() d) add()
3. How do you access the value of a key in a dictionary?
a) dict[key] b) dict.key c) dict.get(key) d) a & c
4. What happens if you try to access a key that doesn't exist in a dictionary?
a) It returns None b) It raises a KeyError
c) It returns an empty string d) It returns 0
5. Which method is used to remove a key-value pair from a dictionary?
a) pop() b) remove() c) delete() d) clear()
6. What is the output of dict.keys()?
a) A list of keys b) A list of values
c) A list of key-value pairs d) A dictionary
7. Which method is used to add new key value pair if not exists ?
a) get() b) setdefault() c) add() d) insert()
8. How do you merge two dictionaries?
a) dict1 + dict2 b) dict1.update(dict2)
c) dict1.merge(dict2) d) dict1.extend(dict2)
9. What is the output of dict.clear()?
a) An empty dictionary
b) A dictionary with one key-value pair
c) A dictionary with all keys removed
d) A dictionary with all values removed
10.What will be the output of the following Python code snippet?
d1={“amit”:40, “jatin”:45}
d2={“amit”:466, “jatin”:45}
d1>d2
a) True b) False c) error d) None
11. Keys must be _____ in dictionary.(Mutable/Immutable)
12. Write output of following statement.
D={“Amit”:40, “Sumit”:35, “Naina”:30}
print(D[“Amit”]+D[“Sumit”])
13. Write output :
d = {"a" : 1 , "b" : 2}
d["c"] = 3
d["b"] = 4
del d["a"]
print(d)
14. Consider the following dictionary stateCapital:
stateCapital = {"Assam":"Guwahati",
"Bihar":"Patna","Maharashtra":"Mumbai",
"Rajasthan":"Jaipur"}
Find the output of the following statements:
a) print(stateCapital.get("Bihar"))
b) print(stateCapital.keys())
c) print(stateCapital.values())
d) print(stateCapital.items())
e) print(len(stateCapital))
f) print("Maharashtra" in stateCapital)
g) print(stateCapital.get("Assam"))
h) del stateCapital["Assam"]
i) print(stateCapital)
15. Find the incorrect statement/s out of the following:
a) >>> dict1= {'A': 'Apple', 'B':'Boy', 'C':'Cat', 'D':'Dog'}
a) >>>print(len(d)) b) >>>print (dict1[‘a’] )
c) >>> print (dict1[‘B’]) d) >>> print (dict1[‘Cat’] )
e) >>> print(dict1[‘E’] ) f) >>>print(dict1.pop(‘A’))
16. Consider the following dictionary:
states=['D':'Delhi', 'K':'Kerala', 'T':'Tamil Nadu', 'B':'Bihar']
Write Python statements for the following. Kindly state the reason for the operations which is not
possible.
a) To insert (‘R’:“Red Fort”) and (‘J’:“Jantar Mantar”) in the dictionary states
b) To replace the value of key ‘K’ by ‘Karnataka’.
c) To remove the element with key ‘B’.
d) To delete the dictionary, states.
e) To check whether the element with key ‘D’ is present in the dictionary or not.