Saturday, July 6, 2024

30+ MCQs on Python Common Expression

Welcome to the Python Common Expression MCQs! Common expressions, typically abbreviated as “regex,” are highly effective instruments for sample matching and looking out in strings. In Python, the re module supplies help for normal expressions, permitting you to look, match, and manipulate textual content based mostly on particular patterns. These questions will take a look at your understanding of assorted elements of normal expressions in Python, together with sample matching, particular characters, flags, strategies, and extra. Every query is multiple-choice, with just one appropriate reply. Take your time to rigorously learn every query and select the best choice. Let’s dive into the world of Python common expressions collectively!

Python Regular Expression

30+ MCQs on Python Common Expression

Q1. What’s a daily expression in Python?

a) A operate that creates strings

b) A sequence of characters that types a search sample

c) A technique for sorting lists

d) An exception sort in Python

Reply: b

Clarification: An everyday expression is a sequence of characters that types a search sample, used primarily for sample matching with strings.

Q2. Which library in Python is used for normal expressions?

a) re

b) regex

c) reg

d) regexp

Reply: a

Clarification: The re library is the usual library in Python for normal expressions.

Q3. What does the re.search() operate in Python do?

a) Searches for a sample inside a string

b) Types a listing of strings

c) Converts a string to lowercase

d) Compares two strings

Reply: a

Clarification: The re.search() operate searches for a sample inside a string and returns the primary match.

This autumn. Which character is utilized in common expressions to match any single character?

a) *

b) $

c) ?

d) .

Reply: d

Clarification: The . (dot) character in common expressions matches any single character, besides newline characters.

Q5. What is going to the next common expression sample match?

sample = r'd+'

a) Matches a number of digits

b) Matches a number of alphabetic characters

c) Matches any character

d) Matches a selected string

Reply: a

Clarification: The sample d+ matches a number of digits in a string.

Q6. Which of the next quantifiers in common expressions matches zero or one prevalence of the previous sample?

a) +

b) *

c) ?

d) {

Reply: c

Clarification: The ? quantifier in common expressions matches zero or one prevalence of the previous sample.

Q7. What is going to the next common expression sample match?

sample = r'w{3}'

a) Matches precisely three alphanumeric characters

b) Matches any phrase character

c) Matches any three characters

d) Matches three digits

Reply: a

Clarification: The sample w{3} matches precisely three alphanumeric characters.

Q8. What does the re.findall() operate in Python do?

a) Finds all matches of a sample inside a string and returns them as a listing

b) Finds the primary match of a sample inside a string

c) Replaces all occurrences of a sample with a specified string

d) Returns the size of the matching substring

Reply: a

Clarification: The re.findall() operate finds all matches of a sample inside a string and returns them as a listing.

Q9. Which of the next is a legitimate common expression to match the phrase “whats up” in a string?

a) r’whats up’

b) ‘whats up’

c) r’bhellob’

d) ‘whats up’

Reply: c

Clarification: The common expression bhellob matches the phrase “whats up” as a complete phrase in a string.

Q10. What would be the output of the next code?

import re

sample = r'd{2}-d{2}-d{4}'
textual content = "Date of delivery: 12-31-1990"
end result = re.findall(sample, textual content)
print(end result)

a) [’12-31-1990′]

b) [’12’, ’31’, ‘1990’]

c) [’12’, ‘-‘, ’31’, ‘-‘, ‘1990’]

d) []

Reply: a

Clarification: The code will output ['12-31-1990'] as a result of the common expression matches the date format inside the given textual content.

Q11. Which of the next metacharacters in common expressions matches the start of a line?

a) ^

b) $

c) *

d) +

Reply: a

Clarification: The ^ metacharacter in common expressions matches the start of a line.

Q12. What does the re.sub() operate in Python do?

a) Searches for a sample inside a string and returns the primary match

b) Finds all matches of a sample inside a string and returns them as a listing

c) Replaces all occurrences of a sample with a specified string

d) Splits a string into a listing based mostly on a sample

Reply: c

Clarification: The re.sub() operate in Python replaces all occurrences of a sample with a specified string in a given textual content.

Q13. Which of the next characters is used to flee a metacharacter in common expressions?

a) !

b) &

c)

d) /

Reply: c

Clarification: The (backslash) character is used to flee a metacharacter in common expressions.

