Test Series - python

Test Number 96/108

Q: Which of the following functions returns a dictionary mapping group names to group numbers?
A. re.compile.group
B. re.compile.groupindex
C. re.compile.index
D. re.compile.indexgroup
Solution: The function re.compile.groupindex returns a dictionary mapping group names to group numbers.
Q: Which of the following statements regarding the output of the function re.match is incorrect?
A. ‘pq*’ will match ‘pq’
B. ‘pq?’ matches ‘p’
C. ‘p{4}, q’ does not match ‘pppq’
D. ‘pq+’ matches ‘p’
Solution: All of the above statements are correct except that ‘pq+’ match ‘p’. ‘pq+’ will match ‘p’ followed by any non-zero number of q’s, but it will not match ‘p’.
Q: Which of the following functions does not accept any argument?
A. re.purge
B. re.compile
C. re.findall
D. re.match
Solution: The function re.purge is used to clear the cache and it does not accept any arguments.
Q: Which of the following lines of code will not show a match?
A. >>> re.match(‘ab*’, ‘a’)
B. >>> re.match(‘ab*’, ‘ab’)
C. >>> re.match(‘ab*’, ‘abb’)
D. >>> re.match(‘ab*’, ‘ba’)
Solution: In the code shown above, ab* will match to ‘a’ or ‘ab’ or ‘a’ followed by any number of b’s. Hence the only line of code from the above options which does not result in a match is:
>>> re.match(‘ab*’, ‘ba’).
Q: To open a file c:scores.txt for reading, we use _____________
A. infile = open(“c:scores.txt”, “r”)
B. infile = open(“c:\scores.txt”, “r”)
C. infile = open(file = “c:scores.txt”, “r”)
D. infile = open(file = “c:\scores.txt”, “r”)
Solution: Execute help(open) to get more details.
Q: To open a file c:scores.txt for writing, we use ____________
A. outfile = open(“c:scores.txt”, “w”)
B. outfile = open(“c:\scores.txt”, “w”)
C. outfile = open(file = “c:scores.txt”, “w”)
D. outfile = open(file = “c:\scores.txt”, “w”)
Solution: w is used to indicate that file is to be written to.

You Have Score    /6