Python Multiple Choice Questions – Dictionary

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

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
View Answer

Answer: d
Explanation: Dictionaries are created by specifying keys and values.

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) {‘john’: 40, ‘peter’: 45}
c) 40 and 45
d) d = (40:”john”, 45:”peter”)
View Answer

Answer: b
Explanation: Dictionaries appear in the form of keys and values.
advertisement
advertisement

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
View Answer

Answer: a
Explanation: “in” operator can be used to check if the key is present in the dictionary.
advertisement

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
View Answer

Answer: b
Explanation: If d2 was initialized as d2 = d1 the answer would be true.
advertisement

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
View Answer

Answer: c
Explanation: Arithmetic > operator cannot be used with dictionaries.

6. 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”
View Answer

Answer: a
Explanation: Execute in the shell to verify.

7. 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)
View Answer

Answer: c
Explanation: Execute in the shell to verify.

8. 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()
View Answer

Answer: b
Explanation: Execute in the shell to verify.

9. 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)
View Answer

Answer: a
Explanation: The output of the code shown above is a list containing only keys of the dictionary d, in the form of a list.

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 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
View Answer

Answer: c
Explanation: Execute in the shell to verify.

More MCQs on Python Dictionary:

Sanfoundry Global Education & Learning Series – Python.

To practice all areas of Python, here is complete set of 1000+ Multiple Choice Questions and Answers.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.