Q14. What is going to the next common expression sample match?

sample = r'b[A-Z]w+b'

a) Matches phrases that begin with an uppercase letter

b) Matches any phrase character

c) Matches any three characters

d) Matches phrases that begin with a lowercase letter

Reply: a

Clarification: The sample b[A-Z]w+b matches phrases that begin with an uppercase letter adopted by a number of phrase characters.

Q15. Which of the next is NOT a legitimate flag for the re.compile() operate in Python?

a) re.IGNORECASE

b) re.MULTILINE

c) re.ALLCAPS

d) re.DOTALL

Reply: c

Clarification: re.ALLCAPS just isn’t a legitimate flag for the re.compile() operate in Python. The right flag is re.IGNORECASE for case-insensitive matching.

Q16. What would be the output of the next code?

import re

sample = r'[A-Za-z0-9_]+'
textual content = "Good day World_123"
end result = re.findall(sample, textual content)
print(end result)

a) [‘Hello’, ‘World_123’]

b) [‘Hello’, ‘World’, ‘123’]

c) [‘Hello_World_123’]

d) [‘Hello’, ‘World’, ‘_’, ‘123’]

Reply: a

Clarification: The code will output ['Hello', 'World_123'] as a result of the common expression matches alphanumeric characters and underscores.

Q17. Which of the next metacharacters in common expressions matches zero or extra occurrences of the previous sample?

a) ?

b) +

c) *

d) {

Reply: c

Clarification: The * metacharacter in common expressions matches zero or extra occurrences of the previous sample.

Q18. What does the next common expression sample match?

sample = r'^[a-z]+'

a) Matches a number of lowercase letters at first of a line

b) Matches any lowercase letter

c) Matches any character at first of a line

d) Matches a number of lowercase letters

Reply: a

Clarification: The sample ^[a-z]+ matches a number of lowercase letters at first of a line.

Q19. What would be the output of the next code?

import re

sample = r'bd{3}b'
textual content = "123 4567 890 12 3456"
end result = re.findall(sample, textual content)
print(end result)

a) [‘123’, ‘890’]

b) [‘123’, ‘456’, ‘890’, ‘345’]

c) [‘123’, ‘890’, ‘345’]

d) [‘123’]

Reply: a

Clarification: The code will output ['123', '890'] as a result of the sample bd{3}b matches three-digit numbers as entire phrases.

Q20. Which of the next metacharacters in common expressions matches any digit?

a) d

b) w

c) .

d) s

Reply: a

Clarification: The d metacharacter in common expressions matches any digit from 0 to 9.

Q21. What is going to the next common expression sample match?

sample = r'b[A-Z]w*'

a) Matches phrases that begin with an uppercase letter

b) Matches any phrase character

c) Matches any three characters

d) Matches phrases that begin with a lowercase letter

Reply: a

Clarification: The sample b[A-Z]w* matches phrases that begin with an uppercase letter adopted by zero or extra phrase characters.

Q22. What’s the objective of the re.compile() operate in Python common expressions?

a) To compile Python code

b) To outline a daily expression sample

c) To create a compiled common expression object for reuse

d) To execute a daily expression sample

Reply: c

Clarification: The re.compile() operate in Python is used to create a compiled common expression object for reuse, which might enhance efficiency.

Q23. Which of the next metacharacters in common expressions matches the top of a line?

a) $

b) ^

c) .

d) *

Reply: a

Clarification: The $ metacharacter in common expressions matches the top of a line.

Q24. What does the re.cut up() operate in Python do?

a) Searches for a sample inside a string and returns the primary match

b) Finds all matches of a sample inside a string and returns them as a listing

c) Replaces all occurrences of a sample with a specified string

d) Splits a string into a listing based mostly on a sample

Reply: d

Clarification: The re.cut up() operate in Python splits a string into a listing based mostly on a sample.

Q25. What would be the output of the next code?

import re

sample = r'b[A-Za-z]+b'
textual content = "Good day World123"
end result = re.findall(sample, textual content)
print(end result)

a) [‘Hello’, ‘World’]

b) [‘Hello’, ‘World123’]

c) [‘Hello’, ‘World123’]

d) [‘Hello’]

Reply: a

Clarification: The code will output ['Hello', 'World'] as a result of the common expression matches phrases containing solely letters.

Q26. Which of the next is a legitimate common expression to match an e-mail deal with?

