Master Python Dictionaries with These 30 Multiple Choice Questions
30 multiple-choice questions about Python dictionaries:
What is the data structure used to represent dictionaries in Python?
A) Arrays
B) Linked Lists
C) Hash Tables
D) Stacks
How do you initialize an empty dictionary in Python?
A)
dict()
B)
{}
C)
[]
D)
()
In a dictionary, keys must be:
A) Immutable
B) Mutable
C) Ordered
D) Sorted
Which method is used to access the value associated with a key in a dictionary?
A)
get()
B)
retrieve()
C)
access()
D)
value()
What happens if you try to access a key that doesn't exist in a dictionary?
A) It raises a KeyError
B) It returns None
C) It returns an empty dictionary
D) It returns an empty string
How do you add a new key-value pair to an existing dictionary?
A) Using the
insert()
methodB) Using the
add()
methodC) Using the
append()
methodD) Using square brackets
[]
Can a dictionary have multiple keys with the same value?
A) Yes
B) No
How do you remove a key-value pair from a dictionary?
A)
delete()
B)
remove()
C)
pop()
D)
erase()
Which method is used to iterate over the keys in a dictionary?
A)
keys()
B)
iterate()
C)
get_keys()
D)
get()
How do you merge two dictionaries in Python?
A) Using the
merge()
methodB) Using the
combine()
methodC) Using the
update()
methodD) Using the
concatenate()
method
Which method is used to sort a dictionary by its keys?
A)
sort()
B)
sorted()
C)
sort_keys()
D)
order_keys()
How do you check if a key exists in a dictionary?
A) Using the
exists()
methodB) Using the
contains()
methodC) Using the
in
keywordD) Using the
has_key()
method
How do you create a dictionary from two lists?
A) Using loops
B) Using the
dict()
constructorC) Using the
zip()
functionD) Using list comprehension
What is the purpose of the
collections.defaultdict
class?A) To create dictionaries with default values
B) To create dictionaries with ordered keys
C) To create dictionaries with sorted values
D) To create dictionaries with unique keys
How do you create a shallow copy of a dictionary?
A) Using the
copy()
methodB) Using the
clone()
methodC) Using the
shallow_copy()
methodD) Using the
duplicate()
method
Which method is used to clear all elements from a dictionary?
A)
clear()
B)
remove_all()
C)
delete_all()
D)
empty()
How do you check if a dictionary is empty?
A) Using the
empty()
methodB) Using the
is_empty()
methodC) Using the
len()
functionD) Using the
size()
method
Can dictionaries be nested in Python?
A) Yes
B) No
How do you sort a dictionary by its values?
A) Using the
sort_values()
methodB) Using the
sorted()
function with a custom keyC) Using the
order_values()
methodD) Using the
sort()
method
Which method is used to create a dictionary with default values?
A)
defaultdict()
B)
initialize()
C)
set_default()
D)
create_default()
What is the difference between
dict.keys()
,dict.values()
, anddict.items()
?A) They return the same result
B)
keys()
returns keys,values()
returns values, anditems()
returns key-value pairsC)
keys()
returns values,values()
returns keys, anditems()
returns keysD) They are not valid methods in Python dictionaries
How do you create a deep copy of a dictionary?
A) Using the
deepcopy()
function from thecopy
moduleB) Using the
clone()
methodC) Using the
duplicate()
methodD) Using the
deep_copy()
method
What is the difference between
dict.get(key)
anddict[key]
?A) There is no difference
B)
get()
returns the value if the key exists, otherwise None, while[key]
raises a KeyError if the key doesn't existC)
get()
raises a KeyError if the key doesn't exist, while[key]
returns NoneD)
get()
returns True if the key exists, otherwise False, while[key]
returns the value
How do you iterate over the values in a dictionary?
A) Using a
for
loop withvalues()
methodB) Using a
for
loop withitems()
methodC) Using a
while
loop withvalues()
methodD) Using a
while
loop withitems()
method
How do you iterate over key-value pairs in a dictionary?
A) Using a
for
loop withkeys()
methodB) Using a
for
loop withvalues()
methodC) Using a
for
loop withitems()
methodD) Using a
while
loop withkeys()
method
Which method is used to create a dictionary with keys from a sequence and default value for all keys?
A)
defaultdict()
B)
setdefault()
C)
fromkeys()
D)
initialize()
How do you create a dictionary comprehension in Python?
A)
[key: value for key, value in iterable]
B)
{key: value for key, value in iterable}
C)
(key: value for key, value in iterable)
D) `<key, value> for key, value in
iterable`
Which method is used to check the presence of a key in a dictionary without raising an error?
A)
contains_key()
B)
has_key()
C)
key_exists()
D)
get()
Which method is used to update a dictionary with elements from another dictionary or from an iterable of key-value pairs?
A)
merge()
B)
update()
C)
extend()
D)
append()
How do you sort a dictionary by its keys in descending order?
A)
sorted(d, reverse=True)
B)
sort(d, reverse=True)
C)
d.sort(reverse=True)
D)
sort_keys(d, reverse=True)
Answers:
C) Hash Tables
B)
{}
A) Immutable
A)
get()
A) It raises a KeyError
D) Using square brackets
[]
A) Yes
C)
pop()
A)
keys()
C) Using the
update()
methodB)
sorted()
C) Using the
in
keywordC) Using the
zip()
functionA) To create dictionaries with default values
A) Using the
copy()
methodA)
clear()
C) Using the
len()
functionA) Yes
B) Using the
sorted()
function with a custom keyA)
defaultdict()
B)
keys()
returns keys,values()
returns values, anditems()
returns key-value pairsA) Using the
deepcopy()
function from thecopy
moduleB)
get()
returns the value if the key exists, otherwise None, while[key]
raises a KeyError if the key doesn't existA) Using a
for
loop withvalues()
methodC) Using a
for
loop withitems()
methodC)
fromkeys()
B)
{key: value for key, value in iterable}
D)
get()
B)
update()
A)
sorted(d, reverse=True)