Python Dictionary MCQ
1). Which method returns a string representation of a dictionary?
a) str()
b) repr()
c) string()
d) __str__()
Corresct answer is: d) __str__()
Explanation: The __str__() method returns a string representation of a dictionary. It can be customized by overriding the method in a custom dictionary class.
2). Which method returns a dictionary’s iterator for keys?
a) keys()
b) iterate_keys()
c) key_iterator()
d) __iter__()
Corresct answer is: a) keys()
Explanation: The keys() method returns an iterator for the keys in a dictionary, allowing iteration over the keys.
3). Which method returns a dictionary’s iterator for values?
a) values()
b) value_iterator()
c) iterate_values()
d) __iter__()
Corresct answer is: a) values()
Explanation: The values() method returns an iterator for the values in a dictionary, allowing iteration over the values.
4). Which method returns a dictionary’s iterator for key-value pairs as tuples?
a) iteritems()
b) items()
c) iterate_pairs()
d) __iter__()
Corresct answer is: b) items()
Explanation: The items() method returns an iterator for the key-value pairs in a dictionary as tuples, allowing iteration over the key-value pairs.
5). Which method returns the sum of all the values in a dictionary?
a) sum()
b) total()
c) add_values()
d) __sum__()
Corresct answer is: a) sum()
Explanation: The sum() method can be used to calculate the sum of all the values in a dictionary.
6). In Python which method returns a new dictionary with only the specified keys and their values?
a) subset()
b) filter()
c) select()
d) __getitem__()
Corresct answer is: b) filter()
Explanation: The filter() method returns a new dictionary with only the specified keys and their corresponding values.
7). Which method returns a new dictionary with the specified default value for keys that are not present in the original dictionary?
a) set_default()
b) default()
c) default_value()
d) __missing__()
Corresct answer is: a) set_default()
Explanation: The set_default() method returns a new dictionary with the specified default value for keys that are not present in the original dictionary.
8). Which method returns the number of key-value pairs in a dictionary?
a) count()
b) size()
c) length()
d) __len__()
Corresct answer is: d) __len__()
Explanation: The __len__() method returns the number of key-value pairs in a dictionary.
9). Which method returns a copy of a dictionary?
a) clone()
b) copy()
c) duplicate()
d) __copy__()
Corresct answer is: b) copy()
Explanation: The copy() method returns a shallow copy of a dictionary.
10). Which method can be used to clear all the key-value pairs in a dictionary?
a) clear()
b) empty()
c) remove_all()
d) __clear__()
Corresct answer is: a) clear()
Explanation: The clear() method removes all the key-value pairs from a dictionary, making it empty.
11). Which method can be used to check if a specific value is present in a dictionary?
a) contains()
b) has_value()
c) in_value()
d) __contains__()
Corresct answer is: d) __contains__()
Explanation: The __contains__() method can be used to check if a specific value is present in a dictionary.
12). Which method can be used to combine two dictionaries into one?
a) combine()
b) merge()
c) join()
d) __add__()
Corresct answer is: b) merge()
Explanation: The merge() method can be used to combine two dictionaries into one.
13). Which method can be used to remove a specific key-value pair from a dictionary?
a) remove()
b) delete()
c) pop()
d) __remove__()
Corresct answer is: c) pop()
Explanation: The pop() method can be used to remove a specific key-value pair from a dictionary based on the given key.
14). What is the output of the following code?
dictionary = {‘a’: 5, ‘b’:3, ‘c’: 6, ‘d’: 8}
for val in dictionary:
print(val,”:”, dictionary[val]**2)
a) a: 5, b: 3, c: 6, d: 8
b) a: 25, b: 9, c: 36, d: 64
c) a: 5^2, b: 3^2, c: 6^2, d: 8^2
d) a: 25, b: 3, c: 36, d: 8
Corresct answer is: b) a: 25, b: 9, c: 36, d: 64
Explanation: The code iterates over the dictionary using a for loop. In each iteration, it prints the key followed by a colon and the square of the corresponding value. The output will be “a: 25, b: 9, c: 36, d: 64” because the values are squared before printing.
15). What will be the output of the following code?
D1 = {‘name’:’john’,’city’:’Landon’,’country’:’UK’}
D2 = {}
for val in D1:
D2[val] = D1[val]
print(D2)
a) {‘name’:’john’,’city’:’Landon’,’country’:’UK’}
b) {‘john’:’name’,’Landon’:’city’,’UK’:’country’}
c) {‘name’,’john’,’city’,’Landon’,’country’,’UK’}
d) {‘john’,’Landon’,’UK’}
Corresct answer is: a) {‘name’:’john’,’city’:’Landon’,’country’:’UK’}
Explanation: The code iterates over the key-value pairs in dictionary D1. It then assigns each key-value pair to the corresponding key in dictionary D2. Therefore, D2 will have the same key-value pairs as D1, resulting in the output {‘name’:’john’,’city’:’Landon’,’country’:’UK’}.
16). What is the output of the following code?
dict1 = {‘Name’:’Harry’,’Rollno’:345,’Address’:’Jordan’}
dict2 = {‘Age’:25,’salary’: ‘$25k’}
dict1.update(dict2)
print(dict1)
a){‘Name’:’Harry’,’Rollno’:345,’Address’:’Jordan’,’Age’:25,’salary’: ‘$25k’}
b) {‘Name’:’Harry’,’Age’:25,’Rollno’:345,’salary’: ‘$25k’,’Address’:’Jordan’}
c) {‘Name’:’Harry’,’Rollno’:345,’Address’:’Jordan’}
d) {‘Age’:25,’salary’: ‘$25k’}
Corresct answer is: a) {‘Name’:’Harry’,’Rollno’:345,’Address’:’Jordan’,’Age’:25,’salary’: ‘$25k’}
Explanation: The update() method is used to add the key-value pairs from dict2 into dict1. The original values in dict1 are updated with the corresponding values from dict2. Therefore, the output will be {‘Name’:’Harry’,’Rollno’:345,’Address’:’Jordan’,’Age’:25,’salary’: ‘$25k’}
17). What is the output of the following code?
dict1 = {1:25, 5:’abc’, 8:’pqr’, 21:’xyz’, 12:’def’, 2:’utv’}
list1 = [[val, dict1[val]] for val in dict1 if val%2 == 0]
list2 = [[val, dict1[val]] for val in dict1 if val%2 != 0]
print(“Even key = “, list1)
print(“Odd key = “, list2)
a) Even key = [[8, ‘pqr’], [12, ‘def’], [2, ‘utv’]]
Odd key = [[1, 25], [5, ‘abc’], [21, ‘xyz’]]
b) Even key = [[1, 25], [5, ‘abc’], [21, ‘xyz’]]
Odd key = [[8, ‘pqr’], [12, ‘def’], [2, ‘utv’]]
c) Even key = []
Odd key = [[1, 25], [5, ‘abc’], [21, ‘xyz’], [8, ‘pqr’], [12, ‘def’], [2, ‘utv’]]
d) Even key = [[1, 25], [5, ‘abc’], [21, ‘xyz’], [8, ‘pqr’], [12, ‘def’], [2, ‘utv’]]
Odd key = []
Corresct answer is: a) Even key = [[8, ‘pqr’], [12, ‘def’], [2, ‘utv’]]
Odd key = [[1, 25], [5, ‘abc’], [21, ‘xyz’]]
Explanation: In the given code, `list1` is generated using a list comprehension that iterates over the values in `dict1`. It only includes key-value pairs where the key is even. Similarly, `list2` is generated using a list comprehension that includes key-value pairs where the key is odd. The code then prints the contents of `list1` and `list2`. The output shows that `list1` contains the even key-value pairs and `list2` contains the odd key-value pairs from `dict1`.
18). What will be the output of the following code?
list1 = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]
list2 = [12, 23, 24, 25, 15, 16]
dict1 = {}
for a, b in zip(list1, list2):
dict1[a] = b
print(dict1)
a) {‘a’: 12, ‘b’: 23, ‘c’: 24, ‘d’: 25, ‘e’: 15}
b) {‘a’: 12, ‘b’: 23, ‘c’: 24, ‘d’: 25, ‘e’: 15, ’16’: None}
c) {‘a’: 12, ‘b’: 23, ‘c’: 24, ‘d’: 25, ‘e’: 15, ’16’: 16}
d) Error
Corresct answer is: a) {‘a’: 12, ‘b’: 23, ‘c’: 24, ‘d’: 25, ‘e’: 15}
Explanation: The code uses the `zip()` function to iterate over `list1` and `list2` simultaneously. It then assigns each element of `list1` as a key and each element of `list2` as a value in `dict1`. The resulting dictionary will be `{‘a’: 12, ‘b’: 23, ‘c’: 24, ‘d’: 25, ‘e’: 15}`.
19). What is the output of the following code?
list1 = [4, 5, 6, 2, 1, 7, 11]
dict1 = {}
for val in list1:
if val % 2 == 0:
dict1[val] = val**2
else:
dict1[val] = val**3
print(dict1)
a) {4: 16, 5: 125, 6: 36, 2: 4, 1: 1, 7: 343, 11: 1331}
b) {4: 8, 5: 9, 6: 10, 2: 4, 1: 1, 7: 49, 11: 121}
c) {4: 8, 5: 125, 6: 36, 2: 4, 1: 1, 7: 343, 11: 121}
d) {4: 16, 5: 9, 6: 10, 2: 4, 1: 1, 7: 343, 11: 1331}
Corresct answer is: a) {4: 16, 5: 125, 6: 36, 2: 4, 1: 1, 7: 343, 11: 1331}
Explanation: The code initializes an empty dictionary `dict1` and iterates over the elements of `list1`. For each element, if it is even (divisible by 2), the element is used as a key in `dict1` with its square value as the corresponding value. If it is odd, the element is used as a key with its cube value as the corresponding value. The final dictionary `dict1` will contain the following key-value pairs: `{4: 16, 5: 125, 6: 36, 2: 4, 1: 1, 7: 343, 11: 1331}`.
20). What is the output of the following code?
dict1 = {‘Name’: ‘Harry’, ‘Rollno’: 345, ‘Address’: ‘Jordan’}
dict1.clear()
print(dict1)
a) {}
b) {‘Name’: ‘Harry’, ‘Rollno’: 345, ‘Address’: ‘Jordan’}
c) Error
d) None
Corresct answer is: a) {}
Explanation: The `clear()` method removes all the key-value pairs from the dictionary, making it empty. Therefore, the output is an empty dictionary `{}`.
21). What is the output of the following code?
string = “SQAToolss”
dict1 = {}
for char in string:
dict1[char] = string.count(char)
print(dict1)
a) {‘S’: 2, ‘Q’: 1, ‘A’: 1, ‘T’: 1, ‘o’: 2, ‘l’: 1, ‘s’: 2}
b) {‘S’: 1, ‘Q’: 1, ‘A’: 1, ‘T’: 1, ‘o’: 2, ‘l’: 1, ‘s’: 2}
c) {‘S’: 1, ‘Q’: 1, ‘A’: 1, ‘T’: 1, ‘o’: 1, ‘l’: 1, ‘s’: 2}
d) {‘S’: 1, ‘Q’: 1, ‘A’: 1, ‘T’: 1, ‘o’: 1, ‘l’: 1, ‘s’: 1}
Corresct answer is: a) {‘S’: 2, ‘Q’: 1, ‘A’: 1, ‘T’: 1, ‘o’: 2, ‘l’: 1, ‘s’: 2}
Explanation: The code initializes an empty dictionary `dict1` and iterates over each character in the string “SQAToolss”. For each character, it counts the number of occurrences in the string using the `count()` method and assigns the count as the value for that character in the dictionary. The final dictionary will contain the counts of each character in the string.
22). What is the output of the following code?
dict1 = {‘d’:14,’b’:52,’a’:13,’c’: 1}
sorted_ = {key: value for key, value in sorted(dict1.items(), key=lambda item: item[1])}
print(sorted_)
a) {‘c’: 1, ‘a’: 13, ‘d’: 14, ‘b’: 52}
b) {‘a’: 13, ‘b’: 52, ‘c’: 1, ‘d’: 14}
c) {‘b’: 52, ‘d’: 14, ‘a’: 13, ‘c’: 1}
d) {‘c’: 1, ‘b’: 52, ‘a’: 13, ‘d’: 14}
Corresct answer is: a) {‘c’: 1, ‘a’: 13, ‘d’: 14, ‘b’: 52}
Explanation: The code snippet sorts the dictionary `dict1` based on the values in ascending order using a lambda function as the sorting key. The resulting dictionary `sorted_` will have key-value pairs sorted by their values from smallest to largest.The output will be `{‘c’: 1, ‘a’: 13, ‘d’: 14, ‘b’: 52}`.
23). What is the output of the following code?
D1 = {‘x’: 23, ‘y’: 10, ‘z’: 7}
total = 0
for val in D1.values():
total += val
print(total)
a) 23
b) 40
c) 50
d) 7
Corresct answer is: b) 40
Explanation: The code calculates the sum of all the values in the dictionary D1. The values are accessed using the values() method and then added to the total variable using a loop. The sum of 23 + 10 + 7 is 40, which is printed as the output.
24). What is the output of the following code?
D1 = {‘city’:’pune’,’state’:’maharashtra’}
count = 0
for key in D1.keys():
if key == “Country”:
count += 1
if count > 0:
print(“Key exists”)
else:
print(“Key does not exist”)
a) Key exists
b) Key does not exist
c) Error
d) None
Corresct answer is: b) Key does not exist
Explanation: In the given code, the loop iterates over the keys of the dictionary `D1`. It checks if any key is equal to “Country”. Since there is no key with the name “Country” in the dictionary, the `count` variable remains 0. After the loop, the code checks if `count` is greater than 0. Since it is not, the output will be “Key does not exist”.
25). What is the output of the following code?
n = 4
D1 = {}
for i in range(1, n+1):
D1.update({i: i**3})
print(D1)
a) {1: 1, 2: 4, 3: 9, 4: 16}
b) {1: 1, 2: 8, 3: 27, 4: 64}
c) {1: 1, 2: 9, 3: 27, 4: 81}
d) {1: 1, 2: 16, 3: 81, 4: 256}
Corresct answer is: b) {1: 1, 2: 8, 3: 27, 4: 64}
Explanation: The code initializes an empty dictionary, `D1`. It then iterates over the numbers from 1 to 4 (inclusive) using the `range()` function. For each number, it updates the dictionary `D1` with a key-value pair, where the key is the number itself, and the value is the cube of the number. After the loop, the resulting dictionary `D1` will contain the key-value pairs `{1: 1, 2: 8, 3: 27, 4: 64}`.