Python Multiple Choice Questions – Dictionary

Python Multiple Choice Questions & Answers (MCQs) focuses on “Dictionary”.

Q 1. Which of the following statements create a dictionary?
A. d = {}
B. d = {“john”:40, “peter”:45}
C. d = {40:”john”, 45:”peter”}
D. All of the mentioned

Show Answer Answer:-D. All of the mentioned
Explanation Dictionaries are created by specifying keys and values.

Q 2. What will be the output of the following Python code snippet?

d = {“john”:40, “peter”:45}
print(d)
A. “john”, 40, “peter”, 45
B. 40 and 45
C. {‘john’: 40, ‘peter’: 45}
D. d = (40:”john”, 45:”peter”)

Show Answer Answer:-C. {‘john’: 40, ‘peter’: 45}
Explanation Dictionaries appear in the form of keys and values.


Q 3. What will be the output of the following Python code snippet?

>>> d = {“john”:40, “peter”:45}
>>> “john” in d
A. True
B. False
C. None
D. Error

Show Answer Answer:-A. True
Explanation “in” operator can be used to check if the key is present in the dictionary.


Q 4. What will be the output of the following Python code snippet?

d1 = {“john”:40, “peter”:45}
d2 = {“john”:466, “peter”:45}
d1 == d2
A. True
B. False
C. None
D. Error

Show Answer Answer:-B. False
Explanation If d2 was initialized as d2 = d1 the answer would be true.


Q 5. What will be the output of the following Python code snippet?

d1 = {“john”:40, “peter”:45}
d2 = {“john”:466, “peter”:45}
d1 > d2
A. True
B. False
C. Error
D. None

Show Answer Answer:-C. Error
Explanation Arithmetic > operator cannot be used with dictionaries.


Q 6. What will be the output of the following Python code snippet?

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)

Show Answer Answer:-A. [“john”, “peter”]
Explanation The output of the code shown above is a list containing only keys of the dictionary d, in the form of a list.


Q 7. What will be the output of the following Python code snippet?

d = {“john”:40, “peter”:45}
d[“john”]
A. 40
B. 45
C. “john”
D. “peter”

Show Answer Answer:-A. 40


Q 8. 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”:40)
D. del d[“john”]

Show Answer Answer:-D. del d[“john”]


Q 9. 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()

Show Answer Answer:-B. len(d)


Q 10. 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 key 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 value in the set, Python raises a KeyError exception
D. Since “susan” is not a key in the set, Python raises a syntax error

Show Answer Answer:-A. Since “susan” is not a key in the set, Python raises a KeyError exception

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You cannot copy content of this page