Python Questions and Answers – Dictionary – 3

This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “Dictionary – 3”.

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
View Answer

Answer: c
Explanation: Dictionary values do not have to be unique — multiple keys can have the same value. Only keys must be unique. So, the statement “Values of a dictionary must be unique” is false.

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
View Answer

Answer: c
Explanation: The del statement deletes the entire dictionary object from memory. After del a, the variable a no longer exists. Attempting to access it afterward would raise a NameError.
advertisement

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
View Answer

Answer: a
Explanation: The a.popitem() method removes and returns an arbitrary key-value pair from the dictionary. In Python 3.7 and later, it removes the last inserted item due to insertion-order preservation.
Free 30-Day Python Certification Bootcamp is Live. Join Now!

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
View Answer

Answer: c
Explanation:The insert() function keeps track of how many times an item is inserted using a dictionary named total.

  • ‘Apple’ is inserted first → total = {‘Apple’: 1}
  • ‘Ball’ is inserted → total = {‘Apple’: 1, ‘Ball’: 1}
  • ‘Apple’ is inserted again → count updated → total = {‘Apple’: 2, ‘Ball’: 1}

The length of the dictionary (len(total)) is 2, since there are two unique keys: ‘Apple’ and ‘Ball’.

advertisement

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
View Answer

Answer: c
Explanation: In the given code, a dictionary is created with both an integer key 1 and a string key ‘1’, which are treated as separate keys in Python. The values for both keys are updated and then summed using a loop. The final output is 4, since both keys have the value 2.

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’}}
View Answer

Answer: d
Explanation: In this code, two dictionaries numbers and letters are created and then assigned as values to keys ‘Numbers’ and ‘Letters’ in another dictionary comb. Python supports nested dictionaries, so the output will be: {‘Numbers’: {1: 56, 3: 7}, ‘Letters’: {4: ‘B’}}.

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) None
c) 3
d) An exception is thrown
View Answer

Answer: a
Explanation: The dictionary test is first initialized with three key-value pairs but is then reassigned to an empty dictionary using test = {}. So, len(test) returns 0.

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) 2
c) Error as the key-value pair of 1:’A’ is already deleted
d) 1
View Answer

Answer: b
Explanation: The dictionary starts with 3 items. Key 1 is deleted and then re-added with a new value. Key 2 is deleted, leaving only two keys: 1 and 3. So, len(test) returns 2.

9. 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) An exception is thrown
b) 3
c) 6
d) 2
View Answer

Answer: c
Explanation: In Python, 1 (int) and 1.0 (float) are considered the same dictionary key. So when a[1.0] = 4 is assigned, it overwrites a[1] = 1. The dictionary becomes: {1: 4, ‘1’: 2}
The loop adds 4 + 2, so the final output is 6.

10. 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}
View Answer

Answer: d
Explanation: The dictionary a is assigned two key-value pairs: ‘a’ maps to 1 and ‘b’ maps to the list [2, 3, 4]. Printing a outputs the dictionary with both these entries exactly as assigned, so the output is {‘b’: [2, 3, 4], ‘a’: 1}.

11. What will be the output of the following Python code snippet?

import collections
a=collections.Counter([1,1,2,3,3,4,4,4])
print(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}
View Answer

Answer: c
Explanation: The code uses collections.Counter to count the occurrences of each element in the list [1,1,2,3,3,4,4,4]. It returns a Counter object showing how many times each number appears: 4 appears 3 times, 1 and 3 appear twice each, and 2 appears once. Therefore, the output is Counter({4: 3, 1: 2, 3: 2, 2: 1}).

12. What will be the output of the following Python code snippet?

import collections
b=collections.Counter([2,2,3,4,4,4])
print(b.most_common(1))

a) Counter({4: 3, 2: 2, 3: 1})
b) {3:1}
c) {4:3}
d) [(4, 3)]
View Answer

Answer: d
Explanation: The code uses collections.Counter to count the occurrences of each element in the list [2,2,3,4,4,4]. The most_common(1) method returns a list with the single most common element and its count as a tuple. Since 4 appears 3 times (the most), the output is [(4, 3)].

13. What will be the output of the following Python code snippet?

import collections
b=collections.Counter([2,2,3,4,4,4])
print(b.most_common(0))

a) Counter({4: 3, 2: 2, 3: 1})
b) {3:1}
c) {4:3}
d) []
View Answer

Answer: d
Explanation: The most_common(0) method returns an empty list because it asks for zero most common elements. So, the output is [].

14. 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])
print(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})
View Answer

Answer: a
Explanation: The | operator on Counter objects returns the element-wise maximum of counts from both counters. For each key, it takes the higher count between the two counters.

  • For 2, counts are 2 in both a and b, so max is 2.
  • For 3, counts are 3 in a and 1 in b, so max is 3.
  • For 4, counts are 1 in a and 2 in b, so max is 2.

Hence, the result is Counter({3: 3, 2: 2, 4: 2}).

15. 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])
print(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})
View Answer

Answer: b
Explanation: The & operator on Counter objects returns the intersection, which is the minimum count for each element present in both counters.

  • For 3, counts are 2 in a and 1 in b, so min is 1.
  • For 4, counts are 1 in a and 2 in b, so min is 1.
  • For 5, counts are 1 in a and 3 in b, so min is 1.

So the result is Counter({3: 1, 4: 1, 5: 1}).

Sanfoundry Global Education & Learning Series – Python.

To practice all areas of Python, here is complete set of 1000+ Multiple Choice Questions and Answers.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.