Python Dictionaries Quiz
A 35-question quiz covering dictionary creation, key-value pairs, accessing data, modifying dictionaries, and checking for keys.
Question 1
Which syntax is used to create a dictionary in Python?
Question 2
In a dictionary, how are keys and values separated?
Question 3
Which of the following is a requirement for dictionary keys?
Question 4
How do you access the value associated with the key 'name' in a dictionary called 'person'?
Question 5
What happens if you try to access a key that does not exist in the dictionary?
Question 6
How do you add a new key-value pair to an existing dictionary?
Question 7
What does the pop() method do in a dictionary?
Question 8
Which keyword is used to check if a key exists in a dictionary?
Question 9
What is the output of this code?
d = {"a": 1, "b": 2}
print(d["a"])Question 10
What does this code output?
data = {"x": 10}
data["y"] = 20
print(len(data))Question 11
Which method returns all the keys in a dictionary?
Question 12
What is the result of this code?
info = {"name": "Alice", "age": 25}
info.pop("age")
print(info)Question 13
Can a dictionary contain another dictionary as a value?
Question 14
What is the output?
nums = {"one": 1, "two": 2}
print("one" in nums)Question 15
What happens if you assign a value to an existing key?
Question 16
What is the output?
d = {"a": 1}
d["a"] = 2
print(d["a"])Question 17
Which method allows you to retrieve a value but return a default if the key is missing?
Question 18
What is the output?
scores = {"math": 90, "science": 85}
print(scores.get("history", 0))Question 19
How do you create an empty dictionary?
Question 20
What is the output?
x = {"a": 1, "b": 2}
del x["a"]
print("a" in x)Question 21
Which of these is NOT a valid dictionary key?
Question 22
What is the output?
d = {1: "one", 2: "two"}
print(d[1])Question 23
What does the values() method return?
Question 24
What is the output?
d = {"a": 1}
print(d.pop("b", 0))Question 25
How do you merge dictionary B into dictionary A?
Question 26
What is the output?
d = {"a": 1, "b": 2}
print(len(d))Question 27
What is the output?
d = {"a": 1, "a": 2}
print(d["a"])Question 28
Which method removes all items from a dictionary?
Question 29
What is the output?
d = {1: "a", 2: "b"}
print(2 in d)Question 30
What is the output?
d = {"x": 10}
val = d.pop("x")
print(val)Question 31
What is the result of this code?
d = {}
d["key"] = "value"
print(d)Question 32
Can you iterate over a dictionary using a for loop?
Question 33
What is the output?
d = {"a": 1}
d["b"] = 2
del d["a"]
print(len(d))Question 34
What is the output?
d = {"a": 1}
print(d.get("b"))Question 35
Which of the following creates a dictionary from a list of tuples?
pairs = [("a", 1), ("b", 2)]