Skip to main contentSkip to solution

Arrange the following program statements in correct sequence in order to read the contents from a binary file:

A. var = pickle.load(fileobj)

B. fileobj = open("mybinary.dat", "rb")

C. print(var)

D. import pickle

Solution

✅ Correct Option: 3

D. import pickle comes first, since pickle.load() cannot be used until the module is imported.

B. fileobj = open("mybinary.dat", "rb") opens the file in binary read mode and creates the file object.

A. var = pickle.load(fileobj) reads the data from that open file object into var.

C. print(var) displays the data, so var must already hold a value.


Option 2: prints var before it has been loaded, which raises a NameError.

Option 4: calls pickle.load(fileobj) before fileobj exists.

Option 1: uses pickle before it is imported and fileobj before it is created.

Remember the order for reading a binary file: import, open, load, use. Writing follows the same pattern with "wb" and pickle.dump().

Keyboard Shortcuts

  • Left arrow: Previous question
  • Right arrow: Next question
  • S key: Jump to solution
  • Q key: Jump to question