Python Multiple Choice Questions & Answers (MCQs) focuses on “Dictionary – 3”.
Q 1. Which of the statements about dictionary values if false?
A. More than one key can have the same value
B. The values of the dictionary can be accessed as dict[key]
C. Values of a dictionary must be unique
D. Values of a dictionary can be a mixture of letters and numbers
Show Answer
Answer:-C. Values of a dictionary must be uniqueExplanation
More than one key can have the same value.Q 2. What will be the output of the following Python code snippet?
a={1:””A””,2:””B””,3:””C””}
del a
A. method del doesn’t exist for the dictionary
B. del deletes the values in the dictionary
C. del deletes the entire dictionary
D. del deletes the keys in the dictionary
Show Answer
Answer:-C. del deletes the entire dictionaryExplanation
del deletes the entire dictionary and any further attempt to access it will throw an error.Q 3.If a is a dictionary with some key-value pairs, what does a.popitem() do?
A. Removes an arbitrary element
B. Removes all the key-value pairs
C. Removes the key-value pair for the key given as an argument
D. Invalid method for dictionary
Show Answer
Answer:-A. Removes an arbitrary elementExplanation
The method popitem() removes a random key-value pair.Q 4. What will be the output of the following Python code snippet?
total={}
def insert(items):
if items in total:
total[items] += 1
else:
total[items] = 1
insert(‘Apple’)
insert(‘Ball’)
insert(‘Apple’)
print (len(total))
A. 3
B. 1
C. 2
D. 0
Show Answer
Answer:-C. 2Explanation
The insert() function counts the number of occurrences of the item being inserted into the dictionary. There are only 2 keys present since the key ‘Apple’ is repeated. Thus, the length of the dictionary is 2.Q 5. What will be the output of the following Python code snippet?
a = {}
a[1] = 1
a[‘1’] = 2
a[1]=a[1]+1
count = 0
for i in a:
count += a[i]
print(count)
A. 1
B. 2
C. 4
D. Error, the keys can’t be a mixture of letters and numbers
Show Answer
Answer:-C. 4Explanation
The above piece of code basically finds the sum of the values of keys.Q 6. What will be the output of the following Python code snippet?
numbers = {}
letters = {}
comb = {}
numbers[1] = 56
numbers[3] = 7
letters[4] = ‘B’
comb[‘Numbers’] = numbers
comb[‘Letters’] = letters
print(comb)
A. Error, dictionary in a dictionary can’t exist
B. ‘Numbers’: {1: 56, 3: 7}
C. {‘Numbers’: {1: 56}, ‘Letters’: {4: ‘B’}}
D. {‘Numbers’: {1: 56, 3: 7}, ‘Letters’: {4: ‘B’}}
Show Answer
Answer:-D. {‘Numbers’: {1: 56, 3: 7}, ‘Letters’: {4: ‘B’}}Explanation
Dictionary in a dictionary can exist.Q 7. What will be the output of the following Python code snippet?
test = {1:’A’, 2:’B’, 3:’C’}
test = {}
print(len(test))
A. 0
B. 3
C. None
D. An exception is thrown
Show Answer
Answer:-A. 0Explanation
In the second line of code, the dictionary becomes an empty dictionary. Thus, length=0.Q 8. What will be the output of the following Python code snippet?
test = {1:’A’, 2:’B’, 3:’C’}
del test[1]
test[1] = ‘D’
del test[2]
print(len(test))
A. 0
B. 1
C. 2
D. Error as the key-value pair of 1:’A’ is already deleted
Show Answer
Answer:-C. 2Explanation
After the key-value pair of 1:’A’ is deleted, the key-value pair of 1:’D’ is added.Q 9. What will be the output of the following Python code snippet?
import collections
a=collections.Counter([2,2,3,3,3,4])
b=collections.Counter([2,2,3,4,4])
a|b
A. Counter({3: 3, 2: 2, 4: 2})
B. Counter({2: 2, 3: 1, 4: 1})
C. Counter({3: 2})
D. Counter({4: 1})
Show Answer
Answer:-A. Counter({3: 3, 2: 2, 4: 2})Explanation
a|b returns the pair of keys and the highest recurring value.Q 10.What will be the output of the following Python code snippet?
import collections
a=collections.Counter([3,3,4,5])
b=collections.Counter([3,4,4,5,5,5])
a&b
A. Counter({3: 12, 4: 1, 5: 1})
B. Counter({3: 1, 4: 1, 5: 1})
C. Counter({4: 2})
D. Counter({5: 1})
Show Answer
Answer:-B. Counter({3: 1, 4: 1, 5: 1})Explanation
a&b returns the pair of keys and the lowest recurring value.”Q 11. What will be the output of the following Python code snippet?
a = {}
a[1] = 1
a[‘1’] = 2
a[1.0]=4
count = 0
for i in a:
count += a[i]
print(count)
A. 2
B. 3
C. 6
D. An exception is thrown
Show Answer
Answer:-C. 6Explanation
The value of key 1 is 4 since 1 and 1.0 are the same. Then, the function count() gives the sum of all the values of the keys (2+4).Q 12. What will be the output of the following Python code snippet?
a={}
a[‘a’]=1
a[‘b’]=[2,3,4]
print(a)
A. Exception is thrown
B. {‘b’: [2], ‘a’: 1}
C. {‘b’: [2], ‘a’: [3]}
D. {‘b’: [2, 3, 4], ‘a’: 1}
Show Answer
Answer:-D. {‘b’: [2, 3, 4], ‘a’: 1}Explanation
Mutable members can be used as the values of the dictionary but they cannot be used as the keys of the dictionary.Q 13. What will be the output of the following Python code snippet?
import collections
a=collections.Counter([1,1,2,3,3,4,4,4])
a
A. {1,2,3,4}
B. Counter({4, 1, 3, 2})
C. Counter({4: 3, 1: 2, 3: 2, 2: 1})
D. {4: 3, 1: 2, 3: 2, 2: 1}
Show Answer
Answer:-C. Counter({4: 3, 1: 2, 3: 2, 2: 1})Explanation
The statement a=collections.OrderedDict() generates a dictionary with the number as the key and the count of times the number appears as the value.Q 14. What will be the output of the following Python code snippet?
import collections
b=collections.Counter([2,2,3,4,4,4])
b.most_common(1)
A. Counter({4: 3, 2: 2, 3: 1})
B. {3:1}
C. {4:3}
D. [(4, 3)]
Show Answer
Answer:-D. [(4, 3)]Explanation
The most_common() method returns the n number key-value pairs where the value is the most recurring.Q 15. What will be the output of the following Python code snippet?
import collections
b=collections.Counter([2,2,3,4,4,4])
b.most_common(1)
A. Counter({4: 3, 2: 2, 3: 1})
B. {3:1}
C. {4:3}
D. [(4, 3)]
Leave a Reply