Identify the correct code to read data from the file notes.dat in a binary file:
Identify the correct code to read data from the file notes.dat in a binary file:
Solution
Binary files must be opened in binary read mode, "rb". The data is then unpickled with pickle.load(f1), which takes the file object and returns the original Python object. Option 3 does both, then prints and closes the file.
Option 1: mode "r" opens the file in text mode, so pickle.load() fails because it needs a binary stream.
Option 2: file objects have no load() method. load() belongs to the pickle module.
Option 4: f1.read() runs without error but returns raw bytes, not the original object. It does not unpickle the data.
Related questions:
2025: 14 May Shift 1
2025: 15 May Shift 2
2025: 20 May Shift 1
2026: 18 May Shift 2
2026: 23 May Shift 1
