Python Programming Multiple Choice Question -
Regular Expressions
This section focuses on the "Regular Expressions" in Python programming. These Multiple Choice Questions (mcq) should be practiced to improve the Python programming skills required for various interviews (campus interview, walk-in interview, company interview), placement, entrance exam and other competitive examinations.
1. Python has a built-in package called?
A. reg
B. regex
C. re
D. regx
View Answer
Ans : C
Explanation: Python has a built-in package called re, which can be used to work with Regular Expressions.
2. Which function returns a list containing all matches?
A. findall
B. search
C. split
D. find
View Answer
Ans : A
Explanation: findall : Returns a list containing all matches
3. Which character stand for Starts with in regex?
A. &
B. ^
C. $
D. #
View Answer
Ans : B
Explanation: In regex, ^ means start with.
4. Which character stand for Zero or more occurrences in regex?
A. *
B. #
C. @
D. |
View Answer
Ans : A
Explanation: * stands for Zero or more occurrences
5. In Regex, s stands for?
A. Returns a match where the string DOES NOT contain digits
B. Returns a match where the string DOES NOT contain a white space character
C. Returns a match where the string contains a white space character
D. Returns a match if the specified characters are at the end of the string
View Answer
Ans : C
Explanation: s : Returns a match where the string contains a white space character
6. In Regex, [a-n] stands for?
A. Returns a match for any digit between 0 and 9
B. Returns a match for any lower case character, alphabetically between a and n
C. Returns a match for any two-digit numbers from 00 and 59
D. Returns a match for any character EXCEPT a, r, and n
View Answer
Ans : B
Explanation: [a-n] : Returns a match for any lower case character, alphabetically between a and n
7. The expression a{5} will match _____________ characters with the previous regular expression.
A. 5 or less
B. exactly 5
C. 5 or more
D. exactly 4
View Answer
Ans : B
Explanation: The character {m} is used to match exactly m characters to the previous regular expression. Hence the expression a{5} will match exactly 5 characters and not less than that.
8. Which of the following functions clears the regular expression cache?
A. re.sub()
B. re.pos()
C. re.purge()
D. re.subn()
View Answer
Ans : C
Explanation: The function which clears the regular expression cache is re.purge(). Note that this function takes zero positional arguments.
Discussion