Binary File Quiz Class 12

πŸ₯’ 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πŸ”§ File ModesπŸ₯’ pickle ModuleπŸ“– Reading Files✍️ Writing FilesπŸ”„ Updating Files⭐ Board-Style Programs
Q1
πŸ“‚ Opening Files

How do you open a binary file for reading in Python?

  1. file = open(“example.bin”, “r”)
  2. file = open(“example.bin”, “rb”)
  3. file = open(“example.bin”, “w”)
  4. 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.

Q2
πŸ”§ File Modes

Which open mode allows both reading and writing in a binary file WITHOUT truncating (emptying) the file?

  1. rb
  2. rb+
  3. wb
  4. 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.)

Q3
πŸ₯’ pickle Module

What is the purpose of the pickle module in Python when working with binary files?

  1. To compress the binary data
  2. To serialize and deserialize Python objects
  3. To encrypt the binary data
  4. 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.

Q4
πŸ”’ Closing Files

How can you close an open binary file in Python?

  1. file.close()
  2. close(file)
  3. file.close(file)
  4. 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.

Q5
πŸ₯’ pickle Module

Which method of the pickle module is used to write a Python object to a binary file?

  1. pickle.write()
  2. pickle.dump()
  3. pickle.save()
  4. 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.

Q6
πŸ”§ File Modes

In the context of binary files, what does the “rb” open mode signify?

  1. Read mode (text)
  2. Read and Write mode
  3. Binary read mode
  4. 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.

Q7
πŸ”§ File Modes

What does the “wb+” open mode do when opening a binary file?

  1. Write mode only
  2. Read mode only
  3. Append mode
  4. 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.

Q8
πŸ“– Reading Files

Which method is used to read raw data from a binary file in Python?

  1. file.read()
  2. file.readline()
  3. file.readlines()
  4. 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.)

Q9
πŸ₯’ pickle Module

What does the load() method of the pickle module do?

  1. Reads raw bytes from a binary file
  2. Loads (deserializes) a Python object from a binary file
  3. Loads an entire binary file into memory as text
  4. 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().

Q10
✍️ Writing Files

How can you write raw binary data to a file using the “wb” mode?

  1. file.writebinary(data)
  2. file.write(data)
  3. file.writelines(data)
  4. 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).

Q11
πŸ”§ File Modes

What operation does the “ab” open mode perform when opening a binary file?

  1. Append mode (binary)
  2. Write mode
  3. Read mode
  4. 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.

Q12
πŸ”§ File Modes

In the context of binary files, what does the “wb” open mode signify?

  1. Write mode (text)
  2. Read mode
  3. Append mode
  4. 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.

Q13
πŸ₯’ pickle Module

What is the purpose of the dump() method of the pickle module?

  1. Writes raw text data to a binary file
  2. Serializes a Python object and writes it into a binary file
  3. Dumps (deletes) the contents of a binary file
  4. 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.

Q14
πŸ”„ Updating Files

How can you update data at a specific position within a binary file?

  1. Use file.update(position, data)
  2. Use file.seek(position) followed by file.write(data)
  3. Use file.insert(position, data)
  4. 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.

Q15
✍️ Writing Files

Which approach is used to append data to the end of a binary file in Python?

  1. file.add(data)
  2. Open the file in ‘ab’ mode and use file.write(data) or pickle.dump()
  3. file.append(data)
  4. 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.

Q16
πŸ₯’ pickle Module

What exception is raised when pickle.load() is called but there is no more data left to read (end of file)?

  1. EOFError
  2. IndexError
  3. StopIteration
  4. 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.

Q17
⭐ 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)
  1. b'{“name”: “Riya”, “age”: 16}’
  2. {‘name’: ‘Riya’, ‘age’: 16}
  3. “name”, “Riya”, “age”, 16
  4. 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.

Q18
⭐ 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)
  1. Only the first dumped record
  2. Only the last dumped record
  3. Both records, as a list of two dictionaries
  4. 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.

Q19
⭐ 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)
  1. []
  2. [{‘id’: 2, ‘name’: ‘Riya’}]
  3. [{‘id’: 1, ‘name’: ‘Aman’}, {‘id’: 3, ‘name’: ‘Kabir’}]
  4. 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’}].

Q20
⭐ 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))
  1. b’Original Data’ (the pickled content)
  2. ‘Original Data’
  3. b” (an empty bytes object)
  4. 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.

Jitendra Singh
βœ” Verified Educator

Jitendra Singh

Founder of CBSEPython.in

I help CBSE Class 9–12 students learn Python, Information Technology, Artificial Intelligence and Computer Science through easy notes, quizzes, MCQs and sample papers.

Read More About Me β†’