Python Questions and Answers – Pickle Module

This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “Pickle Module”.

1. The process of pickling in Python includes:
a) conversion of a list into a datatable
b) conversion of a byte stream into Python object hierarchy
c) conversion of a Python object hierarchy into byte stream
d) conversion of a datatable into a list
View Answer

Answer: c
Explanation: Pickling is the process of serializing a Python object, that is, conversion of a Python object hierarchy into a byte stream. The reverse of this process is known as unpickling.

2. To sterilize an object hierarchy, the _____________ function must be called. To desterilize a data stream, the ______________ function must be called.
a) dumps(), undumps()
b) loads(), unloads()
c) loads(), dumps()
d) dumps(), loads()
View Answer

Answer: d
Explanation: To sterilize an object hierarchy, the dumps() function must be called. To desterilize a data stream, the loads function must be called.

3. Pick the correct statement regarding pickle and marshal modules.
a) The pickle module supports primarily .pyc files whereas marshal module is used to sterilize Python objects
b) The pickle module keeps track of the objects that have already been sterilized whereas the marshal module does not do this
c) The pickle module cannot be used to sterilize user defined classes and their instances whereas marshal module can be used to perform this task
d) 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
View Answer

Answer: b
Explanation: The correct statement from among the above options is that the python module keeps track of the objects that have already been sterilized whereas the marshal module does not do this. The rest of the statements are incorrect.
advertisement
advertisement

4. What will be the output of the following Python code?

pickle.HIGHEST_PROTOCOL

a) 4
b) 5
c) 3
d) 6
View Answer

Answer: a
Explanation: There are five protocol versions available of the pickle module, namely, 0, 1, 2, 3 and 4. In the code shown above, the highest protocol version is returned, that is, 4.
Note: Join free Sanfoundry classes at Telegram or Youtube

5. Which of the following Python codes will result in an error?

advertisement
object = ‘a’

a) >>> pickle.dumps(object)
b) >>> pickle.dumps(object, 3)
c) >>> pickle.dumps(object, 3, True)
d) >>> pickle.dumps(‘a’, 2)
View Answer

Answer: c
Explanation: The function pickle.dumps requires either 1 or 2 arguments. If any other number of arguments are passed to it, it results in an error. An error is thrown even when no arguments are passed to it.
advertisement

6. Which of the following functions can be used to find the protocol version of the pickle module currently being used?
a) pickle.DEFAULT
b) pickle.CURRENT
c) pickle.CURRENT_PROTOCOL
d) pickle.DEFAULT_PROTOCOL
View Answer

Answer: d
Explanation: The function pickle.DEFAULT_PROTOCOL can be used to find the protocol version of the pickle module currently being used by the system.

7. The output of the following two Python codes is exactly the same.

object
'a'
CODE 1
>>> pickle.dumps('a', 3)
CODE 2
>>> pickle.dumps(object, 3)

a) True
b) False
View Answer

Answer: a
Explanation: The two codes shown above result in the same output, that is, b’\x80\x03X\x01\x00\x00\x00aq\x00.’ Hence this statement is true.

8. Which of the following functions can accept more than one positional argument?
a) pickle.dumps
b) pickle.loads
c) pickle.dump
d) pickle.load
View Answer

Answer: a
Explanation: The functions pickle.loads, pickle.dump and pickle.load accept only one argument. The function pickle.dumps can accept more than one argument.

9. Which of the following functions raises an error when an unpicklable object is encountered by Pickler?
a) pickle.PickleError
b) pickle.PicklingError
c) pickle.UnpickleError
d) pickle.UnpicklingError
View Answer

Answer: b
Explanation: The function pickle.PicklingError raises an error when an unpickable object is encountered by Pickler.

10. The pickle module defines ______ exceptions and exports _______ classes.
a) 2, 3
b) 3, 4
c) 3, 2
d) 4, 3
View Answer

Answer: c
Explanation: The pickle module defines three exceptions, namely, pickle.PickleError, pickle.PicklingError, pickle.UnpickleError and exports two classes, namely, pickle.Pickler and pickle.Unpickler.

11. Which of the following cannot be pickled?
a) Functions which are defined at the top level of a module with lambda
b) Functions which are defined at the top level of a module with def
c) Built-in functions which are defined at the top level of a module
d) Classes which are defined at the top level of a module
View Answer

Answer: a
Explanation: Functions which are defined at the top level of a module with lambda cannot be pickled.

12. If __getstate__() returns _______________ the __setstate__() module will not be called on pickling.
a) True value
b) False value
c) ValueError
d) OverflowError
View Answer

Answer: b
Explanation: If getstate__() returns a false value, the __setstate__() module will not be called on pickling.

13. Lambda functions cannot be pickled because:
a) Lambda functions only deal with binary values, that is, 0 and 1
b) Lambda functions cannot be called directly
c) Lambda functions cannot be identified by the functions of the pickle module
d) All lambda functions have the same name, that is, <lambda>
View Answer

Answer: d
Explanation: Lambda functions cannot be pickled because all the lambda functions have the same name, that is, <lambda>

14. The module _______________ is a comparatively faster implementation of the pickle module.
a) cPickle
b) nPickle
c) gPickle
d) tPickle
View Answer

Answer: a
Explanation: The module cPickle is a comparatively faster implementation of the pickle module.

15. The copy module uses the ___________________ protocol for shallow and deep copy.
a) pickle
b) marshal
c) shelve
d) copyreg
View Answer

Answer: a
Explanation: The copy module uses the pickle protocol for shallow and deep copy.

Sanfoundry Global Education & Learning Series – Python.

To practice all areas of Python, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.