Python MCQ’s on Regular Expressions: Set 5

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

re.match(‘sp(.*)am’, ‘spam’)

a) <_sre.SRE_Match object; span=(1, 4), match=’spam’>

b) <_sre.SRE_Match object; span=(0, 4), match=’spam’>

c) No output

d) Error

Check Answer

Answer: b
The code shown above demonstrates the function re.match, combined with a special character. The output of the code shown is: <_sre.SRE_Match object; span=(0, 4), match=’spam’>

 

42. Which of the following special characters represents a comment (that is, the contents of the parenthesis are simply ignores)?

a) (?:…)

b) (?=…)

c) (?!…)

d) (?#…)

Check Answer

Answer: d
The special character (?#…) represent a comment, that is, the contents of the parenthesis are simply ignored.

 

43. Which of the codes shown below results in a match?

a) re.match(‘George(?=Washington)’, ‘George Washington’)

b) re.match(‘George(?=Washington)’, ‘George’)

c) re.match(‘George(?=Washington)’, ‘GeorgeWashington’)

d) re.match(‘George(?=Washington)’, ‘Georgewashington’)

Check Answer

Answer: c
The code shown above demonstrates the use of the function re.match, along with the special character ?=. This results in a match only when ‘George’ is immediately followed by ‘Washington’. Also, we have not used the module to ignore case. Hence the match is case-sensitive. Therefore the only option which results in a match is:
re.match(‘George(?=Washington)’, ‘GeorgeWashington’)

 

 

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

re.split(r'(a)(t)’, ‘Maths is a difficult subject’)

a) [‘M a t h s i s a d i f f i c u l t s u b j e c t’]

b) [‘Maths’, ‘is’, ‘a’, ‘difficult’, ‘subject’]

c) ‘Maths is a difficult subject’

d) [‘M’, ‘a’, ‘t’, ‘hs is a difficult subject’]

Check Answer

Answer: d
The code shown above demonstrates the use of the function re.match. The first argument of this function specifies the pattern. Since the pattern contains groups, those groups are incorporated in the resultant list as well. Hence the output of the code shown above is [‘M’, ‘a’, ‘t’, ‘hs is a difficult subject’].

 

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

CODE 1

>>> re.split(r'(a)(t)’, ‘The night sky’)

CODE 2

>>> re.split(r’\s+’, ‘The night sky’)

a) True

b) False

Check Answer

Answer: b
The output of the first code is: [‘The night sky’] whereas the output of the second code is:[‘The’, ‘night’, ‘sky’]. Clearly, the outputs of the two codes are different. Hence the statement given above is a false one.

 

 

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

import re

s = ‘abc123 xyz666 lmn-11 def77’

re.sub(r’\b([a-z]+)(\d+)’, r’\2\1:’, s)

a) ‘123abc: 666xyz: lmn-11 77def:’

b) ‘77def: lmn-11: 666xyz: 123abc’

c) ‘abc123:’, ‘xyz666:’, ‘lmn-11:’, ‘def77:’

d) ‘abc123: xyz666: lmn-11: def77’

Check Answer

Answer: a
The function re.sub returns a string produced by replacing every non overlapping occurrence of the first argument with the second argument in the third argument. Hence the output is: ‘123abc: 666xyz: lmn-11 77def:’

 

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

re.subn(‘A’, ‘X’, ‘AAAAAA’, count=4)

a) ‘XXXXAA, 4’

b) (‘AAAAAA’, 4)

c) (‘XXXXAA’, 4)

d) ‘AAAAAA, 4’

Check Answer

Answer: c
The line of code shown above demonstrates the function re.subn. This function is very similar to the function re.sub except that in the former, a tuple is returned instead of a string. The output of the code shown above is: (‘XXXXAA’, 4).

 

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

n = re.sub(r’\w+’, ‘Hello’, ‘Cats and dogs’)

a)

Hello

Hello

Hello

b) ‘Hello Hello Hello’

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

d) (‘Hello’, ‘Hello’, ‘Hello’)

Check Answer

Answer: b
The code shown above demonstrates the function re.sub. Since the string given as an argument consists of three words. The output of the code is: ‘Hello Hello Hello’. Had the string consisted of 4 words, the output would be: ‘Hello Hello Hello Hello’

 

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

w = re.compile(‘[A-Za-z]+’)

w.findall(‘It will rain today’)

a) ‘It will rain today’

b) (‘It will rain today’)

c) [‘It will rain today’]

d) [‘It’, ‘will’, ‘rain’, ‘today’]

Check Answer

Answer: d
The code shown above demonstrates the function re.findall. Since all the words in the string match the criteria, the output of the code is: [‘It’, ‘will’, ‘rain’, ‘today’].

 

50. In the functions re.search.start(group) and re.search.end(group), if the argument groups not specified, it defaults to __________

a) Zero

b) None

c) One

d) Error

Check Answer

Answer: a
In the functions re.search.start(group) and re.search.end(group), if the argument groups not specified, it defaults to Zero.

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