Regular Expression Quiz: MCQs on Regular Expression

Regular Expression Quiz. MCQs Quiz in Python Programming on Regular Expressions.

Regular Expression Quiz

 

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)

 

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’]

 

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

 

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

 

Regular Expression Quiz

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

a) re.purge
b) re.compile
c) re.findall
d) re.match

 

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’]

 

The following Python code snippet results in an error ??

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

a) True
b) False

 

What will be the output of the following Python ??

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

 

Regular Expression Quiz

What is the output of ??

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

a) ‘XXXXAA, 4’
b) (‘AAAAAA’, 4)
c) (‘XXXXAA’, 4)
d) ‘AAAAAA, 4’

 

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’

 

What will be the output of the following ??

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’

 

What will be the output of the ??

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’]

 

Regular Expression Quiz

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

 

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

 

What will be the output of ??

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’]

Read More

Leave a Comment

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

error: Content is protected !!