Skip to main contentSkip to solution

Identify the correct sequence of working in binary files:

(A) f=open("myfile.dat","rb")

(B) f.close()

(C) import pickle

(D) data=pickle.load(f)

Solution

✅ Correct Option: 4

The module must be imported first, then the file opened, then read, then closed. The sequence is (C), (A), (D), (B).

One line per step in order:

(C) import pickle: the module has to be available before pickle.load() can be used.

(A) f=open("myfile.dat","rb"): opens the binary file in read mode.

(D) data=pickle.load(f): reads the pickled object from the open file.

(B) f.close(): closes the file after use.


Options 1 and 3: close the file before reading, or read before opening, which gives an error.

Option 2: calls f.close() before pickle.load(f), so loading would fail with a "closed file" error.

Keyboard Shortcuts

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