Python MCQs on Dictionary: SET 3

21. Which of the following isn’t true about dictionary keys?

a) More than one key isn’t allowed

b) Keys must be immutable

c) Keys must be integers

d) When duplicate keys encountered, the last assignment wins

Check Answer

Answer: c
Keys of a dictionary may be any data type that is immutable.

22. What will be the output of the following Python code?

a={1:5,2:3,3:4}

a.pop(3)

print(a)

a) {1: 5}

b) {1: 5, 2: 3}

c) Error, syntax error for pop() method

d) {1: 5, 3: 4}

Check Answer

Answer: b
pop() method removes the key-value pair for the key mentioned in the pop() method.

23. What will be the output of the following Python code?

a={1:5,2:3,3:4}

print(a.pop(4,9))

a) 9

b) 3

c) Too many arguments for pop() method

d) 4

Check Answer

Answer: a
pop() method returns the value when the key is passed as an argument and otherwise returns the default value(second argument) if the key isn’t present in the dictionary.

24. What will be the output of the following Python code?

a={1:”A”,2:”B”,3:”C”}

for i in a:

print(i,end=” “)

a) 1 2 3

b) ‘A’ ‘B’ ‘C’

c) 1 ‘A’ 2 ‘B’ 3 ‘C’

d) Error, it should be: for i in a.items():

Check Answer

Answer: a
The variable i iterates over the keys of the dictionary and hence the keys are printed.

25. What will be the output of the following Python code?

>>> a={1:”A”,2:”B”,3:”C”}

>>> a.items()

a) Syntax error

b) dict_items([(‘A’), (‘B’), (‘C’)])

c) dict_items([(1,2,3)])

d) dict_items([(1, ‘A’), (2, ‘B’), (3, ‘C’)])

Check Answer

Answer: d
The method items() returns list of tuples with each tuple having a key-value pair.

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

Check Answer

Answer: c
More than one key can have the same value.

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

Check Answer

Answer: c
del deletes the entire dictionary and any further attempt to access it will throw an error.

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

Check Answer

Answer: a
The method popitem() removes a random key-value pair.

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

Check Answer

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

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

Check Answer

Answer: c
The above piece of code basically finds the sum of the values of keys.

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