Python MCQ and Answers – Part 20
This collection of Python Multiple Choice Questions and Answers (MCQs): Quizzes & Practice Tests with Answer.
1. What is the output of the following code?
min(max(False,-2,-5), 1,5)
A -5
B -2
C 1
D False
2. What is the output of the following code?
>>> t = (1, 2, 3) >>> t.append( (4, 5, 6) ) >>>print len(t)
A 3
B 4
C 6
D Error
3. What is the output of the following code?
if None: print(“Lorem Ipsum”)
A Lorem Ipsum
B False
C The code doesn’t display anything
D Syntax error
4. In Python, “for” and “while” loops can have an optional “else” statement?
A Only the “for” loop can have an optional “else” statement
B Only the “while” loop can have an optional “else” statement
C Both loops can have an optional “else” statement
D Loops cannot have other statements in Python
5. What is the output of the following code?
i = s = 0 while i <= 3: s += i i = i+1 print(s)
A 3
B 4
C 6
D 0
6. What does the “re.match” function do?
A Match a pattern at any position in the string
B Matches a pattern at the start of the string
C This function does not exist
D None of the above
7. What does the “re.search” function do?
A Match a pattern at any position in the string
B Matches a pattern at the start of the string
C This function does not exist
D None of the above
8. What is the output of the following code?
while 2 == 2: print('2')
A 2 is displayed once
B 2 is displayed twice
C 2 is displayed infinitely
D Syntax error
9. Is it better to use “for” loop rather than “while” if you are iterating through a sequence (like: list)?
A No, you better use “While” loop
B Yes, it is better to use “for” loop
C No, you cannot iterate through a sequence using the “while” loop.
D No, you cannot iterate through a sequence using a loop.
10. What is the output of the following code?
import re str = 'welcome to stackhowto' matched = re.match(r'(.*) (.*?) (.*)', str) print(matched.groups())
A welcome, to, stackhowto
B (welcome, to, stackhowto)
C ‘welcome to stackhowto’
D (‘welcome’, ‘to’, ‘stackhowto’)