Test Series - python

Test Number 92/108

Q: The character Dot (that is, ‘.’) in the default mode, matches any character other than _____________
A. caret
B. ampersand
C. percentage symbol
D. newline
Solution: The character Dot (that is, ‘,’) in the default mode, matches any character other than newline. If DOTALL flag is used, then it matches any character other than newline.
Q: 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
Solution: 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.
Q: ________ matches the start of the string.
________ matches the end of the string.
A. ‘^’, ‘$’
B. ‘$’, ‘^’
C. ‘$’, ‘?’
D. ‘?’, ‘^’
Solution: ‘^’ (carat) matches the start of the string.
‘$’ (dollar sign) matches the end of the string.
Q: Which of the following will result in an error?
A. >>> p = re.compile("d") >>> p.search("door")
B. >>> p = re.escape(‘hello’)
C. >>> p = re.subn()
D. >>> p = re.purge()
Solution: The function re.subn() will result in an error. This is because subn() requires 3 positional arguments while we have entered none.
Q: What will be the output of the following Python function?

re.findall("hello world", "hello", 1)
A. [“hello”]
B. [ ]
C. hello
D. hello world
Solution: The function findall returns the word matched if and only if both the pattern and the string match completely, that is, they are exactly the same. Observe the example shown below:
>>> re.findall(“hello”, “hello”, 1) The output is: [‘hello’] Hence the output of the code shown in this question is [].
Q: Choose the function whose output can be: <_sre.SRE_Match object; span=(4, 8), match=’aaaa’>.
A. >>> re.search(‘aaaa’, “alohaaaa”, 0)
B. >>> re.match(‘aaaa’, “alohaaaa”, 0)
C. >>> re.match(‘aaa’, “alohaaa”, 0)
D. >>> re.search(‘aaa’, “alohaaa”, 0)
Solution: The output shown above is that of a search function, whose pattern is ‘aaaa’ and the string is that of 8 characters. The only option which matches all these criteria is:
>>> re.search(‘aaaa’, “alohaaaa”, 0)
Q: Which of the following functions clears the regular expression cache?
A. re.sub()
B. re.pos()
C. re.purge()
D. re.subn()
Solution: The function which clears the regular expression cache is re.purge(). Note that this function takes zero positional arguments.
Q: What will be the output of the following Python code?

import re
re.ASCII
A. 8
B. 32
C. 64
D. 256
Solution: The expression re.ASCII returns the total number of ASCII characters that are present, that is 256. This can also be abbreviated as re.A, which results in the same output (that is, 256).
Q: Which of the following functions results in case insensitive matching?
A. re.A
B. re.U
C. re.I
D. re.X
Solution: The function re.I (that is, re.IGNORECASE) results in case-insensitive matching. That is, expressions such as [A-Z] will match lowercase characters too.

You Have Score    /9