Home
MCQS
MCQs Practice Portal - thefunword.com
Python MCQ Quiz Hub
Python Mcq Set 17
Choose a topic to test your knowledge and improve your Python skills
1. The process of pickling in Python includes:
conversion of a list into a datatable
conversion of a byte stream into Python object hierarchy
conversion of a Python object hierarchy into byte stream
conversion of a datatable into a list
2. To sterilize an object hierarchy, the _____________ function must be called. To desterilize a data stream, the ______________ function must be called.
dumps(), undumps()
loads(), unloads()
loads(), dumps()
dumps(), loads()
3. Pick the correct statement regarding pickle and marshal modules.
The pickle module supports primarily .pyc files whereas marshal module is used to sterilize Python objects
The pickle module keeps track of the objects that have already been sterilized whereas the marshal module does not do this
The pickle module cannot be used to sterilize user defined classes and their instances whereas marshal module can be used to perform this task
The format of sterilization of the pickle module is not guaranteed to be supported across all versions of Python. The marshal module sterilization is compatible across all the versions of Python
4. What will be the output of the following Python code? pickle.HIGHEST_PROTOCOL
4
5
3
6
5. Which of the following Python codes will result in an error? object = ‘a’
>>> pickle.dumps(object)
>>> pickle.dumps(object, 3)
>>> pickle.dumps(object, 3, True)
>>> pickle.dumps(‘a’, 2)
6. Which of the following functions can be used to find the protocol version of the pickle module currently being used?
pickle.DEFAULT
pickle.CURRENT
pickle.CURRENT_PROTOCOL
pickle.DEFAULT_PROTOCOL
7. Which of the following functions can accept more than one positional argument?
pickle.dumps
pickle.loads
pickle.dump
pickle.load
8. Which of the following functions raises an error when an unpicklable object is encountered by Pickler?
pickle.PickleError
pickle.PicklingError
pickle.UnpickleError
pickle.UnpicklingError
9. The pickle module defines ______ exceptions and exports _______ classes.
2, 3
3, 4
3, 2
4, 3
10. Which of the following cannot be pickled?
Functions which are defined at the top level of a module with lambda
Functions which are defined at the top level of a module with def
Built-in functions which are defined at the top level of a module
Classes which are defined at the top level of a module
11. If __getstate__() returns _______________ the __setstate__() module will not be called on pickling.
True value
False value
ValueError
OverflowError
12. Lambda functions cannot be pickled because:
Lambda functions only deal with binary values, that is, 0 and 1
Lambda functions cannot be called directly
Lambda functions cannot be identified by the functions of the pickle module
All lambda functions have the same name, that is, <lambda>
13. The module _____ is a comparatively faster implementation of the pickle module.
cPickle
nPickle
gPickle
tPickle
14. The copy module uses the ________ protocol for shallow and deep copy.
pickle
marshal
shelve
copyreg
15. Which module in Python supports regular expressions?
re
regex
pyregex
none of the mentioned
16. Which of the following creates a pattern object?
re.create(str)
re.regex(str)
re.compile(str)
re.assemble(str)
17. What does the function re.match do?
matches a pattern at the start of the string
matches a pattern at any position in the string
such a function does not exist
None of the mentioned
18. What does the function re.search do?
matches a pattern at the start of the string
matches a pattern at any position in the string
such a function does not exist
None of the mentioned
19. What will be the output of the following Python code? sentence = 'we are humans' matched = re.match(r'(.*) (.*?) (.*)', sentence) print(matched.groups())
(‘we’, ‘are’, ‘humans’)
(we, are, humans)
(‘we’, ‘humans’)
‘we are humans’
20. What will be the output of the following Python code? sentence = 'we are humans' matched = re.match(r'(.*) (.*?) (.*)', sentence) print(matched.group())
(‘we’, ‘are’, ‘humans’)
(we, are, humans)
(‘we’, ‘humans’)
‘we are humans’
21. What will be the output of the following Python code? sentence = 'we are humans' matched = re.match(r'(.*) (.*?) (.*)', sentence) print(matched.group(2))
‘are’
‘we’
‘humans’
‘we are humans’
22. What will be the output of the following Python code? sentence = 'horses are fast' regex = re.compile('(?P<animal>w+) (?P<verb>w+) (?P<adjective>w+)') matched = re.search(regex, sentence) print(matched.groupdict())
{‘animal’: ‘horses’, ‘verb’: ‘are’, ‘adjective’: ‘fast’}
(‘horses’, ‘are’, ‘fast’)
horses are fast’
‘are’
23. What will be the output of the following Python code? sentence = 'horses are fast' regex = re.compile('(?P<animal>w+) (?P<verb>w+) (?P<adjective>w+)') matched = re.search(regex, sentence) print(matched.groups())
{‘animal’: ‘horses’, ‘verb’: ‘are’, ‘adjective’: ‘fast’}
(‘horses’, ‘are’, ‘fast’)
‘horses are fast’
‘are’
24. What will be the output of the following Python code? 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))
{‘animal’: ‘horses’, ‘verb’: ‘are’, ‘adjective’: ‘fast’}
(‘horses’, ‘are’, ‘fast’)
‘horses are fast’
‘are’
25. The character Dot (that is, ‘.’) in the default mode, matches any character other than _______
caret
ampersand
percentage symbol
newline
26. The expression a{5} will match _____ characters with the previous regular expression.
5 or less
exactly 5
5 or more
exactly 4
27. ______ matches the start of the string. ________ matches the end of the string.
‘^’, ‘$’
‘$’, ‘^’
‘$’, ‘?’
‘?’, ‘^’
28. What will be the output of the following Python code? re.split('W+', 'Hello, hello, hello.')
[‘Hello’, ‘hello’, ‘hello.’]
[‘Hello, ‘hello’, ‘hello’]
[‘Hello’, ‘hello’, ‘hello’, ‘.’]
[‘Hello’, ‘hello’, ‘hello’, â€]
29. What will be the output of the following Python function? re.findall("hello world", "hello", 1)
[“helloâ€]
[ ]
hello
hello world
30. Choose the function whose output can be: <_sre.SRE_Match object; span=(4, 8), match=’aaaa’>.
&gt;&gt;&gt; re.search(‘aaaa’, “alohaaaaâ€, 0)
&gt;&gt;&gt; re.match(‘aaaa’, “alohaaaaâ€, 0)
&gt;&gt;&gt; re.match(‘aaa’, “alohaaaâ€, 0)
&gt;&gt;&gt; re.search(‘aaa’, “alohaaaâ€, 0)
31. Which of the following functions clears the regular expression cache?
re.sub()
re.pos()
re.purge()
re.subn()
32. What will be the output of the following Python code? import re re.ASCII
8
32
64
256
33. Which of the following functions results in case insensitive matching?
re.A
re.U
re.I
re.X
34. What will be the output of the following Python code? re.compile('hello', re.X)
[‘h’, ‘e’, ‘l’, ‘l’, ‘o’]
re.compile(‘hello’, re.VERBOSE)
Error
Junk value
35. What will be the output of the following Python code? re.split('[a-c]', '0a3B6', re.I)
Error
[‘a’, ‘B’]
[‘0’, ‘3B6’]
[‘a’]
36. What will be the output of the following Python code? re.sub('morning', 'evening', 'good morning')
‘good evening’
‘good’
‘morning’
‘evening’
37. What will be the output of the following Python code? re.escape('new**world')
‘new world’
‘new\*\*world’
‘**’
‘new’, ‘*’, ‘*’, ‘world’
38. What will be the output of the following Python code? re.fullmatch('hello', 'hello world')
No output
[]
&lt;_sre.SRE_Match object; span=(0, 5), match=&#039;hello&#039;&gt;
error
39. The difference between the functions re.sub and re.subn is that re.sub returns a _______ whereas re.subn returns a ______
string, list
list, tuple
string, tuple
tuple, list
40. What will be the output of the following Python code? re.split('mum', 'mumbai*', 1)
Error
[â€, ‘bai*’]
[â€, ‘bai’]
[‘bai*’]
41. What will be the output of the following Python code? re.split(r'(nd)=', 'n1=3.1, n2=5, n3=4.565')
Error
[â€, ‘n1’, ‘3.1, ‘, ‘n2’, ‘5, ‘, ‘n3’, ‘4.565’]
[‘n1’, ‘3.1, ‘, ‘n2’, ‘5, ‘, ‘n3’, ‘4.565’]
[‘3.1, ‘, ‘5, ‘, ‘4.565’]
42. The function of re.search is ____
Matches a pattern at the start of the string
Matches a pattern at the end of the string
Matches a pattern from any part of a string
Such a function does not exist
43. Which of the following functions creates a Python object?
re.compile(str)
re.assemble(str)
re.regex(str)
re.create(str)
44. Which of the following pattern matching modifiers permits whitespace and comments inside the regular expression?
re.L
re.S
re.U
re.X
45. What will be the output of the following Python code? s = 'welcome home' m = re.match(r'(.*)(.*?)', s) print(m.group())
(‘welcome’, ‘home’)
[‘welcome’, ‘home’]
welcome home
[‘welcome’ // ‘home’ ]
46. The function of re.match is _______
Error
Matches a pattern anywhere in the string
Matches a pattern at the end of the string
Matches a pattern at the start of the string
47. The special character B matches the empty string, but only when it is ____
at the beginning or end of a word
not at the beginning or end of a word
at the beginning of the word
at the end of the word
48. Which of the following special characters matches a pattern only at the end of the string?
B
X
A
49. What will be the output of the following Python code? re.match('sp(.*)am', 'spam')
&lt;_sre.SRE_Match object; span=(1, 4), match=’spam’&gt;
&lt;_sre.SRE_Match object; span=(0, 4), match=’spam’&gt;
No output
Error
50. Which of the following special characters represents a comment (that is, the contents of the parenthesis are simply ignores)?
(?:…)
(?=…)
(?!…)
(?#…)
Submit