LearnFrenzy
Sign in Take a free test

Regular Expressions · Exercise 2 of 4

Regular Expressions Predict the Output

  • 15 questions
  • Page 2 of 2
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Read each Python program and choose what it prints. Every program was run on Python 3, so the answers are the real output.

Question 11 Medium

What is the output of the following Python program?

main.py
import re
text = "one two\nthree four"
print(re.findall(r"^\w+", text), re.findall(r"^\w+", text, re.M), re.findall(r"\w+$", text, re.M))
A ['one', 'three'] ['one', 'three'] ['four']
B ['one'] ['one'] ['four']
C ['one'] ['one', 'three'] ['two', 'four']
D ['one'] ['one', 'three'] ['four']
Question page & discussion
Question 12 Easy

What is the output of the following Python program?

main.py
import re
print(re.findall(r"\b[A-Z][a-z]+\b", "Asha met Ravi in pune and DELHI"))
A ['Asha', 'Ravi', 'DELHI']
B ['Asha', 'Ravi', 'Pune']
C ['A', 'R', 'D']
D ['Asha', 'Ravi']
Question page & discussion
Question 13 Medium

What is the output of the following Python program?

main.py
import re
s = "v1.2 and 3x4 and 10.05"
print(re.findall(r"\d+\.\d+", s), re.findall(r"\d+.\d+", s))
A ['1.2', '10.05'] ['1.2', '10.05']
B ['1.2', '3x4', '10.05'] ['1.2', '10.05']
C ['1.2'] ['1.2', '3x4']
D ['1.2', '10.05'] ['1.2', '3x4', '10.05']
Question page & discussion
Question 14 Medium

What is the output of the following Python program?

main.py
import re
print(re.sub(r"\d+", lambda m: str(int(m.group()) * 2), "3 apples 10 pears"))
print(re.sub(r"\b(\w)", lambda m: m.group(1).upper(), "learn python fast"))
A 6 apples 20 pears Learn Python Fast
B 33 apples 1010 pears Learn Python Fast
C 6 apples 20 pears LEARN PYTHON FAST
D 3 apples 10 pears learn python fast
Question page & discussion
Question 15 Hard

What is the output of the following Python program?

main.py
import re
rule = r"(?=.*\d)(?=.*[A-Z]).{8,}"
print([bool(re.fullmatch(rule, p)) for p in ["Passw0rd", "password1", "Pass1", "PASSWORD9"]])
A [True, True, False, True]
B [True, False, False, False]
C [True, False, False, True]
D [False, False, False, True]
Question page & discussion
Up next · Exercise 3Find the Error10 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links