![]() |
VOOZH | about |
Prerequisite: Dictionary
1) What is the output of the following program?
a) Both b and d
b) Runtime error
c) GFG
facebook
d) facebook
GFG
Ans. (a)
Output:
GFG
Explanation: The statement: del dictionary; removes the entire dictionary, so iterating over a deleted dictionary throws a runtime error as follows:
Traceback (most recent call last):
File "cbeac2f0e35485f19ae7c07f6b416e84.py", line 12, in
for key, values in dictionary.items():
NameError: name 'dictionary' is not defined
2) What is the output of the following program?
a) Compilation error
b) Runtime error
c) ('Google', 1)
('Facebook', 2)
('Youtube', 3)
('Microsoft', 2)
('GFG', 1)
d) None of these
Ans. (c)
Explanation: dictionary1.update(dictionary2) is used to update the entries of dictionary1 with entries of dictionary2. If there are same keys in two dictionaries, then the value in second dictionary is used.
3) What is the output of the following program?
a) Compilation error due to duplicate keys
b) Runtime time error due to duplicate keys
c) 3
d) 1
Ans. (c)
Explanation: Here, GFG is the duplicate key. Duplicate keys are not allowed in python. If there are same keys in a dictionary, then the value assigned mostly recently is assigned to that key.
4) What is the output of the following program?
a) Compilation error
b) {'key1': 44, 'key2': 566}[1, 2, 3, 4]
c) Runtime error
d) None of the above
Ans. (b)
Explanation: A dictionary can hold any value such as an integer, string, list, or even another dictionary holding key-value pairs.
5) What is the output of the following Python program?
a) Google 3 GFG 1 Facebook 2
b) Facebook 2 GFG 1 Google 3
c) Facebook 2 Google 3 GFG 1
d) Any of the above
e) None of the above
Ans. (e)
Explanation for (e): Since python 3.7 dictionaries are ordered in the insertion order.