Python MCQ’s on Modules: SET 16

151. 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)

Check Answer

Answer: c
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.

152. 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

Check Answer

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

153. 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

Check Answer

Answer: a
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.

154. Which of the following functions can accept more than one positional argument?

a) pickle.dumps

b) pickle.loads

c) pickle.dump

d) pickle.load

Check Answer

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

155. 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

Check Answer

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

156. The pickle module defines ______ exceptions and exports _______ classes.

a) 2, 3

b) 3, 4

c) 3, 2

d) 4, 3

Check Answer

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

157. 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

Check Answer

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

158. If __getstate__() returns _______________ the __setstate__() module will not be called on pickling.

a) True value

b) False value

c) ValueError

d) OverflowError

Check Answer

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

159. 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>

Check Answer

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

160. The module _______________ is a comparatively faster implementation of the pickle module.

a) cPickle

b) nPickle

c) gPickle

d) tPickle

Check Answer

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

161. The copy module uses the ___________________ protocol for shallow and deep copy.

a) pickle

b) marshal

c) shelve

d) copyreg

Check Answer

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

Page  1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16 

Leave a Comment