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
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}
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
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():
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’)])
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
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
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
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
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
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.