A directory of Objective Type Questions covering all the Computer Science subjects. Here you can access and discuss Multiple choice questions and answers for various compitative exams and interviews.

1. Which module in Python supports regular expressions?
a. re
b. regex
c. pyregex
d. none of the mentioned
View Answer Report Discuss Too Difficult!
Answer: (a).re

2. Which of the following creates a pattern object?
a. re.create(str)
b. re.regex(str)
c. re.compile(str)
d. re.assemble(str)
View Answer Report Discuss Too Difficult!
Answer: (c).re.compile(str)

3. What does the function re.match do?
a. matches a pattern at the start of the string
b. matches a pattern at any position in the string
c. such a function does not exist
d. none of the mentioned
View Answer Report Discuss Too Difficult!
Answer: (a).matches a pattern at the start of the string

4. What does the function re.search do?
a. matches a pattern at the start of the string
b. matches a pattern at any position in the string
c. such a function does not exist
d. none of the mentioned
View Answer Report Discuss Too Difficult!
Answer: (b).matches a pattern at any position in the string

5. What is the output of the following?
sentence = 'we are humans'
matched = re.match(r'(.*) (.*?) (.*)', sentence)
print(matched.groups())
a. (‘we’, ‘are’, ‘humans’)
b. (we, are, humans)
c. (‘we’, ‘humans’)
d. ‘we are humans’
View Answer Report Discuss Too Difficult!
Answer: (a).(‘we’, ‘are’, ‘humans’)

6. What is the output of the following?
sentence = 'we are humans'
matched = re.match(r'(.*) (.*?) (.*)', sentence)
print(matched.group())
a. (‘we’, ‘are’, ‘humans’)
b. (we, are, humans)
c. (‘we’, ‘humans’)
d. ‘we are humans’
View Answer Report Discuss Too Difficult!
Answer: (d).‘we are humans’

7. What is the output of the following?
sentence = 'we are humans'
matched = re.match(r'(.*) (.*?) (.*)', sentence)
print(matched.group(2))
a. ‘are’
b. ‘we’
c. ‘humans’
d. ‘we are humans’
View Answer Report Discuss Too Difficult!
Answer: (c).‘humans’

8. What is the output of the following?
sentence = 'horses are fast'
regex = re.compile('(?P<animal>\w+) (?P<verb>\w+) (?P<adjective>\w+)')
matched = re.search(regex, sentence)
print(matched.groupdict())
a. {‘animal’: ‘horses’, ‘verb’: ‘are’, ‘adjective’: ‘fast’}
b. (‘horses’, ‘are’, ‘fast’)
c. ‘horses are fast’
d. ‘are’
View Answer Report Discuss Too Difficult!
Answer: (a).{‘animal’: ‘horses’, ‘verb’: ‘are’, ‘adjective’: ‘fast’}

9. What is the output of the following?
sentence = 'horses are fast'
regex = re.compile('(?P<animal>\w+) (?P<verb>\w+) (?P<adjective>\w+)')
matched = re.search(regex, sentence)
print(matched.groups())
a. {‘animal’: ‘horses’, ‘verb’: ‘are’, ‘adjective’: ‘fast’}
b. (‘horses’, ‘are’, ‘fast’)
c. ‘horses are fast’
d. ‘are’
View Answer Report Discuss Too Difficult!
Answer: (b).(‘horses’, ‘are’, ‘fast’)

10. What is the output of the following?
sentence = 'horses are fast'
regex = re.compile('(?P<animal>\w+) (?P<verb>\w+) (?P<adjective>\w+)')
matched = re.search(regex, sentence)
print(matched.group(2))
a. {‘animal’: ‘horses’, ‘verb’: ‘are’, ‘adjective’: ‘fast’}
b. (‘horses’, ‘are’, ‘fast’)
c. ‘horses are fast’
d. ‘are’
View Answer Report Discuss Too Difficult!
Answer: (d).‘are’

Page 1 of 10