Regular Expression In Python: MCQs on Regular Expression

Regular Expression In Python. MCQs on the topic of Regular Expression In Python programming.

 

Regular Expression In Python

 

 

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

a) re.A
b) re.U
c) re.I
d) re.X

 

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

sentence = ‘horses are fast’
regex = re.compile(‘(?P<animal>\w+) (?P<verb>\w+) (?P<adjective>\w+)’)
matched = re.search(regex, sentence)
print(matched.group(2))

a) {‘animal’: ‘horses’, ‘verb’: ‘are’, ‘adjective’: ‘fast’}
b) (‘horses’, ‘are’, ‘fast’)
c) ‘horses are fast’
d) ‘are’

 

What will be the output of ??

import re
re.ASCII

a) 8
b) 32
c) 64
d) 256

 

What will be the output ??

sentence = ‘horses are fast’
regex = re.compile(‘(?P<animal>\w+) (?P<verb>\w+) (?P<adjective>\w+)’)
matched = re.search(regex, sentence)
print(matched.groups())

a) {‘animal’: ‘horses’, ‘verb’: ‘are’, ‘adjective’: ‘fast’}
b) (‘horses’, ‘are’, ‘fast’)
c) ‘horses are fast’
d) ‘are’

 

Regular Expression In Python

 

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

a) re.sub()
b) re.pos()
c) re.purge()
d) re.subn()

 

What will be the output of the following Python ??

sentence = ‘horses are fast’
regex = re.compile(‘(?P<animal>\w+) (?P<verb>\w+) (?P<adjective>\w+)’)
matched = re.search(regex, sentence)
print(matched.groupdict())

a) {‘animal’: ‘horses’, ‘verb’: ‘are’, ‘adjective’: ‘fast’}
b) (‘horses’, ‘are’, ‘fast’)
c) ‘horses are fast’
d) ‘are’

 

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)

 

What will be the output of the following ??

sentence = ‘we are humans’
matched = re.match(r'(.*) (.*?) (.*)’, sentence)
print(matched.group(2))

a) ‘are’
b) ‘we’
c) ‘humans’
d) ‘we are humans’

 

Regular Expression In Python

 

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

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

a) [“hello”]
b) [ ]
c) hello
d) hello world

 

What is the output of the following ??

sentence = ‘we are humans’
matched = re.match(r'(.*) (.*?) (.*)’, sentence)
print(matched.group())

a) (‘we’, ‘are’, ‘humans’)
b) (we, are, humans)
c) (‘we’, ‘humans’)
d) ‘we are humans’

 

What will be the output of the Python code ??

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

a) [‘Hello’, ‘hello’, ‘hello.’]
b) [‘Hello, ‘hello’, ‘hello’]
c) [‘Hello’, ‘hello’, ‘hello’, ‘.’]
d) [‘Hello’, ‘hello’, ‘hello’, ”]

 

What will be the output ??

sentence = ‘we are humans’
matched = re.match(r'(.*) (.*?) (.*)’, sentence)
print(matched.groups())

a) (‘we’, ‘are’, ‘humans’)
b) (we, are, humans)
c) (‘we’, ‘humans’)
d) ‘we are humans’

 

Regular Expression In Python

 

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()

 

What does the function re.search do ??

a) matches a pattern at the start of the string
b) matches a pattern at any position in the string
c) such a function does not exist
d) none of the mentioned

 

What matches the start of the string. What matches the end of the string ??

a) ‘^’, ‘$’
b) ‘$’, ‘^’
c) ‘$’, ‘?’
d) ‘?’, ‘^’

Leave a Comment

Your email address will not be published. Required fields are marked *

error: Content is protected !!