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
42. Which of the following special characters represents a comment (that is, the contents of the parenthesis are simply ignores)?
a) (?:…)
b) (?=…)
c) (?!…)
d) (?#…)
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’)
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’]
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
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’
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’
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’)
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’]
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
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.