Dictionary Quizs
Dictionary Quizs
Question 1: What is the result of calling the `len()` function on an empty dictionary?
0 - Correct!
None
Error
>
[ ] - Correct!
Question 3: What will be the output of the following code? my_dict = {'a': 1, 'b': 2, 'c': 3}
print(my_dict['b'])
2 - Correct!
insert()
append()
update() - Correct!
Question 5: What is the output of the following code? my_dict = {'a': 1, 'b': 2, 'c': 3} my_dict['d'] = 4
print(my_dict)
Error
Question 6: Which of the following is not a valid key type for a dictionary in Python?
String - Incorrect! Correct answer: List
List - Correct!
Tuple
Removes and returns the last element - Incorrect! Correct answer: Removes and returns the
specified key-value pair
Question 8: What will be the output of the following code? my_dict = {'a': 1, 'b': 2, 'c': 3}
print(my_dict.pop('b'))
2 - Correct!
'b'
KeyError: 'b'
Question 9: Which method is used to remove a key-value pair from a dictionary without raising
an error if the key is not found?
delete()
discard()
pop() - Correct!
Question 10: What is the output of the following code? my_dict = {'a': 1, 'b': 2, 'c': 3}
print(my_dict.get('d'))
KeyError
None - Correct!
Error
keys() - Correct!
get_keys()
dict_keys()
list_keys()
Question 12: What is the output of the following code? my_dict = {'a': 1, 'b': 2, 'c': 3} print('b' in
my_dict)
True - Correct!
False
Error
None
Question 13: Which of the following is used to iterate over the items of a dictionary?
Question 14: What is the output of the following code? my_dict = {'a': 1, 'b': 2, 'c': 3}
my_dict.clear() print(my_dict)
{} - Correct!
None
Error
Question 15: What will be the output of the following code? my_dict = {'a': 1, 'b': 2, 'c': 3}
print(len(my_dict))
3 - Correct!
copy() - Correct!
duplicate()
Question 17: What is the correct way to create an empty dictionary in Python?
{} - Correct!
()
None
Question 18: What will be the output of the following code? my_dict = {'a': 1, 'b': 2, 'c': 3}
my_dict.update({'d': 4}) print(my_dict)
Error
Question 19: What method is used to retrieve the value for a given key from a dictionary?
retrieve()
get() - Correct!
fetch()
read()
Associative - Correct!
Ordered
Indexed
Question 21: Which of the following is not a valid key type for a dictionary in Python?
Integer
String
List - Correct!
Tuple
Question 22: What will be the output of the following code? my_dict = {'a': 1, 'b': 2, 'c': 3}
print(my_dict['d'])
KeyError: 'd'
None
'd'
Question 23: Which method is used to remove a key-value pair from a dictionary?
pop() - Correct!
delete()
remove()
discard()
Question 24: What will be the output of the following code? my_dict = {'apple': 2, 'banana': 3,
'orange': 4} del my_dict['banana'] print(my_dict)
{'apple': 2, 'orange': 4}
{'banana': 3, 'orange': 4}
Question 25: Which of the following methods returns a list of all the keys in a dictionary?
keys() - Correct!
get_keys()
dict_keys()
list_keys()
Adds a new key-value pair - Incorrect! Correct answer: Updates existing key-value pairs with
new values
Question 27: What is the time complexity for accessing an element in a dictionary by its key?
O(1) - Correct!
O(log n)
O(n)
O(n log n)
Question 28: Which of the following functions creates a dictionary from two lists, one
containing keys and the other containing values?
create_dict()
dict_from_lists()
zip_dict()
dict() - Correct!
Question 29: What will be the output of the following code? my_dict = {'a': 1, 'b': 2, 'c': 3}
print(len(my_dict))
3 - Correct!