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)
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
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.
Related questions:
2025: 27 May Shift 2
2026: 11 May Shift 2
2026: 20 May Shift 1