Python Dictionaries Quiz

Python
0 Passed
0% acceptance

A 35-question quiz covering dictionary creation, key-value pairs, accessing data, modifying dictionaries, and checking for keys.

35 Questions
~70 minutes
1

Question 1

Which syntax is used to create a dictionary in Python?

A
Curly braces {}
B
Square brackets []
C
Parentheses ()
D
Angle brackets <>
2

Question 2

In a dictionary, how are keys and values separated?

A
By a colon (:)
B
By a comma (,)
C
By an equal sign (=)
D
By a dash (-)
3

Question 3

Which of the following is a requirement for dictionary keys?

A
They must be immutable
B
They must be strings
C
They must be integers
D
They must be mutable
4

Question 4

How do you access the value associated with the key 'name' in a dictionary called 'person'?

A
person['name']
B
person.name
C
person(name)
D
person->name
5

Question 5

What happens if you try to access a key that does not exist in the dictionary?

A
KeyError
B
ValueError
C
None is returned
D
IndexError
6

Question 6

How do you add a new key-value pair to an existing dictionary?

A
dict[key] = value
B
dict.add(key, value)
C
dict.push(key, value)
D
dict.insert(key, value)
7

Question 7

What does the pop() method do in a dictionary?

A
Removes a key and returns its value
B
Removes the last item added
C
Removes a random item
D
Deletes the dictionary
8

Question 8

Which keyword is used to check if a key exists in a dictionary?

A
in
B
exists
C
has
D
contains
9

Question 9

What is the output of this code?

python
d = {"a": 1, "b": 2}
print(d["a"])
A
1
B
a
C
2
D
Error
10

Question 10

What does this code output?

python
data = {"x": 10}
data["y"] = 20
print(len(data))
A
2
B
1
C
0
D
Error
11

Question 11

Which method returns all the keys in a dictionary?

A
keys()
B
get_keys()
C
all_keys()
D
list_keys()
12

Question 12

What is the result of this code?

python
info = {"name": "Alice", "age": 25}
info.pop("age")
print(info)
A
{'name': 'Alice'}
B
{'age': 25}
C
{}
D
Error
13

Question 13

Can a dictionary contain another dictionary as a value?

A
Yes, dictionaries can be nested
B
No, values must be primitives
C
Only if keys are strings
D
Only one level deep
14

Question 14

What is the output?

python
nums = {"one": 1, "two": 2}
print("one" in nums)
A
True
B
False
C
1
D
None
15

Question 15

What happens if you assign a value to an existing key?

A
The old value is overwritten
B
A new duplicate key is created
C
An error is raised
D
The assignment is ignored
16

Question 16

What is the output?

python
d = {"a": 1}
d["a"] = 2
print(d["a"])
A
2
B
1
C
1, 2
D
Error
17

Question 17

Which method allows you to retrieve a value but return a default if the key is missing?

A
get()
B
fetch()
C
retrieve()
D
find()
18

Question 18

What is the output?

python
scores = {"math": 90, "science": 85}
print(scores.get("history", 0))
A
0
B
None
C
Error
D
history
19

Question 19

How do you create an empty dictionary?

A
{}
B
[]
C
()
D
empty_dict()
20

Question 20

What is the output?

python
x = {"a": 1, "b": 2}
del x["a"]
print("a" in x)
A
False
B
True
C
None
D
Error
21

Question 21

Which of these is NOT a valid dictionary key?

A
['a', 'b']
B
'key'
C
123
D
(1, 2)
22

Question 22

What is the output?

python
d = {1: "one", 2: "two"}
print(d[1])
A
one
B
1
C
two
D
Error
23

Question 23

What does the values() method return?

A
A view of all values in the dictionary
B
A list of keys
C
A list of tuples
D
The first value
24

Question 24

What is the output?

python
d = {"a": 1}
print(d.pop("b", 0))
A
0
B
Error
C
None
D
b
25

Question 25

How do you merge dictionary B into dictionary A?

A
A.update(B)
B
A.merge(B)
C
A.append(B)
D
A.add(B)
26

Question 26

What is the output?

python
d = {"a": 1, "b": 2}
print(len(d))
A
2
B
4
C
1
D
0
27

Question 27

What is the output?

python
d = {"a": 1, "a": 2}
print(d["a"])
A
2
B
1
C
Error
D
[1, 2]
28

Question 28

Which method removes all items from a dictionary?

A
clear()
B
empty()
C
delete_all()
D
reset()
29

Question 29

What is the output?

python
d = {1: "a", 2: "b"}
print(2 in d)
A
True
B
False
C
b
D
Error
30

Question 30

What is the output?

python
d = {"x": 10}
val = d.pop("x")
print(val)
A
10
B
x
C
None
D
Error
31

Question 31

What is the result of this code?

python
d = {}
d["key"] = "value"
print(d)
A
{'key': 'value'}
B
['key', 'value']
C
('key', 'value')
D
Error
32

Question 32

Can you iterate over a dictionary using a for loop?

A
Yes, it iterates over keys by default
B
Yes, it iterates over values
C
No, dictionaries are not iterable
D
Yes, but only with .items()
33

Question 33

What is the output?

python
d = {"a": 1}
d["b"] = 2
del d["a"]
print(len(d))
A
1
B
2
C
0
D
Error
34

Question 34

What is the output?

python
d = {"a": 1}
print(d.get("b"))
A
None
B
Error
C
0
D
False
35

Question 35

Which of the following creates a dictionary from a list of tuples?

python
pairs = [("a", 1), ("b", 2)]
A
dict(pairs)
B
list(pairs)
C
tuple(pairs)
D
convert(pairs)

QUIZZES IN Python