Python Regular Expressions MCQ Questions and Answers

1. Which module in Python provides support for regular expressions?

a) regex
b) re
c) regular
d) regexp

Answer:

b) re

Explanation:

The 're' module in Python provides support for working with regular expressions.

2. How do you search for a pattern in a string using regular expressions?

a) re.search('pattern', string)
b) re.match('pattern', string)
c) re.find('pattern', string)
d) re.pattern('pattern', string)

Answer:

a) re.search('pattern', string)

Explanation:

The re.search() function searches for a pattern in a string and returns a match object if found.

3. What does the re.match() function do?

a) Matches a pattern at the beginning of the string
b) Matches a pattern anywhere in the string
c) Matches all occurrences of the pattern in the string
d) Splits the string at each match of the pattern

Answer:

a) Matches a pattern at the beginning of the string

Explanation:

re.match() checks for a match of the pattern at the beginning of the string.

4. How do you compile a regular expression pattern for repeated use?

a) re.compile('pattern')
b) re.pattern('pattern')
c) re.make('pattern')
d) re.create('pattern')

Answer:

a) re.compile('pattern')

Explanation:

The re.compile() function compiles a regular expression pattern into a regular expression object, which can be used for matching.

5. What is the meaning of the wildcard '.' in regular expressions?

a) Matches any character except a newline
b) Matches a period character
c) Matches any string
d) Matches the end of a line

Answer:

a) Matches any character except a newline

Explanation:

In regular expressions, '.' is a wildcard that matches any character except a newline.

6. How do you find all occurrences of a pattern in a string?

a) re.search('pattern', string)
b) re.match('pattern', string)
c) re.findall('pattern', string)
d) re.find('pattern', string)

Answer:

c) re.findall('pattern', string)

Explanation:

The re.findall() function returns a list of all non-overlapping matches of the pattern in the string.

7. What is the function of re.sub() in regular expressions?

a) Substitutes a substring in a string
b) Searches for a substring in a string
c) Splits a string based on a pattern
d) Compiles a regular expression pattern

Answer:

a) Substitutes a substring in a string

Explanation:

The re.sub() function replaces occurrences of a pattern in a string with a replacement string.

8. What does the '^' character signify in regular expressions?

a) The beginning of a string
b) The end of a string
c) Any character
d) A literal '^' character

Answer:

a) The beginning of a string

Explanation:

The '^' character is used to match the beginning of a string.

9. What does the '$' character signify in regular expressions?

a) The beginning of a string
b) The end of a string
c) Any character
d) A literal '$' character

Answer:

b) The end of a string

Explanation:

The '$' character is used to match the end of a string.

10. How do you indicate zero or more occurrences of a pattern in regular expressions?

a) *
b) +
c) ?
d) {}

Answer:

a) *

Explanation:

The '*' character signifies zero or more occurrences of the preceding pattern.

11. How do you specify a group in a regular expression?

a) [group]
b) {group}
c) "group"
d) (group)

Answer:

d) (group)

Explanation:

Parentheses are used to create groups in regular expressions.

12. What does the '+' character signify in regular expressions?

a) Zero or more occurrences
b) One or more occurrences
c) Optional occurrence
d) Exact one occurrence

Answer:

b) One or more occurrences

Explanation:

The '+' character signifies one or more occurrences of the preceding pattern.

13. What is the function of the re.IGNORECASE flag?

a) Ignores whitespace in the pattern
b) Performs a case-insensitive match
c) Ignores special characters in the pattern
d) Compiles the pattern for faster execution

Answer:

b) Performs a case-insensitive match

Explanation:

The re.IGNORECASE flag allows for case-insensitive matching of the pattern.

14. How do you escape special characters in regular expressions?

a) Using the '\' character before the special character
b) Enclosing the special character in square brackets
c) Using the '^' character before the special character
d) Placing the special character in parentheses

Answer:

a) Using the '\' character before the special character

Explanation:

The '\' (backslash) is used to escape special characters in regular expressions.

15. How do you match either 'cat' or 'dog' in a string?

a) re.search('(cat|dog)', string)
b) re.search('cat|dog', string)
c) re.search('[cat|dog]', string)
d) re.search('cat dog', string)

Answer:

b) re.search('cat|dog', string)

Explanation:

The '|' character is used as a logical OR in regular expressions to match either 'cat' or 'dog'.


Comments