Python Binary Files MCQs | 25 Practice Questions with Answers | CBSE Class 12 Computer Science
Class 12 · Computer Science
Python Binary Files MCQs
Practice these multiple-choice questions to strengthen your understanding of Python Binary Files. Click Show Answer to reveal the correct answer and explanation.
Q1. What is a binary file?
A. A file that stores only text.
B. A file that stores data in binary format (bytes).
C. A file that stores only numbers.
D. A file that stores only images.
Show Answer
Answer: B. A file that stores data in binary format (bytes).
Explanation: Binary files store data in binary form and are commonly used to store objects efficiently.
Q2. Which module is commonly used to read and write Python objects in binary files?
A. math
B. random
C. pickle
D. os
Show Answer
Answer: C. pickle
Explanation: The pickle module serializes and deserializes Python objects.
Q3. Which function is used to write an object into a binary file?
A. write()
B. dump()
C. load()
D. append()
Show Answer
Answer: B. dump()
Explanation: The pickle.dump() function writes a Python object into a binary file.
Q4. Which function is used to read an object from a binary file?
A. read()
B. write()
C. load()
D. dump()
Show Answer
Answer: C. load()
Explanation: The pickle.load() function reads a serialized object from a binary file.
Q5. Which file mode is used to open a binary file for reading only?
A. rb
B. wb
C. ab
D. rb+
Show Answer
Answer: A. rb
Explanation: The rb mode opens an existing binary file for reading.
Q6. Which mode opens a binary file for writing and deletes existing contents?
A. rb
B. wb
C. ab
D. rb+
Show Answer
Answer: B. wb
Explanation: The wb mode creates a new binary file or overwrites an existing one.
Q7. Which mode opens a binary file for appending data?
A. rb
B. wb
C. ab
D. rb+
Show Answer
Answer: C. ab
Explanation: The ab mode appends new data at the end of a binary file.
Q8. Which binary file mode allows both reading and writing without deleting existing contents?
A. wb
B. rb+
C. ab
D. wb+
Show Answer
Answer: B. rb+
Explanation: The rb+ mode allows both reading and writing while preserving existing data.
Q9. Which binary file mode allows reading and writing but removes existing contents?
A. rb+
B. wb+
C. ab+
D. rb
Show Answer
Answer: B. wb+
Explanation: The wb+ mode opens a binary file for both reading and writing but truncates existing data.
Q10. Which binary file mode allows reading as well as appending data?
A. rb
B. wb
C. ab+
D. rb+
Show Answer
Answer: C. ab+
Explanation: The ab+ mode allows both reading and appending data to a binary file.
Q11. Which statement is used to import the pickle module?
A. include pickle
B. using pickle
C. import pickle
D. open pickle
Show Answer
Answer: C. import pickle
Explanation: The import pickle statement imports the pickle module for binary file operations.
Q12. Which function is used to store a Python object into a binary file?
A. write()
B. dump()
C. save()
D. store()
Show Answer
Answer: B. dump()
Explanation: The pickle.dump() function serializes and stores a Python object into a binary file.
Q13. Which function retrieves a Python object from a binary file?
A. load()
B. read()
C. open()
D. write()
Show Answer
Answer: A. load()
Explanation: The pickle.load() function deserializes and returns an object from a binary file.
Q14. What is the output of the following code?
import pickle
f=open("student.dat","wb")
pickle.dump({"Roll":101},f)
f.close()
print("Done")
A. Error
B. Done
C. {'Roll':101}
D. Nothing
Show Answer
Answer: B. Done
Explanation: The dictionary is successfully stored in the binary file, and the program prints Done.
Q15. Which operation is commonly used to locate a particular record in a binary file?
A. Search
B. Compile
C. Execute
D. Import
Show Answer
Answer: A. Search
Explanation: Records in a binary file are read one by one and compared to perform a search operation.
Q16. Which operation is used to add new records at the end of an existing binary file?
A. Search
B. Read
C. Append
D. Delete
Show Answer
Answer: C. Append
Explanation: The append operation adds new records to the end of the binary file without disturbing existing records.
Q17. Which operation is used to modify an existing record in a binary file?
A. Append
B. Search
C. Update
D. Create
Show Answer
Answer: C. Update
Explanation: The update operation changes the values of an existing record stored in the binary file.
Q18. Which operation is performed first before updating a record in a binary file?
A. Delete the file
B. Search for the required record
C. Close the file
D. Create another file
Show Answer
Answer: B. Search for the required record
Explanation: Before updating a record, the required record must first be located.
Q19. Which method is used to close a binary file?
A. stop()
B. exit()
C. close()
D. end()
Show Answer
Answer: C. close()
Explanation: The close() method closes both text and binary files.
Q20. Which statement about binary files is correct?
A. They store data in binary format.
B. They are commonly used with the pickle module.
C. They can store Python objects directly.
D. All of these.
Show Answer
Answer: D. All of these.
Explanation: Binary files store data in binary format and, with the help of the pickle module, can efficiently store and retrieve Python objects.
Q21. What will be the output of the following code?
import pickle
f=open("student.dat","wb")
pickle.dump({"Name":"Aman"},f)
f.close()
print("Record Saved")
A. {'Name':'Aman'}
B. Record Saved
C. Error
D. None
Show Answer
Answer: B. Record Saved
Explanation: The dictionary is successfully stored in the binary file using pickle.dump(), and the program prints Record Saved.
Q22. What is the correct sequence for reading records from a binary file?
A. Open the file → Use pickle.load() repeatedly → Close the file.
B. Close the file → Open the file → Read the records.
C. Open the file → Use pickle.dump() → Close the file.
D. Open the file → Use write() → Close the file.
Show Answer
Answer: A.
Explanation: Binary records are read using pickle.load() after opening the file in an appropriate read mode.
Q23. Which file mode is generally used while searching records in a binary file?
A. wb
B. rb
C. ab
D. wb+
Show Answer
Answer: B. rb
Explanation: Searching requires reading records, so the binary file is generally opened in rb mode.
Q24. Which file mode is commonly used while updating records in a binary file?
A. rb+
B. wb
C. rb
D. ab
Show Answer
Answer: A. rb+
Explanation: The rb+ mode allows both reading and writing without deleting existing records, making it suitable for updating data.
Q25. Which statement about binary file handling in Python is correct?
A. Binary files use the pickle module to store Python objects.
B. dump() writes objects to a binary file.
C. load() reads objects from a binary file.
D. All of these.
Show Answer
Answer: D. All of these.
Explanation: The pickle module is used for object serialization. The dump() function stores objects, while load() retrieves them from binary files.
Answer Key
| Q.No. | Answer | Q.No. | Answer | Q.No. | Answer |
|---|---|---|---|---|---|
| 1 | B | 10 | C | 19 | C |
| 2 | C | 11 | C | 20 | D |
| 3 | B | 12 | B | 21 | B |
| 4 | C | 13 | A | 22 | A |
| 5 | A | 14 | B | 23 | B |
| 6 | B | 15 | A | 24 | A |
| 7 | C | 16 | C | 25 | D |
| 8 | B | 17 | C | ||
| 9 | B | 18 | B |
Practice Tips
- Remember the purpose of each binary file mode:
rb,rb+,wb,wb+,ab, andab+. - Always import the
picklemodule before performing binary file operations. - Revise the difference between
pickle.dump()(write object) andpickle.load()(read object). - Practice the basic binary file operations:
- Create (Write)
- Read
- Search
- Append
- Update
- Remember that binary files store complete Python objects such as dictionaries, lists, and tuples in serialized form.
- Always close the binary file after use or use the
withstatement whenever appropriate. - Practice output-based and record-based questions, as these are frequently asked in CBSE Class 12 Computer Science examinations.