Engineering Questions with Answers - Multiple Choice Questions

Python MCQs

1 - Question

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: More than one key can have the same value.




2 - Question

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: del deletes the entire dictionary and any further attempt to access it will throw an error.




3 - Question

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 method popitem() removes a random key-value pair.




4 - Question

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




5 - Question

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: The above piece of code basically finds the sum of the values of keys.




6 - Question

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: Dictionary in a dictionary can exist.




7 - Question

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: In the second line of code, the dictionary becomes an empty dictionary. Thus, length=0.




8 - Question

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: After the key-value pair of 1:’A’ is deleted, the key-value pair of 1:’D’ is added.




9 - Question

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




10 - Question

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: Mutable members can be used as the values of the dictionary but they cannot be used as the keys of the dictionary.




11 - Question

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])
>>> 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 statement a=collections.OrderedDict() generates a dictionary with the number as the key and the count of times the number appears as the value.




12 - Question

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

View Answer

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




13 - Question

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

View Answer

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




14 - Question

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])
>>> 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: a|b returns the pair of keys and the highest recurring value.




15 - Question

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])
>>> 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: a&b returns the pair of keys and the lowest recurring value.

Get weekly updates about new MCQs and other posts by joining 18000+ community of active learners