π₯ Binary File Quiz β Class 12 Computer Science (Code 083)
Practice 20 CBSE Class 12 Computer Science (Code 083) MCQs on binary file handling β opening a binary file in different modes (rb, rb+, wb, wb+, ab), closing files, the pickle module (dump() and load()), reading/writing/updating/appending binary data, and handling EOFError when reading multiple records.
This set includes 4 board-style predict-the-output questions with full working code β including the classic multi-record dump/load loop and the rb+ vs wb+ truncation trap. Every result below has been verified by actually running the code in Python. Tap any question to reveal the answer with a clear explanation.
π Opening Files
How do you open a binary file for reading in Python?
- file = open(“example.bin”, “r”)
- file = open(“example.bin”, “rb”)
- file = open(“example.bin”, “w”)
- file = open(“example.bin”, “wb”)
Show Answer & Explanation
β Answer: (b) file = open(“example.bin”, “rb”)
“rb” opens a file for reading in binary mode. Using plain "r" (text mode) on a binary file can corrupt the data, since Python tries to decode it as text.
π§ File Modes
Which open mode allows both reading and writing in a binary file WITHOUT truncating (emptying) the file?
- rb
- rb+
- wb
- wb+
Show Answer & Explanation
β Answer: (b) rb+
rb+ opens an existing binary file for both reading and writing while preserving its existing content. (wb+ also allows both, but truncates the file to empty first.)
π₯ pickle Module
What is the purpose of the pickle module in Python when working with binary files?
- To compress the binary data
- To serialize and deserialize Python objects
- To encrypt the binary data
- To create a binary file automatically
Show Answer & Explanation
β Answer: (b) To serialize and deserialize Python objects
The pickle module converts Python objects (lists, dictionaries, custom objects) into a byte stream (serialization) that can be saved to a binary file, and converts them back into live Python objects again (deserialization) when loaded.
π Closing Files
How can you close an open binary file in Python?
- file.close()
- close(file)
- file.close(file)
- close(“file.bin”)
Show Answer & Explanation
β Answer: (a) file.close()
file.close() β exactly the same syntax used for text files, since it’s a method on the file object itself.
π₯ pickle Module
Which method of the pickle module is used to write a Python object to a binary file?
- pickle.write()
- pickle.dump()
- pickle.save()
- pickle.store()
Show Answer & Explanation
β Answer: (b) pickle.dump()
pickle.dump(object, file) serializes the given Python object and writes it directly to the open binary file.
π§ File Modes
In the context of binary files, what does the “rb” open mode signify?
- Read mode (text)
- Read and Write mode
- Binary read mode
- Binary read and write mode
Show Answer & Explanation
β Answer: (c) Binary read mode
“rb” specifically means binary read mode β for reading a file’s raw bytes rather than text.
π§ File Modes
What does the “wb+” open mode do when opening a binary file?
- Write mode only
- Read mode only
- Append mode
- Read and Write mode (but the file is truncated to empty first)
Show Answer & Explanation
β Answer: (d) Read and Write mode (but the file is truncated to empty first)
“wb+” allows both reading and writing in binary mode, but just like its text-mode counterpart "w+", it immediately truncates the file to empty the moment it’s opened.
π Reading Files
Which method is used to read raw data from a binary file in Python?
- file.read()
- file.readline()
- file.readlines()
- file.readbinary()
Show Answer & Explanation
β Answer: (a) file.read()
file.read() works for binary files too β it returns the raw bytes as a bytes object instead of a string. (readbinary() isn’t a real method; readline()/readlines() are text-file concepts that don’t apply meaningfully to raw binary data.)
π₯ pickle Module
What does the load() method of the pickle module do?
- Reads raw bytes from a binary file
- Loads (deserializes) a Python object from a binary file
- Loads an entire binary file into memory as text
- Loads a module into the program
Show Answer & Explanation
β Answer: (b) Loads (deserializes) a Python object from a binary file
pickle.load(file) reads the next serialized object from the binary file and reconstructs it back into a live Python object β the exact reverse of dump().
βοΈ Writing Files
How can you write raw binary data to a file using the “wb” mode?
- file.writebinary(data)
- file.write(data)
- file.writelines(data)
- file.writebytes(data)
Show Answer & Explanation
β Answer: (b) file.write(data)
file.write(data) is used for binary files too β as long as data is a bytes object (or, when using pickle, you use pickle.dump() instead of calling write() directly).
π§ File Modes
What operation does the “ab” open mode perform when opening a binary file?
- Append mode (binary)
- Write mode
- Read mode
- Read and Write mode
Show Answer & Explanation
β Answer: (a) Append mode (binary)
“ab” opens a binary file in append mode β new data is added to the end, and existing content is preserved, just like "a" does for text files.
π§ File Modes
In the context of binary files, what does the “wb” open mode signify?
- Write mode (text)
- Read mode
- Append mode
- Binary write mode
Show Answer & Explanation
β Answer: (d) Binary write mode
“wb” means binary write mode β it creates a new file (or truncates an existing one) for writing raw bytes.
π₯ pickle Module
What is the purpose of the dump() method of the pickle module?
- Writes raw text data to a binary file
- Serializes a Python object and writes it into a binary file
- Dumps (deletes) the contents of a binary file
- Deletes a binary file entirely
Show Answer & Explanation
β Answer: (b) Serializes a Python object and writes it into a binary file
pickle.dump(object, file) serializes the given object and writes that serialized data into the binary file β it does not delete or dump out anything, despite the name.
π Updating Files
How can you update data at a specific position within a binary file?
- Use file.update(position, data)
- Use file.seek(position) followed by file.write(data)
- Use file.insert(position, data)
- Use file.append(data)
Show Answer & Explanation
β Answer: (b) Use file.seek(position) followed by file.write(data)
You first move the cursor to the exact byte position with seek(position), then write(data) to overwrite the bytes starting from there. Python has no dedicated update() or insert() file method.
βοΈ Writing Files
Which approach is used to append data to the end of a binary file in Python?
- file.add(data)
- Open the file in ‘ab’ mode and use file.write(data) or pickle.dump()
- file.append(data)
- file.extend(data)
Show Answer & Explanation
β Answer: (b) Open the file in ‘ab’ mode and use file.write(data) or pickle.dump()
There is no dedicated append() method for files β instead, you open the file in ‘ab’ (append binary) mode and then simply use write() or pickle.dump(), which adds to the end automatically.
π₯ pickle Module
What exception is raised when pickle.load() is called but there is no more data left to read (end of file)?
- EOFError
- IndexError
- StopIteration
- FileNotFoundError
Show Answer & Explanation
β Answer: (a) EOFError
EOFError (βEnd Of File Errorβ) is raised when pickle.load() tries to read but the file has no more serialized objects left β this is exactly how you detect βno more recordsβ when reading multiple objects from one file.
β Board-Style Programs
What does the following program print?
import pickle
with open("data.dat", "wb") as f:
pickle.dump({"name": "Riya", "age": 16}, f)
with open("data.dat", "rb") as f:
obj = pickle.load(f)
print(obj)- b'{“name”: “Riya”, “age”: 16}’
- {‘name’: ‘Riya’, ‘age’: 16}
- “name”, “Riya”, “age”, 16
- Error, since dictionaries can’t be pickled
Show Answer & Explanation
β Answer: (b) {‘name’: ‘Riya’, ‘age’: 16}
pickle.dump() serializes the dictionary exactly as-is, and pickle.load() reconstructs it perfectly back into a real Python dictionary: {‘name’: ‘Riya’, ‘age’: 16}. Unlike text files, there’s no need to manually convert it to/from a string.
β Board-Style Programs
What does the following program print? (Storing and reading multiple records until EOFError.)
import pickle
with open("multi.dat", "wb") as f:
pickle.dump({"id": 1, "name": "Aman"}, f)
pickle.dump({"id": 2, "name": "Riya"}, f)
records = []
with open("multi.dat", "rb") as f:
while True:
try:
records.append(pickle.load(f))
except EOFError:
break
print(records)- Only the first dumped record
- Only the last dumped record
- Both records, as a list of two dictionaries
- Error, since pickle can only store one object per file
Show Answer & Explanation
β Answer: (c) Both records, as a list of two dictionaries
Calling pickle.dump() multiple times on the same open file writes multiple separate objects, one after another. Reading them back with repeated pickle.load() calls inside a loop, catching EOFError when there’s nothing left, retrieves every record: [{'id': 1, 'name': 'Aman'}, {'id': 2, 'name': 'Riya'}]. This is the standard technique for storing multiple records in one binary file.
β Board-Style Programs
What does the following program print? (Searching for a record where id == 2, using the records list from multiple pickle.dump() calls.)
records = [{"id": 1, "name": "Aman"}, {"id": 2, "name": "Riya"}, {"id": 3, "name": "Kabir"}]
found = [r for r in records if r["id"] == 2]
print(found)- []
- [{‘id’: 2, ‘name’: ‘Riya’}]
- [{‘id’: 1, ‘name’: ‘Aman’}, {‘id’: 3, ‘name’: ‘Kabir’}]
- Error
Show Answer & Explanation
β Answer: (b) [{‘id’: 2, ‘name’: ‘Riya’}]
This is the classic search a binary file suggested program: read all records into a list, then filter for the ones matching a condition. Only the record with id == 2 matches: [{‘id’: 2, ‘name’: ‘Riya’}].
β Board-Style Programs
What does the following program print? (The classic file-mode truncation trap, applied to binary files.)
import pickle
with open("t.dat", "wb") as f:
pickle.dump("Original Data", f)
with open("t.dat", "wb+") as f:
content = f.read()
print(repr(content))- b’Original Data’ (the pickled content)
- ‘Original Data’
- b” (an empty bytes object)
- Error, since wb+ cannot be used with read()
Show Answer & Explanation
β Answer: (c) b” (an empty bytes object)
Just like with text files, opening in “wb+” mode immediately truncates the binary file to empty the moment it’s opened β so reading right after gives b”, an empty bytes object. To read existing data without erasing it, you must use "rb+" instead.
