Welcome to the Python Dictionaries Quiz!
Dictionaries in Python are highly effective knowledge buildings that retailer key-value pairs. They permit for quick lookups, versatile knowledge storage, and simple modifications. This quiz will check your understanding of the right way to create, manipulate, and entry dictionaries in Python.
Every query is designed to strengthen your information of dictionary strategies, operations, and finest practices. Let’s see how effectively you realize Python dictionaries!
Desk of Contents:
- Python quizzes for inexperienced persons sequence
Dictionaries Quiz
Quiz Questions
1. How do you create an empty dictionary in Python?
a) dict = {}
b) dict = []
c) dict = ()
d) dict = ""
Reply
a) dict = {}
2. What is going to dict1 = {"identify": "Alice", "age": 25}
return for dict1["name"]
?
a) "Alice"
b) "age"
c) 25
d) Error
Reply
a) "Alice"
3. Which methodology is used to get all of the keys in a dictionary?
a) keys()
b) values()
c) gadgets()
d) get_keys()
Reply
a) keys()
4. What does the next code return?
my_dict = {"a": 1, "b": 2, "c": 3}
print(my_dict.get("d", 10))
a) None
b) 10
c) d
d) Error
Reply
b) 10
5. How do you examine if a key exists in a dictionary?
a) "key" in dict
b) dict.has("key")
c) dict.comprises("key")
d) dict.exists("key")
Reply
a) "key" in dict
6. What does dict1.gadgets()
return?
a) A listing of dictionary values
b) A listing of dictionary keys
c) A listing of key-value pairs as tuples
d) A string of dictionary contents
Reply
c) Record of key-value pairs as tuples
7. How will you take away an merchandise from a dictionary utilizing a key?
a) dict.pop(key)
b) dict.take away(key)
c) dict.delete(key)
d) del dict.key
Reply
a) dict.pop(key)
8. What occurs should you attempt to entry a non-existent key utilizing dict[key]
?
a) It returns None
b) It raises a KeyError
c) It provides the important thing with a default worth
d) It creates a brand new dictionary
Reply
b) Raises a KeyError
9. Which methodology removes all key-value pairs from a dictionary?
a) clear()
b) popitem()
c) take away()
d) del()
Reply
a) clear()
10. Can dictionary keys be mutable knowledge sorts (like lists)?
a) Sure
b) No
Reply
b) No
11. How do you merge two dictionaries in Python 3.9+?
a) dict1.replace(dict2)
b) dict3 = dict1 + dict2
c) dict3 = dict1 | dict2
d) dict1.append(dict2)
Reply
c) dict3 = dict1 | dict2
12. What does dict1.values()
return?
a) Record of dictionary values
b) Record of dictionary keys
c) Record of key-value pairs
d) Error
Reply
a) Record of dictionary values
13. What is going to dict1.popitem()
do?
a) Take away a random key-value pair
b) Take away the final inserted key-value pair
c) Take away the primary inserted key-value pair
d) Elevate an error
Reply
b) Take away the final inserted key-value pair
14. What’s 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
Reply
a) "a"
15. What occurs if we attempt to assign an inventory as a dictionary key?
a) The important thing-value pair is added efficiently
b) Python raises a TypeError
c) The listing is routinely transformed to a tuple
d) The listing key’s ignored
Reply
b) Raises a TypeError
16. How do you copy a dictionary with out modifying the unique?
a) dict2 = dict1.copy()
b) dict2 = dict1
c) dict2 = dict(dict1)
d) Each a and c
Reply
d) Each dict1.copy()
and dict(dict1)
17. What’s the appropriate option to iterate over dictionary keys?
a) for key in dict.keys():
b) for key in dict:
c) for key, worth in dict.gadgets():
d) Each a and b
Reply
d) Each for key in dict.keys():
and for key in dict:
18. Can dictionary values be duplicated?
a) Sure
b) No
Reply
a) Sure
19. What is going to my_dict.setdefault("key", "worth")
do if "key"
doesn’t exist?
a) Add "key": "worth"
to the dictionary
b) Elevate an error
c) Take away the present key
d) Return "key"
with out modifying the dictionary
Reply
a) Provides "key": "worth"
20. What would be the output of the next 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
Reply
a) {"x": 10, "y": 20, "z": 30}
How did you do? 🎯
- 18-20 appropriate → 🏆 Glorious! You’re a Python features professional!
- 14-17 appropriate → 👍 Nice job! Hold practising.
- 10-13 appropriate → 🙂 Good, however there’s room for enchancment.
- Under 10 → 🤔 No worries! Evaluation the ideas and check out once more.