File Handling MCQ Quiz - Objective Question with Answer for File Handling - Download Free PDF
Last updated on Apr 14, 2025
Latest File Handling MCQ Objective Questions
File Handling Question 1:
Which of the following represents mode of both writing and reading in binary format in file?
Answer (Detailed Solution Below)
Option 1 : wb+
File Handling Question 1 Detailed Solution
The correct answer is wb+.
- The mode wb+ stands for "write binary plus", meaning it opens a file for both reading and writing in binary mode.
- When a file is opened in wb+ mode, it allows you to read from and write to the file in binary format.
- Binary mode means the data is read and written in binary format, which is suitable for non-text files like images or executable files.
- Opening a file in wb+ mode will create the file if it does not exist or overwrite the file if it already exists.
- In contrast, the mode w opens a file for writing in text mode, but it does not allow reading.
- The mode a+ opens a file for reading and appending (writing at the end) in text mode.
- The mode w+ opens a file for both reading and writing in text mode, but not in binary format.
- Binary mode (using 'b' in the mode) is essential when dealing with binary files to ensure data is read and written correctly without any encoding issues.
Here is an example of how to use the wb+ mode in Python:
# Open a file for both reading and writing in binary mode
with open('example.bin', 'wb+') as file:
# Write binary data to the file
file.write(b'This is a test.')
# Move the file pointer to the beginning
file.seek(0)
# Read the binary data from the file
content = file.read()
print(content)
Top File Handling MCQ Objective Questions
File Handling Question 2:
Which of the following represents mode of both writing and reading in binary format in file?
Answer (Detailed Solution Below)
Option 1 : wb+
File Handling Question 2 Detailed Solution
The correct answer is wb+.
- The mode wb+ stands for "write binary plus", meaning it opens a file for both reading and writing in binary mode.
- When a file is opened in wb+ mode, it allows you to read from and write to the file in binary format.
- Binary mode means the data is read and written in binary format, which is suitable for non-text files like images or executable files.
- Opening a file in wb+ mode will create the file if it does not exist or overwrite the file if it already exists.
- In contrast, the mode w opens a file for writing in text mode, but it does not allow reading.
- The mode a+ opens a file for reading and appending (writing at the end) in text mode.
- The mode w+ opens a file for both reading and writing in text mode, but not in binary format.
- Binary mode (using 'b' in the mode) is essential when dealing with binary files to ensure data is read and written correctly without any encoding issues.
Here is an example of how to use the wb+ mode in Python:
# Open a file for both reading and writing in binary mode
with open('example.bin', 'wb+') as file:
# Write binary data to the file
file.write(b'This is a test.')
# Move the file pointer to the beginning
file.seek(0)
# Read the binary data from the file
content = file.read()
print(content)