Python MCQ’s on Regular Expressions: Set 3

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

re.compile(‘hello’, re.X)

a) [‘h’, ‘e’, ‘l’, ‘l’, ‘o’]

b) re.compile(‘hello’, re.VERBOSE)

c) Error

d) Junk value

Check Answer

Answer: b
The compile function compiles a pattern of regular expression into an object of regular expression. Re.X is a flag which is also used as re.VERBOSE. Hence the output of this code is: re.compile(‘hello’, re.VERBOSE).

 

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

re.split(‘[a-c]’, ‘0a3B6’, re.I)

a) Error

b) [‘a’, ‘B’]

c) [‘0’, ‘3B6’]

d) [‘a’]

Check Answer

Answer: c
The function re.split() splits the string on the basis of the pattern given in the parenthesis. Since we have used the flag e.I (that is, re.IGNORECASE), the output is: [‘0’, ‘3B6’].

 

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

re.sub(‘morning’, ‘evening’, ‘good morning’)

a) ‘good evening’

b) ‘good’

c) ‘morning’

d) ‘evening’

Check Answer

Answer: a
The code shown above first searches for the pattern ‘morning’ in the string ‘good morning’ and then replaces this pattern with ‘evening’. Hence the output of this code is: ‘good evening’.

 

24. The function re.error raises an exception if a particular string contains no match for the given pattern.

a) True

b) False

Check Answer

Answer: b
The function re.error raises an exception when a string passed to one of its functions here is not a valid regular expression. It does not raise an exception if a particular string does not contain a match for the given pattern.

 

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

re.escape(‘new**world’)

a) ‘new world’

b) ‘new\\*\\*world’

c) ‘**’

d) ‘new’, ‘*’, ‘*’, ‘world’

Check Answer

Answer: b
The function re.escape escapes all the characters in the pattern other than ASCII letters and numbers. Hence the output of the code shown above is: ‘new\\*\\*world’.

 

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

re.fullmatch(‘hello’, ‘hello world’)

a) No output

b) []

c) <_sre.SRE_Match object; span=(0, 5), match=’hello’>

d) Error

Check Answer

Answer: a
The function re.fullmatch applies the pattern to the entire string and returns an object if match is found and none if match in not found. In the code shown above, match is not found. Hence there is no output.

 

27. Choose the option wherein the two choices do not refer to the same option.

a)

re.I

re.IGNORECASE

b)

re.M

re.MULTILINE

c)

re.X

re.VERBOSE

d)

re.L

re.LOWERCASE

Check Answer

Answer: d
The function re.L is also written as re.LOCALE. There is no function such as re.LOWERCASE in the re module of Python.

 

28. The difference between the functions re.sub and re.subn is that re.sub returns a _______________ whereas

re.subn returns a __________________

a) string, list

b) list, tuple

c) string, tuple

d) tuple, list

Check Answer

Answer: c
The difference the functions re.sub and re.subn is that re.sub returns a string whereas re.subn returns a tuple.

 

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

re.split(‘mum’, ‘mumbai*’, 1)

a) Error

b) [”, ‘bai*’]

c) [”, ‘bai’]

d) [‘bai*’]

Check Answer

Answer: b
The code shown above splits the string based on the pattern given as an argument. Hence the output of the code is: [”, ‘bai*’].

 

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

re.findall(‘good’, ‘good is good’)

re.findall(‘good’, ‘bad is good’)

a)

[‘good’, ‘good’] [‘good’]

b)

(‘good’, ‘good’)

(good)

c)

(‘good’)

(‘good’)

d)

[‘good’] [‘good’]
Check Answer

Answer: a
The function findall returns a list of all the non overlapping matches in a string. Hence the output of the first function is: [‘good’, ‘good’] and that of the second function is: [‘good’].

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