250+ TOP MCQs on Python Pickle Module and Answers

Python Multiple Choice Questions 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
Answer: c
Clarification: Pickling is the process of sterilizing a Python object, that is, conversion of a byte stream into Python object hierarchy. 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()
Answer: d
Clarification: 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
Answer: b
Clarification: 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.

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

a) 4
b) 5
c) 3
d) 6
Answer: a
Clarification: 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.

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

a) >>> pickle.dumps(object)
b) >>> pickle.dumps(object, 3)
c) >>> pickle.dumps(object, 3, True)
d) >>> pickle.dumps(‘a’, 2)
Answer: c
Clarification: 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.

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
Answer: d
Clarification: 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
Answer: a
Clarification: The two codes shown above result in the same output, that is, b’x80x03Xx01x00x00x00aqx00.’ 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
Answer: a
Clarification: 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
Answer: b
Clarification: 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
Answer: c
Clarification: 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
Answer: a
Clarification: 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
Answer: b
Clarification: 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,
Answer: d
Clarification: Lambda functions cannot be pickled because all the lambda functions have the same name, that is,

14. The module _______________ is a comparatively faster implementation of the pickle module.
a) cPickle
b) nPickle
c) gPickle
d) tPickle
Answer: a
Clarification: 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
Answer: a
Clarification: The copy module uses the pickle protocol for shallow and deep copy.

Leave a Reply

Your email address will not be published. Required fields are marked *