MCQ

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

D
Function max() is used to find the maximum value among -2, -5, and false. Since false equals to 0, we are left with min(0, 1, 5). So the result is 0 (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

D
Tuples are immutable and do not have the “append” method. An exception is thrown in this case.
 

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

C
 

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

C
 

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

C
 

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

B
It will look for the pattern at the start and return None if it is not found.
 

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

A
It will search for the pattern at any position in a String.
 

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

C
 

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.

B
 

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’)

D
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *