Test Series - python

Test Number 90/108

Q: 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
Solution: 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.
Q: 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()
Solution: To sterilize an object hierarchy, the dumps() function must be called. To desterilize a data stream, the loads function must be called.
Q: 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
Solution: 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.
Q: What will be the output of the following Python code?

pickle.HIGHEST_PROTOCOL
A. 4
B. 5
C. 3
D. 6
Solution: 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.
Q: Which of the following Python codes will result in an error?

object = ‘a’
A. >>> pickle.dumps(object)
B. >>> pickle.dumps(object, 3)
C. >>> pickle.dumps(object, 3, True)
D. >>> pickle.dumps(‘a’, 2)
Solution: 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.
Q: 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
Solution: The function pickle.DEFAULT_PROTOCOL can be used to find the protocol version of the pickle module currently being used by the system.
Q: Which of the following functions can accept more than one positional argument?
A. pickle.dumps
B. pickle.loads
C. pickle.dump
D. pickle.load
Solution: The functions pickle.loads, pickle.dump and pickle.load accept only one argument. The function pickle.dumps can accept more than one argument.
Q: 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
Solution: The function pickle.PicklingError raises an error when an unpickable object is encountered by Pickler.
Q: The pickle module defines ______ exceptions and exports _______ classes.
A. 2, 3
B. 3, 4
C. 3, 2
D. 4, 3
Solution: The pickle module defines three exceptions, namely, pickle.PickleError, pickle.PicklingError, pickle.UnpickleError and exports two classes, namely, pickle.Pickler and pickle.Unpickler.

You Have Score    /9