Python MCQ’s on Regular Expressions: Set 6

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

re.split(r’\s+’, ‘Chrome is better than explorer’, maxspilt=3)

a) [‘Chrome’, ‘is’, ‘better’, ‘than’, ‘explorer’]

b) [‘Chrome’, ‘is’, ‘better’, ‘than explorer’]

c) (‘Chrome’, ‘is’, ‘better’, ‘than explorer’)

d) ‘Chrome is better’ ‘than explorer’

Check Answer

Answer: b
The code shown above demonstrates the use of the function re.split, including the use of maxsplit. Since maxsplit is equal to 3, the output of the code shown above is:[‘Chrome’, ‘is’, ‘better’, ‘than explorer’]

 

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

a=re.compile(‘[0-9]+’)

a.findall(‘7 apples and 3 mangoes’)

a) [‘apples’ ‘and’ ‘mangoes’]

b) (7, 4)

c) [‘7’, ‘4’]

d) Error

Check Answer

Answer: c
The code shown above demonstrates the use of the functions re.compile and re.findall. Since we have specified in the code that only digits from 0-9 be found, hence the output of this code is: [‘7’, ‘4’].

 

53. Which of the following functions returns a dictionary mapping group names to group numbers?

a) re.compile.group

b) re.compile.groupindex

c) re.compile.index

d) re.compile.indexgroup

Check Answer

Answer: b
The function re.compile.groupindex returns a dictionary mapping group names to group numbers.

 

54. Which of the following statements regarding the output of the function re.match is incorrect?

a) ‘pq*’ will match ‘pq’

b) ‘pq?’ matches ‘p’

c) ‘p{4}, q’ does not match ‘pppq’

d) ‘pq+’ matches ‘p’

Check Answer

Answer: d
All of the above statements are correct except that ‘pq+’ match ‘p’. ‘pq+’ will match ‘p’ followed by any non-zero number of q’s, but it will not match ‘p’.

 

55. The following Python code snippet results in an error.

c=re.compile(r'(\d+)(\[A-Z]+)([a-z]+)’)

c.groupindex

a) True

b) False

Check Answer

Answer: b
In the code shown above, none of the group names match the group numbers. In such a case, no error is thrown. The output of the code is an empty dictionary, that is, {}.

 

56. Which of the following functions does not accept any argument?

a) re.purge

b) re.compile

c) re.findall

d) re.match

Check Answer

Answer: a
The function re.purge is used to clear the cache and it does not accept any arguments.

 

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

a = re.compile(‘0-9’)

a.findall(‘3 trees’)

a) []

b) [‘3’]

c) Error

d) [‘trees’]

Check Answer

Answer: c
The output of the code shown above is an empty list. This is due to the way the arguments have been passed to the function re.compile. Carefully read the code shown below in order to understand the correct syntax:
>>> a = re.compile(‘[0-9]’)
>>> a.findall(‘3 trees’)
[‘3’].

 

58. Which of the following lines of code will not show a match?

a) >>> re.match(‘ab*’, ‘a’)

b) >>> re.match(‘ab*’, ‘ab’)

c) >>> re.match(‘ab*’, ‘abb’)

d) >>> re.match(‘ab*’, ‘ba’)

Check Answer

Answer: d
In the code shown above, ab* will match to ‘a’ or ‘ab’ or ‘a’ followed by any number of b’s. Hence the only line of code from the above options which does not result in a match is:
>>> re.match(‘ab*’, ‘ba’).

 

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

m = re.search(‘a’, ‘The blue umbrella’)

m.re.pattern

a) {}

b) ‘The blue umbrella’

c) ‘a’

d) No output

Check Answer

Answer: c
The PatternObject is used to produce the match. The real regular expression pattern string must be retrieved from the PatternObject’s pattern method. Hence the output of this code is: ‘a’.

 

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

re.sub(‘Y’, ‘X’, ‘AAAAAA’, count=2)

a) ‘YXAAAA’

b) (‘YXAAAA’)

c) (‘AAAAAA’)

d) ‘AAAAAA’

Check Answer

Answer: d
The code shown above demonstrates the function re.sub, which returns a string. The pattern specified is substituted in the string and returned. Hence the output of the code shown above is: ‘AAAAAA’.

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