Question:medium

Identify the correct code to read data from the file \texttt{notes.dat} in a binary file:

Show Hint

When working with Pickle: - Always use "rb" to read binary files. - Use \texttt{pickle.load()} to retrieve objects. - Use \texttt{pickle.dump()} to save objects.
Updated On: Feb 16, 2026
  • import pickle f1=open("notes.dat","r") data=pickle.load(f1) print(data) f1.close()
  • import pickle f1=open("notes.dat","rb") data=f1.load() print(data) f1.close()
  • import pickle f1=open("notes.dat","rb") data=pickle.load(f1) print(data) f1.close()
  • import pickle f1=open("notes.dat","rb") data=f1.read() print(data) f1.close()
Show Solution

The Correct Option is C

Solution and Explanation

Step 1: Rules for reading binary files with Pickle.
- Binary files must be opened using mode \texttt{"rb"} (read binary).
- To read pickled data, use \texttt{pickle.load(file\_object)}.
Step 2: Checking each option.
- Option 1: Incorrect. Uses \texttt{"r"} instead of \texttt{"rb"}.
- Option 2: Incorrect. \texttt{f1.load()} is invalid; \texttt{load()} is a module function, not a file method.
- Option 3: Correct. Opens in binary read mode and uses \texttt{pickle.load(f1)}.
- Option 4: Incorrect. Uses \texttt{f1.read()}, which reads raw bytes, not deserialized objects.

Step 3: Correct code.
\begin{verbatim} import pickle f1=open("notes.dat","rb") data=pickle.load(f1) print(data) f1.close() \end{verbatim}
Final Answer: \[\boxed{\text{Option (3)}}\]
Was this answer helpful?
0