Python Dictionaries Quiz
Welcome to the Python Dictionaries Quiz!
Dictionaries in Python are powerful data structures that store key-value pairs. They allow for fast lookups, flexible data storage, and easy modifications. This quiz will test your understanding of how to create, manipulate, and access dictionaries in Python.
Each question is designed to strengthen your knowledge of dictionary methods, operations, and best practices. Let’s see how well you know Python dictionaries!
Table of Contents:
- Python quizzes for beginners series
Dictionaries Quiz
Quiz Questions
1. How do you create an empty dictionary in Python?
a) dict = {}
b) dict = []
c) dict = ()
d) dict = ""
Answer
a) dict = {}
2. What will dict1 = {"name": "Alice", "age": 25}
return for dict1["name"]
?
a) "Alice"
b) "age"
c) 25
d) Error
Answer
a) "Alice"
3. Which method is used to get all the keys in a dictionary?
a) keys()
b) values()
c) items()
d) get_keys()
Answer
a) keys()
4. What does the following code return?
my_dict = {"a": 1, "b": 2, "c": 3}
print(my_dict.get("d", 10))
a) None
b) 10
c) d
d) Error
Answer
b) 10
5. How do you check if a key exists in a dictionary?
a) "key" in dict
b) dict.has("key")
c) dict.contains("key")
d) dict.exists("key")
Answer
a) "key" in dict
6. What does dict1.items()
return?
a) A list of dictionary values
b) A list of dictionary keys
c) A list of key-value pairs as tuples
d) A string of dictionary contents
Answer
c) List of key-value pairs as tuples
7. How can you remove an item from a dictionary using a key?
a) dict.pop(key)
b) dict.remove(key)
c) dict.delete(key)
d) del dict.key
Answer
a) dict.pop(key)
8. What happens if you try to access a non-existent key using dict[key]
?
a) It returns None
b) It raises a KeyError
c) It adds the key with a default value
d) It creates a new dictionary
Answer
b) Raises a KeyError
9. Which method removes all key-value pairs from a dictionary?
a) clear()
b) popitem()
c) remove()
d) del()
Answer
a) clear()
10. Can dictionary keys be mutable data types (like lists)?
a) Yes
b) No
Answer
b) No
11. How do you merge two dictionaries in Python 3.9+?
a) dict1.update(dict2)
b) dict3 = dict1 + dict2
c) dict3 = dict1 | dict2
d) dict1.append(dict2)
Answer
c) dict3 = dict1 | dict2
12. What does dict1.values()
return?
a) List of dictionary values
b) List of dictionary keys
c) List of key-value pairs
d) Error
Answer
a) List of dictionary values
13. What will dict1.popitem()
do?
a) Remove a random key-value pair
b) Remove the last inserted key-value pair
c) Remove the first inserted key-value pair
d) Raise an error
Answer
b) Remove the last inserted key-value pair
14. What is the output of this code?
my_dict = {1: "a", 2: "b", 3: "c"}
print(my_dict[1])
a) "a"
b) 1
c) None
d) Error
Answer
a) "a"
15. What happens if we try to assign a list as a dictionary key?
a) The key-value pair is added successfully
b) Python raises a TypeError
c) The list is automatically converted to a tuple
d) The list key is ignored
Answer
b) Raises a TypeError
16. How do you copy a dictionary without modifying the original?
a) dict2 = dict1.copy()
b) dict2 = dict1
c) dict2 = dict(dict1)
d) Both a and c
Answer
d) Both dict1.copy()
and dict(dict1)
17. What is the correct way to iterate over dictionary keys?
a) for key in dict.keys():
b) for key in dict:
c) for key, value in dict.items():
d) Both a and b
Answer
d) Both for key in dict.keys():
and for key in dict:
18. Can dictionary values be duplicated?
a) Yes
b) No
Answer
a) Yes
19. What will my_dict.setdefault("key", "value")
do if "key"
does not exist?
a) Add "key": "value"
to the dictionary
b) Raise an error
c) Remove the existing key
d) Return "key"
without modifying the dictionary
Answer
a) Adds "key": "value"
20. What will be the output of the following code?
d = {"x": 10, "y": 20}
d["z"] = d["x"] + d["y"]
print(d)
a) {"x": 10, "y": 20, "z": 30}
b) {"x": 10, "y": 20, "z": 10}
c) {"x": 10, "y": 20}
d) Error
Answer
a) {"x": 10, "y": 20, "z": 30}
How did you do? 🎯
- 18-20 correct → 🏆 Excellent! You’re a Python functions pro!
- 14-17 correct → 👍 Great job! Keep practicing.
- 10-13 correct → 🙂 Good, but there’s room for improvement.
- Below 10 → 🤔 No worries! Review the concepts and try again.