Regular Expressions In Python — Part 1 MCQs with Explanations
Practice high-yield Programming & Software Development MCQs on Regular Expressions In Python — Part 1 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
What is a regular expression in Python?
Correct Answer: Option A — A sequence of characters that defines a search pattern
Detailed Academic Explanation:
A sequence of characters that defines a search pattern is the correct answer to “What is a regular expression in Python”. 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 the purpose of the re.compile() function in Python?
Correct Answer: Option A — It compiles a regular expression pattern into a regular expression object
Detailed Academic Explanation:
It compiles a regular expression pattern into a regular expression object is the correct answer to “What is the purpose of the re.compile() function in Python”. 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
Which module in Python supports regular expressions?
Correct Answer: Option A — re
Detailed Academic Explanation:
re is the correct answer to “Which module in Python supports regular expressions”. 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
How can you use regular expressions in Python?
Correct Answer: Option A — By using the re module
Detailed Academic Explanation:
By using the re module is the correct answer to “How can you use 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 5 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What does the match() function in the re module do?
Correct Answer: Option A — It searches for a match only at the beginning of the string
Detailed Academic Explanation:
It searches for a match only at the beginning of the string is the correct answer to “What does the match() function in the re module do”. 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 does the findall() function in the re module do?
Correct Answer: Option A — It finds all occurrences of a pattern in a string and returns them as a list
Detailed Academic Explanation:
It finds all occurrences of a pattern in a string and returns them as a list is the correct answer to “What does the findall() function in the re module do”. 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.IGNORECASE flag in Python?
Correct Answer: Option B — It compiles a regular expression
Detailed Academic Explanation:
It compiles a regular expression is the correct answer to “What is the purpose of the re.IGNORECASE 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
What is the output of the following code: import re result = re.sub(r'd+', 'number', 'There are 5 apples and 3 oranges.') print(result)
Correct Answer: Option A — There are number apples and number oranges.
Detailed Academic Explanation:
There are number apples and number oranges. is the correct answer to “What is the output of the following code: import re result = re.sub(r'd+', 'number', '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 9 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
How can you search for a specific pattern in a string using regular expressions in Python?
Correct Answer: Option A — By using the search() function from the re module
Detailed Academic Explanation:
By using the search() function from the re module is the correct answer to “How can you search for a specific pattern in a 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 10 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which of the following creates a pattern object?
Correct Answer: Option C — re.compile(str)
Detailed Academic Explanation:
re.compile(str) is the correct answer to “Which of the following creates a pattern object”. 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
What does the function re.match do?
Correct Answer: Option A — 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 “What does the function re.match do”. 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 is the output of the following code: import re result = re.findall(r'bw+b', 'Hello, world!') print(result)
Correct Answer: Option B — An error will occur
Detailed Academic Explanation:
An error will occur is the correct answer to “What is the output of the following code: import re result = re.findall(r'bw+b', '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 13 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What does the function re.search do?
Correct Answer: Option B — matches a pattern at any position in the string
Detailed Academic Explanation:
matches a pattern at any position in the string is the correct answer to “What does the function re.search do”. 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
What is the output of the following code: import re result = re.search(r'hello', 'Hello, world!', re.IGNORECASE) print(result.group())
Correct Answer: Option C — world
Detailed Academic Explanation:
world is the correct answer to “What is the output of the following code: import re result = re.search(r'hello', 'Hello, world!', re.IGNORECASE) print(result.group())”. 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 extract a group from a matched regular expression in Python?
Correct Answer: Option A — By using the group() method on the match object
Detailed Academic Explanation:
By using the group() method on the match object is the correct answer to “How can you extract a group from a matched regular expression 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
What is the purpose of the re.DOTALL flag in Python?
Correct Answer: Option A — It makes the . character match any character, including newline
Detailed Academic Explanation:
It makes the . character match any character, including newline is the correct answer to “What is the purpose of the re.DOTALL flag in Python”. 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
What will be the output of the following code: import re pattern = re.compile(r'd+') result = pattern.search('There are 5 apples and 3 oranges.') print(result.group())
Correct Answer: Option D — ['5', '3']
Detailed Academic Explanation:
['5', '3'] is the correct answer to “What will be the output of the following code: import re pattern = re.compile(r'd+') result = pattern.search('There are 5 apples and 3 oranges.') print(result.group())”. 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
Which of the codes shown below results in a match?
Correct Answer: Option C — re.match('George(?=Washington)', 'GeorgeWashington')
Detailed Academic Explanation:
re.match('George(?=Washington)', 'GeorgeWashington') is the correct answer to “Which of the codes shown below results in a match”. 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
The function of re.search is . . . . . . . .
Correct Answer: Option C — Matches a pattern from any part of a string
Detailed Academic Explanation:
Matches a pattern from any part of a string is the correct answer to “The function of re.search is . . . . . . . .”. 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
How can you match the beginning 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 beginning 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 21 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
The function re.error raises an exception if a particular string contains no match for the given pattern.
Correct Answer: Option B — False
Detailed Academic Explanation:
False is the correct answer to “The function re.error raises an exception if a particular string contains no match for the given pattern.”. 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? re.split('\W+', 'Hello, hello, hello.')
Correct Answer: Option D — ['Hello', 'hello', 'hello', "]
Detailed Academic Explanation:
['Hello', 'hello', 'hello', "] is the correct answer to “What will be the output of the following Python code? re.split('\W+', 'Hello, hello, hello.')”. 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
. . . . . . . . matches the start of the string. . . . . . . . . matches the end of the string.
Correct Answer: Option A — '^', '$'
Detailed Academic Explanation:
'^', '$' is the correct answer to “. . . . . . . . matches the start of the string. . . . . . . . . matches 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
Question 24 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the output of the following code: import re result = re.search(r'd{2}', 'There are 25 apples and 3 oranges.') print(result.group())
Correct Answer: Option A — 25
Detailed Academic Explanation:
25 is the correct answer to “What is the output of the following code: import re result = re.search(r'd{2}', 'There are 25 apples and 3 oranges.') print(result.group())”. 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 will be the output of the following Python code? re.compile('hello', re.X)
Correct Answer: Option B — re.compile('hello', re.VERBOSE)
Detailed Academic Explanation:
re.compile('hello', re.VERBOSE) is the correct answer to “What will be the output of the following Python code? re.compile('hello', re.X)”. 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.subn('A', 'X', 'AAAAAA', count=4)
Correct Answer: Option C — ('XXXXAA', 4)
Detailed Academic Explanation:
('XXXXAA', 4) is the correct answer to “What will be the output of the following Python code? re.subn('A', 'X', 'AAAAAA', count=4)”. 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
What is the purpose of the re.findall() function in Python?
Correct Answer: Option A — It finds all occurrences of a pattern in a string and returns them as a list
Detailed Academic Explanation:
It finds all occurrences of a pattern in a string and returns them as a list is the correct answer to “What is the purpose of the re.findall() function in Python”. 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
Which of the following functions clears the regular expression cache?
Correct Answer: Option C — re.purge()
Detailed Academic Explanation:
re.purge() is the correct answer to “Which of the following functions clears the regular expression cache”. 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? re.split(r'\s+', 'Chrome is better than explorer', maxspilt=3)
Correct Answer: Option B — ['Chrome', 'is', 'better', 'than explorer']
Detailed Academic Explanation:
['Chrome', 'is', 'better', 'than explorer'] is the correct answer to “What will be the output of the following Python code? re.split(r'\s+', 'Chrome is better than explorer', maxspilt=3)”. 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
What will be the output of the following Python code? import re re.ASCII
Correct Answer: Option D — 256
Detailed Academic Explanation:
256 is the correct answer to “What will be the output of the following Python code? import re re.ASCII”. 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? re.escape('new**world')
Correct Answer: Option B — 'new\\*\\*world'
Detailed Academic Explanation:
'new\\*\\*world' is the correct answer to “What will be the output of the following Python code? re.escape('new**world')”. 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
Which of the following functions results in case insensitive matching?
Correct Answer: Option C — re.I
Detailed Academic Explanation:
re.I is the correct answer to “Which of the following functions results in case insensitive matching”. 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
Which of the following will result in an error?
Correct Answer: Option C — >>> p = re.subn()
Detailed Academic Explanation:
>>> p = re.subn() is the correct answer to “Which of the following will result in an error”. 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 expression a{5} will match . . . . . . . . characters with the previous regular expression.
Correct Answer: Option B — exactly 5
Detailed Academic Explanation:
exactly 5 is the correct answer to “The expression a{5} will match . . . . . . . . characters with the previous regular expression.”. 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 is the purpose of the re.VERBOSE flag in Python?
Correct Answer: Option A — It allows you to write more readable and organized regular expressions
Detailed Academic Explanation:
It allows you to write more readable and organized regular expressions is the correct answer to “What is the purpose of the re.VERBOSE flag in Python”. 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
What will be the output of the following Python code? m = re.search('a', 'The blue umbrella') m.re.pattern
Correct Answer: Option C — 'a'
Detailed Academic Explanation:
'a' is the correct answer to “What will be the output of the following Python code? m = re.search('a', 'The blue umbrella') m.re.pattern”. 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
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.groupdict())
{'animal': 'horses', 'verb': 'are', 'adjective': 'fast'} is the correct answer to “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.groupdict())”. 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
What will be the output of the following Python code? re.match('sp(.*)am', 'spam')
Correct Answer: Option B — <_sre.SRE_Match object; span=(0, 4), match='spam'>
Detailed Academic Explanation:
<_sre.SRE_Match object; span=(0, 4), match='spam'> is the correct answer to “What will be the output of the following Python code? re.match('sp(.*)am', 'spam')”. 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
How can you specify a range of characters in a character class in regular expressions?
Correct Answer: Option A — By using a hyphen - between the characters
Detailed Academic Explanation:
By using a hyphen - between the characters is the correct answer to “How can you specify a range of characters in 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 40 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following Python function? re.findall("hello world", "hello", 1)
Correct Answer: Option B — [ ]
Detailed Academic Explanation:
[ ] is the correct answer to “What will be the output of the following Python function? re.findall("hello world", "hello", 1)”. 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.