Python MCQ’s on Regular Expressions: Set 4

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

re.split(r'(n\d)=’, ‘n1=3.1, n2=5, n3=4.565’)

a) Error

b) [”, ‘n1’, ‘3.1, ‘, ‘n2’, ‘5, ‘, ‘n3’, ‘4.565’]

c) [‘n1’, ‘3.1, ‘, ‘n2’, ‘5, ‘, ‘n3’, ‘4.565’]

d) [‘3.1, ‘, ‘5, ‘, ‘4.565’]

Check Answer

Answer: b
In the snippet of code shown above, we extract the numbers as a list of floating point values, including the initial empty string. The example shown above demonstrate how groups in the regular expression influence the result of re.split. Hence the output of the code shown above is:
[”, ‘n1’, ‘3.1, ‘, ‘n2’, ‘5, ‘, ‘n3’, ‘4.565’].

 

32. The function of re.search is __________

a) Matches a pattern at the start of the string

b) Matches a pattern at the end of the string

c) Matches a pattern from any part of a string

d) Such a function does not exist

Check Answer

Answer: c
The re module of Python consists of a function re.search. It’s function is to match a pattern from anywhere in a string.

 

33. Which of the following functions creates a Python object?

a) re.compile(str)

b) re.assemble(str)

c) re.regex(str)

d) re.create(str)

Check Answer

Answer: a
The function re.compile(srt) compiles a pattern of regular expression into an object of regular expression. Hence re.compile(str) is the only function from the above options which creates an object.

 

34. Which of the following pattern matching modifiers permits whitespace and comments inside the regular expression?

a) re.L

b) re.S

c) re.U

d) re.X

Check Answer

Answer: d
The modifier re.X allows whitespace and comments inside the regular expressions.

 

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

s = ‘welcome home’

m = re.match(r'(.*)(.*?)’, s)

print(m.group())

a) (‘welcome’, ‘home’)

b) [‘welcome’, ‘home’]

c) welcome home

d) [‘welcome’ // ‘home’ ]

Check Answer

Answer: c
The code shown above shows the function re.match combined with the use of special characters. Hence the output of this code is: welcome home.

 

36. The function of re.match is ____________

a) Error

b) Matches a pattern anywhere in the string

c) Matches a pattern at the end of the string

d) Matches a pattern at the start of the string

Check Answer

Answer: d
The function of re.match matches a pattern at the start of the string.

 

37. The special character \B matches the empty string, but only when it is _____________

a) at the beginning or end of a word

b) not at the beginning or end of a word

c) at the beginning of the word

d) at the end of the word

Check Answer

Answer: b
The special character \B matches the empty string, but only when it is not at the beginning or end of a word.

 

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

import re

s = “A new day”

m = re.match(r'(.*)(.*?)’, s)

print(m.group(2))

print(m.group(0))

a)

No output

A new day

b)

No output

No output

c)

[‘A’, ‘new’, ‘day’]

(‘A’, ‘new’, ‘day’)

d)

Error

[‘A’, ‘new’, ‘day’]
Check Answer

Answer: a
The code shown above demonstrates the use of the function re.match, with different arguments given to the group method. Hence the first function does not return any output whereas the second function returns the output: A new day

 

39. Which of the following special characters matches a pattern only at the end of the string?

a) \B

b) \X

c) \Z

d) \A

Check Answer

Answer: c
\B matches a pattern which is not at the beginning or end of a string. \X refers to re.VERBOSE. \A matches a pattern only at the start of a string. \Z matches a pattern only at the end of a string.

 

40. The output of the following two Python codes are the same.

p = re.compile(‘hello’)

r = p.match(‘hello everyone’)

print(r.group(0))

r = re.match(‘hello’, ‘hello everyone’)

print(r.group(0))

a) True

b) False

Check Answer

Answer: a

The two codes shown above are equivalent. Both of these codes result in the same output, that is: hello. Hence this statement is true.

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