Question:

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: Sep 18, 2025
  • 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()
Hide Solution
collegedunia
Verified By Collegedunia

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: Uses \texttt{"r"} instead of \texttt{"rb"}. Wrong.
- Option 2: \texttt{f1.load()} is invalid because \texttt{load()} is from the \texttt{pickle} module, not a file method. Wrong.
- Option 3: Correct syntax → opens in binary read mode and uses \texttt{pickle.load(f1)}.
- Option 4: Uses \texttt{f1.read()}, which reads raw bytes, not deserialized objects. Wrong.

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
0

Questions Asked in CUET exam

View More Questions