Python Dictionaries MCQs – Multiple Choice Questions for Practice and Learning
Test and improve your understanding of Python dictionaries with these multiple choice questions. Practice key concepts like key-value pairs, dictionary methods, and data manipulation with beginner-friendly MCQs and detailed answers. Ideal for students and Python learners.
Topic: dictionaries
📝 Multiple Choice Questions
🟢 Beginner
Q1. Which of the following correctly creates an empty dictionary in Python?
-
🟢 A. my_dict = []
-
🔵 B. my_dict = {}
-
🟠 C. my_dict = set()
-
🔴 D. my_dict = dict[]
Answer
my_dict = {}
Using {} or dict() creates an empty dictionary. [] creates an empty list.
Q2. Which method would you use to get all keys from a dictionary?
-
🟢 A. get()
-
🔵 B. keys()
-
🟠 C. values()
-
🔴 D. items()
Answer
keys()
The keys() method returns a view of all dictionary keys.
Q3. What will `my_dict.get('key')` return if 'key' does not exist in the dictionary?
my_dict = {'a': 1, 'b': 2}
print(my_dict.get('c'))
-
🟢 A. 0
-
🔵 B. KeyError
-
🟠 C. None
-
🔴 D. 'c'
Answer
None
The get() method returns None if the key is not found, instead of raising KeyError.
Q4. Which of these types can NOT be used as a dictionary key?
-
🟢 A. int
-
🔵 B. str
-
🟠 C. tuple
-
🔴 D. list
Answer
list
Dictionary keys must be immutable and hashable; lists are mutable and unhashable.
Q5. How do you remove a key-value pair from a dictionary by key?
-
🟢 A. remove()
-
🔵 B. pop()
-
🟠 C. discard()
-
🔴 D. delete()
Answer
pop()
The pop() method removes the specified key and returns its value.
Q6. Which statement checks if the key 'age' exists in a dictionary called my_dict?
-
🟢 A. 'age' in my_dict
-
🔵 B. my_dict.has('age')
-
🟠 C. 'age' exists my_dict
-
🔴 D. 'age' == my_dict
Answer
'age' in my_dict
Use the 'in' operator to check if a key exists in a dictionary.
Q7. What does the `items()` method return?
-
🟢 A. A list of keys
-
🔵 B. A list of values
-
🟠 C. A view object of key-value pairs
-
🔴 D. The length of the dictionary
Answer
A view object of key-value pairs
items() returns a view object of (key, value) pairs.
Q8. Which of the following is a correct way to create a set in Python?
-
🟢 A. myset = {}
-
🔵 B. myset = []
-
🟠 C. myset = set()
-
🔴 D. myset = dict()
Answer
myset = set()
Using set() creates an empty set. Using {} creates an empty dictionary.
Q9. What is the output of the following code?
myset = {1, 2, 2, 3}
print(len(myset))
-
🟢 A. 2
-
🔵 B. 3
-
🟠 C. 4
-
🔴 D. 1
Answer
3
Sets store only unique elements, so duplicate 2 is ignored.
Q10. Which method is used to add an item to a set?
-
🟢 A. append()
-
🔵 B. add()
-
🟠 C. insert()
-
🔴 D. extend()
Answer
add()
The add() method adds a single element to a set.
Q11. What will be the output?
a = {1, 2, 3}
a.add(2)
print(a)
-
🟢 A. {1, 2, 3, 2}
-
🔵 B. {1, 2, 3}
-
🟠 C. {1, 3}
-
🔴 D. {1, 2}
Answer
{1, 2, 3}
Adding an existing element has no effect because sets contain only unique items.
Q12. Which method removes an element from a set without raising an error if the element does not exist?
-
🟢 A. remove()
-
🔵 B. discard()
-
🟠 C. pop()
-
🔴 D. clear()
Answer
discard()
The discard() method does not raise an error if the item is not present.
Q13. Which of these is true about sets?
-
🟢 A. Sets are ordered collections
-
🔵 B. Sets can contain duplicate elements
-
🟠 C. Sets are mutable
-
🔴 D. Sets can contain lists
Answer
Sets are mutable
You can add and remove elements, but elements must be immutable.
Q14. Which operator is used for set union?
-
🟢 A. +
-
🔵 B. &
-
🟠 C. |
-
🔴 D. -
Answer
|
The '|' operator returns the union of two sets.
Q15. What is the output?
a = {1, 2}
b = {2, 3}
print(a | b)
-
🟢 A. {1, 2, 3}
-
🔵 B. {2}
-
🟠 C. {1, 3}
-
🔴 D. {1, 2}
Answer
{1, 2, 3}
Union combines elements from both sets, removing duplicates.
Q16. Which method removes all items from a set?
-
🟢 A. delete()
-
🔵 B. remove()
-
🟠 C. clear()
-
🔴 D. discard()
Answer
clear()
clear() empties the set completely.
Q17. Which method can be used to get a copy of a set?
-
🟢 A. copy()
-
🔵 B. clone()
-
🟠 C. duplicate()
-
🔴 D. replicate()
Answer
copy()
copy() returns a shallow copy of the set.
Q18. Which set operation returns elements common to both sets?
-
🟢 A. Union
-
🔵 B. Difference
-
🟠 C. Intersection
-
🔴 D. Symmetric Difference
Answer
Intersection
Intersection returns common elements.
Q19. What is the output?
a = {1, 2, 3}
b = {3, 4, 5}
print(a & b)
-
🟢 A. {1, 2, 3, 4, 5}
-
🔵 B. {3}
-
🟠 C. {1, 2}
-
🔴 D. {}
Answer
{3}
The '&' operator returns the intersection of the sets.
Q20. Which operation returns elements that are in either set but not both?
-
🟢 A. Union
-
🔵 B. Intersection
-
🟠 C. Difference
-
🔴 D. Symmetric Difference
Answer
Symmetric Difference
Symmetric difference excludes common elements.
Q21. What does the pop() method do on a set?
-
🟢 A. Removes and returns an arbitrary element
-
🔵 B. Removes a specific element
-
🟠 C. Removes all elements
-
🔴 D. Adds an element
Answer
Removes and returns an arbitrary element
Sets are unordered; pop() removes any item.
Q22. What is the output?
myset = {1, 2, 3}
myset.remove(4)
-
🟢 A. Removes nothing
-
🔵 B. Raises KeyError
-
🟠 C. Removes 4
-
🔴 D. Adds 4
Answer
Raises KeyError
remove() raises an error if the element is not found.
Q23. What is the result of set([1, 2, 2, 3])?
-
🟢 A. [1, 2, 2, 3]
-
🔵 B. {1, 2, 2, 3}
-
🟠 C. {1, 2, 3}
-
🔴 D. (1, 2, 3)
Answer
{1, 2, 3}
The set() constructor removes duplicates.
Q24. Which set method returns the difference of two sets?
-
🟢 A. intersection()
-
🔵 B. difference()
-
🟠 C. union()
-
🔴 D. symmetric_difference()
Answer
difference()
difference() returns elements only in the first set.
Q25. What is the output?
a = {1, 2, 3}
b = {2, 3, 4}
print(a - b)
-
🟢 A. {1, 4}
-
🔵 B. {2, 3}
-
🟠 C. {1}
-
🔴 D. {}
Answer
{1}
Difference returns items in a but not in b.
Q26. What is the data type of { } in Python?
-
🟢 A. Set
-
🔵 B. List
-
🟠 C. Tuple
-
🔴 D. Dictionary
Answer
Dictionary
Empty {} is a dictionary. Use set() for an empty set.
Q27. Which is a valid way to check if an item exists in a set?
fruits = {"apple", "banana"}
print("apple" ____ fruits)
-
🟢 A. has
-
🔵 B. in
-
🟠 C. exists
-
🔴 D. inside
Answer
in
Use 'in' to check membership in sets.
🟡 Intermediate
🔴 Advanced
🧠 Practice & Progress
Explore More Topics
📘 Learn Python
Tutorials, Roadmaps, Bootcamps & Visualization Projects
Python Fundamentals
Fundamentals more ...
🧠 Python Advanced
More...
🧠 Modules