Python MCQ’s on Regular Expressions: Set 2

11. The character Dot (that is, ‘.’) in the default mode, matches any character other than _____________

a) caret

b) ampersand

c) percentage symbol

d) newline

Check Answer

Answer: d
The character Dot (that is, ‘,’) in the default mode, matches any character other than newline. If DOTALL flag is used, then it matches any character other than newline.

 

12. The expression a{5} will match _____________ characters with the previous regular expression.

a) 5 or less

b) exactly 5

c) 5 or more

d) exactly 4

Check Answer

Answer: b
The character {m} is used to match exactly m characters to the previous regular expression. Hence the expression a{5} will match exactly 5 characters and not less than that.

 

13. ________ matches the start of the string.

________ matches the end of the string.

a) ‘^’, ‘$’

b) ‘$’, ‘^’

c) ‘$’, ‘?’

d) ‘?’, ‘^’

Check Answer

Answer: a
‘^’ (carat) matches the start of the string.
‘$’ (dollar sign) matches the end of the string.

 

14. Which of the following will result in an error?

a)

>>> p = re.compile(“d”)

>>> p.search(“door”)

b) >>> p = re.escape(‘hello’)

c) >>> p = re.subn()

d) >>> p = re.purge()

Check Answer

Answer: c
The function re.subn() will result in an error. This is because subn() requires 3 positional arguments while we have entered none.

 

15. What will be the output of the following Python code?

re.split(‘\W+’, ‘Hello, hello, hello.’)

a) [‘Hello’, ‘hello’, ‘hello.’]

b) [‘Hello, ‘hello’, ‘hello’]

c) [‘Hello’, ‘hello’, ‘hello’, ‘.’]

d) [‘Hello’, ‘hello’, ‘hello’, ”]

Check Answer

Answer: d
In the code shown above, the function split() splits the string based on the pattern given as an argument in the parenthesis. Note: split will never split a string on an empty pattern match. Hence the output of this code is: [‘Hello’, ‘hello’, ‘hello’, ”].

 

16. What will be the output of the following Python function?

re.findall(“hello world”, “hello”, 1)

a) [“hello”]

b) [ ]

c) hello

d) hello world

Check Answer

Answer: b
The function findall returns the word matched if and only if both the pattern and the string match completely, that is, they are exactly the same. Observe the example shown below:
>>> re.findall(“hello”, “hello”, 1) The output is: [‘hello’] Hence the output of the code shown in this question is [].

 

17. Choose the function whose output can be: <_sre.SRE_Match object; span=(4, 8), match=’aaaa’>.

a) >>> re.search(‘aaaa’, “alohaaaa”, 0)

b) >>> re.match(‘aaaa’, “alohaaaa”, 0)

c) >>> re.match(‘aaa’, “alohaaa”, 0)

d) >>> re.search(‘aaa’, “alohaaa”, 0)

Check Answer

Answer: a
The output shown above is that of a search function, whose pattern is ‘aaaa’ and the string is that of 8 characters. The only option which matches all these criteria is:
>>> re.search(‘aaaa’, “alohaaaa”, 0)

 

18. Which of the following functions clears the regular expression cache?

a) re.sub()

b) re.pos()

c) re.purge()

d) re.subn()

Check Answer

Answer: c
The function which clears the regular expression cache is re.purge(). Note that this function takes zero positional arguments.

 

19. What will be the output of the following Python code?

import re

re.ASCII

a) 8

b) 32

c) 64

d) 256

Check Answer

Answer: d
The expression re.ASCII returns the total number of ASCII characters that are present, that is 256. This can also be abbreviated as re.A, which results in the same output (that is, 256).

 

20. Which of the following functions results in case insensitive matching?

a) re.A

b) re.U

c) re.I

d) re.X

Check Answer

Answer: c
The function re.I (that is, re.IGNORECASE) results in case-insensitive matching. That is, expressions such as [A-Z] will match lowercase characters too.

 

Page  1  2  3  4  5  6

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.

Python MCQs on Variables and Operators Python MCQs on Precedence and Associativity
Python MCQs on Data Type Python MCQs on Boolean
Python MCQs on Bitwise Operators Python MCQs on Formatting and Advance Formatting
Python MCQs on Decorators Python MCQs on While and For Loops
Python MCQs on List Python MCQs on List Comprehension
Python MCQs on String Python MCQs on Tuple
Python MCQs on SET Python MCQs on Dictionary
Python MCQs on Functions Python MCQs on Argument Parsing
Python MCQs on Global and Local Variables Python MCQs on Recursion
Python MCQs on Mapping Functions Python MCQs on Modules
Python MCQs on Regular Expressions Python MCQs on Files
Python MCQs on Overloading Python MCQs on Classes and Objects
Python MCQs on Inheritance Python MCQs on Polymorphism and Encapsulation
Python MCQs on Exception Handling

Leave a Comment