Python MCQ and Answers – Part 3
This collection of Python Multiple Choice Questions and Answers (MCQs): Quizzes & Practice Tests with Answer.
1. Which of the following uses the id() function in Python?
A id() returns the object identifier
B An object does not have a unique identifier
C All the answers are true
D None of the above
2. What is the output of the following code?
def add(init = 5, *nbr, **key): c = init for n in nbr: c+=n for k in key: c+=key[k] return c print(add(100,2,2, x=20, y=10))
A 136
B 134
C 100
D 122
3. What is the output of the following code?
import re sentence = 'I am fine' regex = re.compile('(?P<subject>\w+) (?P<verb>\w+) (?P<adjective>\w+)') matched = re.search(regex, sentence) print(matched.groupdict())
A {‘subject’: ‘I’, ‘verb’: ‘am’, ‘adjective’: ‘fine’}
B (‘I’, ‘am’, ‘fine’)
C ‘I am fine’
D ‘am’
4. What is the output of the following code?
try: list = 2*[0]+2*[5] x = list[2] print('OK!') except IndexError: print('Block Except!') else: print('Block Else!') finally: print('Block Finally!')
A OK!
B Block Else!
C Block Finally!
D All the answers are true
5. Suppose list1 is [2, 3, 4, 5, 1, 20, 6], what is the value of list1 after this list1.pop(1)
?
A [2, 3, 4, 5, 20, 6]
B [2, 1, 4, 5, 1, 20, 6]
C [2, 3, 4, 5, 1, 20, 6, 1]
D [2, 4, 5, 1, 20, 6]
6. Suppose we have two sets (s1 and s2) then what is the output of S1 + S2
A Adds the elements of the two sets.
B Removes the duplicate elements and adds the two sets.
C Impossible to perform this type of operation.
D The output will be stored in S1.
7. Are String objects mutable?
A Yes.
B No.
8. Python is a compiled language?
A True
B False
9. Can the use of parentheses change the order of evaluation?
A True
B False
10. What is the correct syntax for reading from a text file stored in “c:\file.txt”?
A f = open('c:\file.txt', 'r')
B f = open(file='c:\\\file.txt', 'r')
C f = open.file('c:\\file.txt', 'r')
D f = open('c:\\file.txt', 'r')