Skip to main contentSkip to solution

Arrange the following code segment to read a binary file in the correct order.

(A) import pickle

(B) obj=pickle.load(fileobj)

(C) fileobj=open("myfile.dat","rb")

(D) print(obj)

Solution

✅ Correct Option: 2

(A) import pickle

(C) fileobj=open("myfile.dat","rb")

(B) obj=pickle.load(fileobj)

(D) print(obj)

The module must be imported before use, and the file must be opened in "rb" mode before pickle.load() can read from it. The object is printed only after it has been loaded.


Options 1 and 3: pickle.load(fileobj) comes before fileobj is created, which gives a NameError.

Option 4: print(obj) comes before obj is loaded, which also gives a NameError.

Keyboard Shortcuts

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