a) r’b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}b’

b) r'[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}’

c) r’b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}’

d) r’b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,3}b’

Reply: a

Clarification: The common expression b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}b is a legitimate sample to match an e-mail deal with.

Q27. What would be the output of the next code?

import re

sample = r'bd{3}-d{2}-d{4}b'
textual content = "Social Safety Numbers: 123-45-6789, 9876-543-210"
end result = re.findall(sample, textual content)
print(end result)

a) [‘123-45-6789’]

b) [‘123′, ’45’, ‘6789’]

c) [‘123-45-6789’, ‘9876-543-210’]

d) [‘123′, ’45’, ‘6789’, ‘9876’, ‘543’, ‘210’]

Reply: a

Clarification: The code will output ['123-45-6789'] as a result of the common expression matches the Social Safety Quantity format inside the given textual content.

Q28. Which of the next metacharacters in common expressions matches any whitespace character?

a) w

b) s

c) .

d) +

Reply: b

Clarification: The s metacharacter in common expressions matches any whitespace character, together with areas, tabs, and newlines.

Q29. What does the re.match() operate in Python do?

a) Searches for a sample inside a string and returns the primary match

b) Finds all matches of a sample inside a string and returns them as a listing

c) Replaces all occurrences of a sample with a specified string

d) Checks if a sample matches at first of a string

Reply: d

Clarification: The re.match() operate in Python checks if a sample matches at first of a string.

Q30. What would be the output of the next code?

import re

sample = r'bd{3}b'
textual content = "123 4567 890 12 3456"
end result = re.findall(sample, textual content)
print(end result)

a) [‘123’, ‘890’]

b) [‘123’, ‘456’, ‘890’, ‘345’]

c) [‘123’, ‘890’, ‘345’]

d) [‘123’]

Reply: a

Clarification: The code will output ['123', '890'] as a result of the sample bd{3}b matches three-digit numbers as entire phrases.

Q31. What would be the output of the next code?

import re

sample = r'b[A-Z][a-z]*b'
textual content = "The short Brown Fox jumps over the Lazy Canine"
end result = re.findall(sample, textual content)
print(end result)

a) [‘The’, ‘Brown’, ‘Fox’, ‘Lazy’, ‘Dog’]

b) [‘The’, ‘Fox’, ‘Lazy’, ‘Dog’]

c) [‘The’, ‘quick’, ‘Brown’, ‘Fox’, ‘Lazy’, ‘Dog’]

d) [‘The’, ‘quick’, ‘Fox’, ‘Dog’]

Reply: a

Clarification: The code will output ['The', 'Brown', 'Fox', 'Lazy', 'Dog'] as a result of the sample b[A-Z][a-z]*b matches phrases beginning with an uppercase letter within the given textual content.

Q32. Which of the next is a legitimate common expression to match a legitimate e-mail deal with?

a) r’b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+.[A-Z|a-z]{2,}b’

b) r’b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,}b’

c) r’b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,3}b’

d) r’b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,4}b’

Reply: d

Clarification: The common expression r'b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,4}b' is a legitimate sample to match a legitimate e-mail deal with.

Q33. What would be the output of the next code?

import re

sample = r'bw{4,}b'
textual content = "Python is a robust programming language"
end result = re.findall(sample, textual content)
print(end result)

a) [‘Python’, ‘powerful’, ‘programming’, ‘language’]

b) [‘Python’, ‘powerful’, ‘programming’]

c) [‘powerful’, ‘programming’, ‘language’]

d) [‘Python’, ‘programming’, ‘language’]

Reply: a

Clarification: The code will output ['Python', 'powerful', 'programming', 'language'] as a result of the sample bw{4,}b matches phrases with 4 or extra letters within the given textual content.

Congratulations on finishing the Python Common Expression MCQs! Common expressions are important for superior string manipulation and sample matching duties in Python. By mastering common expressions, you achieve the power to seek for particular patterns, extract info, validate enter, and carry out complicated textual content manipulation operations. Preserve working towards and experimenting with totally different patterns and strategies offered by the re module to grow to be proficient in utilizing common expressions successfully. When you have any questions or wish to delve deeper into any subject, don’t hesitate to proceed your studying journey. Joyful coding!

You can too enroll in our free Python Course Right this moment!

Learn our extra articles associated to MCQs in Python:

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles