Python MCQs on Dictionary: SET 4

31. 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’}}

Check Answer

Answer: d
Dictionary in a dictionary can exist.

32. 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

Check Answer

Answer: a
In the second line of code, the dictionary becomes an empty dictionary. Thus, length=0.

33. 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

Check Answer

Answer: b
After the key-value pair of 1:’A’ is deleted, the key-value pair of 1:’D’ is added.

34. 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

Check Answer

Answer: c
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).

35. 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}

Check Answer

Answer: d
Mutable members can be used as the values of the dictionary but they cannot be used as the keys of the dictionary.

36. 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}

Check Answer

Answer: c
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.

37. 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)]

Check Answer

Answer: d
The most_common() method returns the n number key-value pairs where the value is the most recurring.

38. 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)]

Check Answer

Answer: d
The most_common() method returns the n number key-value pairs where the value is the most recurring.

39. 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})

Check Answer

Answer: a
a|b returns the pair of keys and the highest recurring value.

40. 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})

Check Answer

Answer: b
a&b returns the pair of keys and the lowest recurring value.

Page  1  2  3  4  5

Python Interview Questions (MCQs)

We have divided these Python Questions and Answers into various parts based on the topics. Open the Topic of your choice and Practice these MCQs.

Python MCQs on Variables and Operators Python MCQs on Precedence and Associativity
Python MCQs on Data Type Python MCQs on Boolean
Python MCQs on Bitwise Operators Python MCQs on Formatting and Advance Formatting
Python MCQs on Decorators Python MCQs on While and For Loops
Python MCQs on List Python MCQs on List Comprehension
Python MCQs on String Python MCQs on Tuple
Python MCQs on SET Python MCQs on Dictionary
Python MCQs on Functions Python MCQs on Argument Parsing
Python MCQs on Global and Local Variables Python MCQs on Recursion
Python MCQs on Mapping Functions Python MCQs on Modules
Python MCQs on Regular Expressions Python MCQs on Files
Python MCQs on Overloading Python MCQs on Classes and Objects
Python MCQs on Inheritance Python MCQs on Polymorphism and Encapsulation
Python MCQs on Exception Handling

Leave a Comment