Regular Expressions In Python — Part 2 MCQs with Explanations
Practice high-yield Programming & Software Development MCQs on Regular Expressions In Python — Part 2 with worked academic solutions for FPSC, PPSC, NTS, NITB, PTS.
Questions
40
Difficulty Level
Exam-Level
Target Exams
FPSC, PPSC, NTS...
Estimated Time
48 mins
Exam-Level FPSC PPSC NTS NITB PTS
Verified for Commission Syllabus • Oct 14, 2026
40
Verified Practice Questions
Interactive test mode with detailed academic worked solutions
Live Progress|0/40 answered
✓0 correct
✗0 wrong
0% score
🎉 Set complete! All 40 questions answered.
Question 1 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
In the functions re.search.start(group) and re.search.end(group), if the argument groups not specified, it defaults to . . . . . . . .
Correct Answer: Option A — Zero
Detailed Academic Explanation:
Zero is the correct answer to “In the functions re.search.start(group) and re.search.end(group), if the argument groups not specified, it defaults to . . . . . . . .”. This question is standard in competitive examination syllabi for Regular Expressions In Python.
💡Key Conceptual Anchor:Regular Expressions In Python
Question 2 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is a character class in regular expressions?
Correct Answer: Option A — A set of characters enclosed in square brackets that matches any character in the set
Detailed Academic Explanation:
A set of characters enclosed in square brackets that matches any character in the set is the correct answer to “What is a character class in regular expressions”. This question is standard in competitive examination syllabi for Regular Expressions In Python.
💡Key Conceptual Anchor:Regular Expressions In Python
Question 3 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following Python code? re.split(r'(a)(t)', 'Maths is a difficult subject')
Correct Answer: Option D — ['M', 'a', 't', 'hs is a difficult subject']
Detailed Academic Explanation:
['M', 'a', 't', 'hs is a difficult subject'] is the correct answer to “What will be the output of the following Python code? re.split(r'(a)(t)', 'Maths is a difficult subject')”. This question is standard in competitive examination syllabi for Regular Expressions In Python.
💡Key Conceptual Anchor:Regular Expressions In Python
Question 4 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following code: import re pattern = re.compile(r'b[A-Z]+b') result = pattern.findall('Hello, WORLD!') print(result)
Correct Answer: Option A — ['Hello', 'WORLD']
Detailed Academic Explanation:
['Hello', 'WORLD'] is the correct answer to “What will be the output of the following code: import re pattern = re.compile(r'b[A-Z]+b') result = pattern.findall('Hello, WORLD!') print(result)”. This question is standard in competitive examination syllabi for Regular Expressions In Python.
💡Key Conceptual Anchor:Regular Expressions In Python
Question 5 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following Python code? n = re.sub(r'\w+', 'Hello', 'Cats and dogs')
Correct Answer: Option B — ‘Hello Hello Hello’
Detailed Academic Explanation:
‘Hello Hello Hello’ is the correct answer to “What will be the output of the following Python code? n = re.sub(r'\w+', 'Hello', 'Cats and dogs')”. This question is standard in competitive examination syllabi for Regular Expressions In Python.
💡Key Conceptual Anchor:Regular Expressions In Python
Question 6 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the purpose of the re.sub() function in Python?
Correct Answer: Option A — It replaces occurrences of a pattern with a specified string
Detailed Academic Explanation:
It replaces occurrences of a pattern with a specified string is the correct answer to “What is the purpose of the re.sub() function in Python”. This question is standard in competitive examination syllabi for Regular Expressions In Python.
💡Key Conceptual Anchor:Regular Expressions In Python
Question 7 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the purpose of the re.MULTILINE flag in Python?
Correct Answer: Option A — It makes the ^ and $ characters match the beginning and end of each line
Detailed Academic Explanation:
It makes the ^ and $ characters match the beginning and end of each line is the correct answer to “What is the purpose of the re.MULTILINE flag in Python”. This question is standard in competitive examination syllabi for Regular Expressions In Python.
💡Key Conceptual Anchor:Regular Expressions In Python
Question 8 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
The difference between the functions re.sub and re.subn is that re.sub returns a . . . . . . . . whereas re.subn returns a . . . . . . . .
Correct Answer: Option C — string, tuple
Detailed Academic Explanation:
string, tuple is the correct answer to “The difference between the functions re.sub and re.subn is that re.sub returns a . . . . . . . . whereas re.subn returns a . . . . . . . .”. This question is standard in competitive examination syllabi for Regular Expressions In Python.
💡Key Conceptual Anchor:Regular Expressions In Python
Question 9 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following Python code? re.split('mum', 'mumbai*', 1)
Correct Answer: Option B — [", 'bai*']
Detailed Academic Explanation:
[", 'bai*'] is the correct answer to “What will be the output of the following Python code? re.split('mum', 'mumbai*', 1)”. This question is standard in competitive examination syllabi for Regular Expressions In Python.
💡Key Conceptual Anchor:Regular Expressions In Python
Question 10 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
The special character B matches the empty string, but only when it is . . . . . . . .
Correct Answer: Option B — not at the beginning or end of a word
Detailed Academic Explanation:
not at the beginning or end of a word is the correct answer to “The special character B matches the empty string, but only when it is . . . . . . . .”. This question is standard in competitive examination syllabi for Regular Expressions In Python.
💡Key Conceptual Anchor:Regular Expressions In Python
Question 11 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which of the following functions returns a dictionary mapping group names to group numbers?
Correct Answer: Option B — re.compile.groupindex
Detailed Academic Explanation:
re.compile.groupindex is the correct answer to “Which of the following functions returns a dictionary mapping group names to group numbers”. This question is standard in competitive examination syllabi for Regular Expressions In Python.
💡Key Conceptual Anchor:Regular Expressions In Python
Question 12 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following Python code? re.split('[a-c]', '0a3B6', re.I)
Correct Answer: Option C — ['0', '3B6']
Detailed Academic Explanation:
['0', '3B6'] is the correct answer to “What will be the output of the following Python code? re.split('[a-c]', '0a3B6', re.I)”. This question is standard in competitive examination syllabi for Regular Expressions In Python.
💡Key Conceptual Anchor:Regular Expressions In Python
Question 13 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
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)
Correct Answer: Option A — '123abc: 666xyz: lmn-11 77def:'
Detailed Academic Explanation:
'123abc: 666xyz: lmn-11 77def:' is the correct answer to “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)”. This question is standard in competitive examination syllabi for Regular Expressions In Python.
💡Key Conceptual Anchor:Regular Expressions In Python
Question 14 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which of the following statements regarding the output of the function re.match is incorrect?
Correct Answer: Option D — 'pq+' matches 'p'
Detailed Academic Explanation:
'pq+' matches 'p' is the correct answer to “Which of the following statements regarding the output of the function re.match is incorrect”. This question is standard in competitive examination syllabi for Regular Expressions In Python.
💡Key Conceptual Anchor:Regular Expressions In Python
Question 15 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
How can you replace a matched pattern with a new string using regular expressions in Python?
Correct Answer: Option A — By using the sub() function from the re module
Detailed Academic Explanation:
By using the sub() function from the re module is the correct answer to “How can you replace a matched pattern with a new string using regular expressions in Python”. This question is standard in competitive examination syllabi for Regular Expressions In Python.
💡Key Conceptual Anchor:Regular Expressions In Python
Question 16 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which of the following pattern matching modifiers permits whitespace and comments inside the regular expression?
Correct Answer: Option D — re.X
Detailed Academic Explanation:
re.X is the correct answer to “Which of the following pattern matching modifiers permits whitespace and comments inside the regular expression”. This question is standard in competitive examination syllabi for Regular Expressions In Python.
💡Key Conceptual Anchor:Regular Expressions In Python
Question 17 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
How can you match zero or more occurrences of a pattern in regular expressions?
Correct Answer: Option A — By using the * symbol after the pattern
Detailed Academic Explanation:
By using the * symbol after the pattern is the correct answer to “How can you match zero or more occurrences of a pattern in regular expressions”. This question is standard in competitive examination syllabi for Regular Expressions In Python.
💡Key Conceptual Anchor:Regular Expressions In Python
Question 18 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following Python code? re.split(r'(n\d)=', 'n1=3.1, n2=5, n3=4.565')
[", 'n1', '3.1, ', 'n2', '5, ', 'n3', '4.565'] is the correct answer to “What will be the output of the following Python code? re.split(r'(n\d)=', 'n1=3.1, n2=5, n3=4.565')”. This question is standard in competitive examination syllabi for Regular Expressions In Python.
💡Key Conceptual Anchor:Regular Expressions In Python
Question 19 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Choose the option wherein the two choices do not refer to the same option.
Correct Answer: Option D — re.L re.LOWERCASE
Detailed Academic Explanation:
re.L re.LOWERCASE is the correct answer to “Choose the option wherein the two choices do not refer to the same option.”. This question is standard in competitive examination syllabi for Regular Expressions In Python.
💡Key Conceptual Anchor:Regular Expressions In Python
Question 20 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following code: import re result = re.findall(r'd{1,3}', 'There are 25 apples and 300 oranges.') print(result)
Correct Answer: Option A — ['25', '300']
Detailed Academic Explanation:
['25', '300'] is the correct answer to “What will be the output of the following code: import re result = re.findall(r'd{1,3}', 'There are 25 apples and 300 oranges.') print(result)”. This question is standard in competitive examination syllabi for Regular Expressions In Python.
💡Key Conceptual Anchor:Regular Expressions In Python
Question 21 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the output of the following code: import re result = re.sub(r's', '-', 'This is a test.') print(result)
Correct Answer: Option A — This-is-a-test.
Detailed Academic Explanation:
This-is-a-test. is the correct answer to “What is the output of the following code: import re result = re.sub(r's', '-', 'This is a test.') print(result)”. This question is standard in competitive examination syllabi for Regular Expressions In Python.
💡Key Conceptual Anchor:Regular Expressions In Python
Question 22 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following Python code? sentence = 'we are humans' matched = re.match(r'(.*) (.*?) (.*)', sentence) print(matched.group())
Correct Answer: Option D — 'we are humans'
Detailed Academic Explanation:
'we are humans' is the correct answer to “What will be the output of the following Python code? sentence = 'we are humans' matched = re.match(r'(.*) (.*?) (.*)', sentence) print(matched.group())”. This question is standard in competitive examination syllabi for Regular Expressions In Python.
💡Key Conceptual Anchor:Regular Expressions In Python
Question 23 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which of the following functions does not accept any argument?
Correct Answer: Option A — re.purge
Detailed Academic Explanation:
re.purge is the correct answer to “Which of the following functions does not accept any argument”. This question is standard in competitive examination syllabi for Regular Expressions In Python.
💡Key Conceptual Anchor:Regular Expressions In Python
Question 24 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
How can you match either of two patterns in regular expressions using the | operator?
Correct Answer: Option A — By using `pattern1
Detailed Academic Explanation:
By using `pattern1 is the correct answer to “How can you match either of two patterns in regular expressions using the | operator”. This question is standard in competitive examination syllabi for Regular Expressions In Python.
💡Key Conceptual Anchor:Regular Expressions In Python
Question 25 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the purpose of the re.search() function in Python?
Correct Answer: Option A — It searches for a match anywhere in the string
Detailed Academic Explanation:
It searches for a match anywhere in the string is the correct answer to “What is the purpose of the re.search() function in Python”. This question is standard in competitive examination syllabi for Regular Expressions In Python.
💡Key Conceptual Anchor:Regular Expressions In Python
Question 26 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following Python code? re.findall('good', 'good is good') re.findall('good', 'bad is good')
Correct Answer: Option A — [‘good’, ‘good’] [‘good’]
Detailed Academic Explanation:
[‘good’, ‘good’] [‘good’] is the correct answer to “What will be the output of the following Python code? re.findall('good', 'good is good') re.findall('good', 'bad is good')”. This question is standard in competitive examination syllabi for Regular Expressions In Python.
💡Key Conceptual Anchor:Regular Expressions In Python
Question 27 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
How can you match the end of a string using regular expressions?
Correct Answer: Option A — By using the $ symbol
Detailed Academic Explanation:
By using the $ symbol is the correct answer to “How can you match the end of a string using regular expressions”. This question is standard in competitive examination syllabi for Regular Expressions In Python.
💡Key Conceptual Anchor:Regular Expressions In Python
Question 28 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following Python code? re.fullmatch('hello', 'hello world')
Correct Answer: Option A — No output
Detailed Academic Explanation:
No output is the correct answer to “What will be the output of the following Python code? re.fullmatch('hello', 'hello world')”. This question is standard in competitive examination syllabi for Regular Expressions In Python.
💡Key Conceptual Anchor:Regular Expressions In Python
Question 29 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following Python code? a=re.compile('[0-9]+') a.findall('7 apples and 3 mangoes')
Correct Answer: Option C — ['7', '4']
Detailed Academic Explanation:
['7', '4'] is the correct answer to “What will be the output of the following Python code? a=re.compile('[0-9]+') a.findall('7 apples and 3 mangoes')”. This question is standard in competitive examination syllabi for Regular Expressions In Python.
💡Key Conceptual Anchor:Regular Expressions In Python
Question 30 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
The function of re.match is . . . . . . . .
Correct Answer: Option D — Matches a pattern at the start of the string
Detailed Academic Explanation:
Matches a pattern at the start of the string is the correct answer to “The function of re.match is . . . . . . . .”. This question is standard in competitive examination syllabi for Regular Expressions In Python.
💡Key Conceptual Anchor:Regular Expressions In Python
Question 31 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following Python code? import re s = "A new day" m = re.match(r'(.*)(.*?)', s) print(m.group(2)) print(m.group(0))
Correct Answer: Option A — No output A new day
Detailed Academic Explanation:
No output A new day is the correct answer to “What will be the output of the following Python code? import re s = "A new day" m = re.match(r'(.*)(.*?)', s) print(m.group(2)) print(m.group(0))”. This question is standard in competitive examination syllabi for Regular Expressions In Python.
💡Key Conceptual Anchor:Regular Expressions In Python
Question 32 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following Python code? s = 'welcome home' m = re.match(r'(.*)(.*?)', s) print(m.group())
Correct Answer: Option C — welcome home
Detailed Academic Explanation:
welcome home is the correct answer to “What will be the output of the following Python code? s = 'welcome home' m = re.match(r'(.*)(.*?)', s) print(m.group())”. This question is standard in competitive examination syllabi for Regular Expressions In Python.
💡Key Conceptual Anchor:Regular Expressions In Python
Question 33 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the output of the following code: import re pattern = re.compile(r'(d+)s(w+)') result = pattern.findall('There are 5 apples and 3 oranges.') print(result)
Correct Answer: Option A — [('5', 'apples'), ('3', 'oranges')]
Detailed Academic Explanation:
[('5', 'apples'), ('3', 'oranges')] is the correct answer to “What is the output of the following code: import re pattern = re.compile(r'(d+)s(w+)') result = pattern.findall('There are 5 apples and 3 oranges.') print(result)”. This question is standard in competitive examination syllabi for Regular Expressions In Python.
💡Key Conceptual Anchor:Regular Expressions In Python
Question 34 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
The following Python code snippet results in an error. c=re.compile(r'(\d+)(\[A-Z]+)([a-z]+)') c.groupindex
Correct Answer: Option B — False
Detailed Academic Explanation:
False is the correct answer to “The following Python code snippet results in an error. c=re.compile(r'(\d+)(\[A-Z]+)([a-z]+)') c.groupindex”. This question is standard in competitive examination syllabi for Regular Expressions In Python.
💡Key Conceptual Anchor:Regular Expressions In Python
Question 35 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following Python code? a = re.compile('0-9') a.findall('3 trees')
Correct Answer: Option C — Error
Detailed Academic Explanation:
Error is the correct answer to “What will be the output of the following Python code? a = re.compile('0-9') a.findall('3 trees')”. This question is standard in competitive examination syllabi for Regular Expressions In Python.
💡Key Conceptual Anchor:Regular Expressions In Python
Question 36 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
The output of the following two Python codes are the same. p = re.compile('hello') r = p.match('hello everyone') print(r.group(0)) r = re.match('hello', 'hello everyone') print(r.group(0))
Correct Answer: Option A — True
Detailed Academic Explanation:
True is the correct answer to “The output of the following two Python codes are the same. p = re.compile('hello') r = p.match('hello everyone') print(r.group(0)) r = re.match('hello', 'hello everyone') print(r.group(0))”. This question is standard in competitive examination syllabi for Regular Expressions In Python.
💡Key Conceptual Anchor:Regular Expressions In Python
Question 37 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which of the following functions creates a Python object?
Correct Answer: Option A — re.compile(str)
Detailed Academic Explanation:
re.compile(str) is the correct answer to “Which of the following functions creates a Python object”. This question is standard in competitive examination syllabi for Regular Expressions In Python.
💡Key Conceptual Anchor:Regular Expressions In Python
Question 38 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which of the following special characters represents a comment (that is, the contents of the parenthesis are simply ignores)?
Correct Answer: Option D — (?#...)
Detailed Academic Explanation:
(?#...) is the correct answer to “Which of the following special characters represents a comment (that is, the contents of the parenthesis are simply ignores)”. This question is standard in competitive examination syllabi for Regular Expressions In Python.
💡Key Conceptual Anchor:Regular Expressions In Python
Question 39 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following Python code? sentence = 'we are humans' matched = re.match(r'(.*) (.*?) (.*)', sentence) print(matched.groups())
Correct Answer: Option A — ('we', 'are', 'humans')
Detailed Academic Explanation:
('we', 'are', 'humans') is the correct answer to “What will be the output of the following Python code? sentence = 'we are humans' matched = re.match(r'(.*) (.*?) (.*)', sentence) print(matched.groups())”. This question is standard in competitive examination syllabi for Regular Expressions In Python.
💡Key Conceptual Anchor:Regular Expressions In Python
Question 40 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which of the following special characters matches a pattern only at the end of the string?
Correct Answer: Option C — Z
Detailed Academic Explanation:
Z is the correct answer to “Which of the following special characters matches a pattern only at the end of the string”. This question is standard in competitive examination syllabi for Regular Expressions In Python.
💡Key Conceptual Anchor:Regular Expressions In Python
✓Link copied to clipboard
Continue Your Practice
Advance Html — Part 1
Master the next curriculum module with 40 exam-standard MCQs.
💡Examiner's Advisory for Programming & Software Development
In competitive commission exams (FPSC, PPSC, KPPSC, SPSC, CSS, PMS), questions are frequently phrased with distractor options designed to catch rote learners. Whenever you answer an MCQ, read the Detailed Academic Explanation and Conceptual Anchor to understand why the remaining options are incorrect.