π File Handling Quiz 1 β Introduction to Files (Class 12 CS, Code 083)
Practice 20 CBSE Class 12 Computer Science (Code 083) MCQs on introduction to file handling β text vs binary vs CSV files, opening files in different modes (r, w, a, r+, rb, wb), relative vs absolute paths, the with statement, the os module, read()/readline()/readlines(), write()/writelines(), and the csv module.
This set includes 2 board-style predict-the-output questions with full working code. Every result below has been verified by actually running the code in Python. Tap any question to reveal the answer with a clear explanation.
π File Basics
What is the primary purpose of using files in programming?
- To slow down program execution
- To store data permanently
- To create intentional errors
- To display information on the console
Show Answer & Explanation
β Answer: (b) To store data permanently
Files let a program store data permanently β unlike variables, which disappear once the program ends, data written to a file survives and can be read again later.
π File Types
Which type of file contains human-readable text and can be opened with a text editor?
- Binary file
- CSV file
- Text file
- Compressed file
Show Answer & Explanation
β Answer: (c) Text file
A text file stores data as plain, human-readable characters β you can open it in Notepad or any text editor and read it directly.
π File Types
What is the main characteristic of a binary file?
- Human-readable text
- Compressed format
- Machine-readable format
- Tabular data
Show Answer & Explanation
β Answer: (c) Machine-readable format
A binary file stores data in a machine-readable format (raw bytes) rather than plain text β images, audio, and executables are all binary files, and they look like gibberish if opened in a text editor.
π Opening Files
In Python, how do you open a file for reading?
- file = open(“filename.txt”, “w”)
- file = open(“filename.txt”, “r”)
- file = open(“filename.txt”, “a”)
- file = open(“filename.txt”, “rb”)
Show Answer & Explanation
β Answer: (b) file = open(“filename.txt”, “r”)
Mode “r” opens a file for reading in text mode β the default and most common mode for reading. ("w" is for writing/overwriting, "a" for appending, and "rb" for reading in binary mode.)
π§ File Modes
What is the purpose of the “wb” mode when opening a file in Python?
- Write and append mode
- Binary write mode
- Binary read mode
- Write mode (text)
Show Answer & Explanation
β Answer: (b) Binary write mode
“wb” opens a file for writing in binary mode β used for non-text data like images or serialized objects, as opposed to "w" which writes plain text.
π File Types
Which type of file is commonly used for storing tabular data, such as spreadsheets?
- Text file
- Binary file
- CSV file
- XML file
Show Answer & Explanation
β Answer: (c) CSV file
A CSV (Comma-Separated Values) file is the standard, simple format for storing tabular/row-column data, and can be opened directly in spreadsheet software like Excel.
π File Types
What does CSV stand for in the context of file formats?
- Comma-Separated Values
- Compressed Storage Variant
- Common System Variables
- Centralized Storage Vector
Show Answer & Explanation
β Answer: (a) Comma-Separated Values
CSV stands for Comma-Separated Values β each line is a row of data, with individual values separated by commas.
πΊοΈ File Paths
What is the purpose of an absolute file path?
- To specify the file’s location relative to the current working directory
- To specify the file’s location irrespective of the current working directory
- To create intentional errors
- To slow down program execution
Show Answer & Explanation
β Answer: (b) To specify the file’s location irrespective of the current working directory
An absolute path gives the complete location of a file starting from the root/drive (e.g. C:/Users/Riya/file.txt), so it works no matter what the program’s current working directory is.
πΊοΈ File Paths
Which of the following is an example of a relative file path?
- “C:/Documents/file.txt”
- “/home/user/file.txt”
- “data/file.txt”
- Both “data/file.txt” and just “file.txt” are relative paths
Show Answer & Explanation
β Answer: (d) Both “data/file.txt” and just “file.txt” are relative paths
A relative path is defined with respect to the current working directory rather than starting from a root or drive. Both "data/file.txt" (relative to a subfolder) and a bare "file.txt" (relative to the current folder) qualify as relative paths β verified with os.path.isabs(), which returns False for both. (This question is corrected from the original, whose intended single answer overlooked that a bare filename is also technically a relative path.)
π Closing Files
In Python, how can you close an open file?
- close(file)
- file.close()
- close(“file”)
- file.close(“file”)
Show Answer & Explanation
β Answer: (b) file.close()
file.close() is the correct syntax β close() is a method called on the file object, not a standalone function.
π§ File Modes
What does opening a file in ‘a’ (append) mode do?
- Overwrites the entire file with new content
- Opens the file only for reading, never writing
- Adds new content to the end of the file, keeping existing content intact
- Deletes the file immediately
Show Answer & Explanation
β Answer: (c) Adds new content to the end of the file, keeping existing content intact
‘a’ mode opens the file for writing but preserves everything already in it β any new data you write gets added at the end, rather than replacing the existing content (which is what 'w' mode would do).
π§ File Modes
What does opening a file in ‘r+’ mode allow you to do?
- Only read the file, nothing else
- Only write to the file, nothing else
- Both read AND write to the file, without erasing its existing content
- Only append new content
Show Answer & Explanation
β Answer: (c) Both read AND write to the file, without erasing its existing content
‘r+’ opens an existing file for both reading and writing β unlike 'w', it does not erase the file’s existing content when opened.
β Best Practices
What is the advantage of using the ‘with’ statement when working with files in Python?
- It makes the file permanently read-only
- It automatically closes the file once the block finishes, even if an error occurs
- It converts text files into binary files automatically
- It has no real advantage over open()/close()
Show Answer & Explanation
β Answer: (b) It automatically closes the file once the block finishes, even if an error occurs
with open(...) as f: is a context manager that automatically closes the file once the indented block ends β even if an exception is raised partway through β so you never risk forgetting to call close().
π₯οΈ os Module
Which function from the os module returns the current working directory?
- os.getcwd()
- os.currentdir()
- os.pwd()
- os.directory()
Show Answer & Explanation
β Answer: (a) os.getcwd()
os.getcwd() (βget current working directoryβ) returns the folder the Python script is currently running from β the reference point used to resolve relative paths.
π₯οΈ os Module
Which function is used to check whether a given file or path actually exists?
- os.path.check()
- os.path.exists()
- os.file.found()
- os.verify()
Show Answer & Explanation
β Answer: (b) os.path.exists()
os.path.exists(path) returns True or False depending on whether the given file or directory is actually present on disk β useful to check before trying to open a file.
π Reading Files
What is the difference between read(), readline(), and readlines()?
- They all do exactly the same thing
- read() gets the whole file as one string; readline() gets just one line; readlines() gets every line as a list
- read() only works on binary files; the other two only work on text files
- readlines() can only be used once per program
Show Answer & Explanation
β Answer: (b) read() gets the whole file as one string; readline() gets just one line; readlines() gets every line as a list
read() returns the entire file content as a single string. readline() returns just the next single line each time it’s called. readlines() returns every line as a list of strings, e.g. ['line1\n', 'line2\n'].
βοΈ Writing Files
What is the difference between write() and writelines()?
- write() writes a single string; writelines() writes each string from an iterable (like a list) one after another
- They are identical in every way
- writelines() automatically adds line breaks between items, write() never can
- write() only works in binary mode
Show Answer & Explanation
β Answer: (a) write() writes a single string; writelines() writes each string from an iterable (like a list) one after another
write() writes exactly the string you give it. writelines() takes an iterable (like a list of strings) and writes each item in sequence β but note it does not automatically insert newlines between them; you must include \n yourself if needed.
π CSV Files
Which module do you need to import to read and write CSV files conveniently in Python?
- file
- csv
- table
- spreadsheet
Show Answer & Explanation
β Answer: (b) csv
The built-in csv module (import csv) provides csv.reader() and csv.writer() to handle comma-separated data properly, including edge cases like commas inside quoted fields.
β Board-Style Programs
What does the following program print? (Assume the file doesn’t exist yet before this code runs.)
with open("notes.txt", "w") as f:
f.write("Hello\n")
with open("notes.txt", "a") as f:
f.write("World\n")
with open("notes.txt", "r") as f:
print(f.read())- Hello
- World
- Hello\nWorld\n printed as two lines: Hello then World
- Error, since the file didn’t exist initially
Show Answer & Explanation
β Answer: (c) Hello\nWorld\n printed as two lines: Hello then World
The first block creates and writes βHelloβ. The second block, opened in append (‘a’) mode, adds βWorldβ without erasing the first line. Reading the file back shows both lines: Hello then World.
β Board-Style Programs
What does the following program print?
with open("data.txt", "w") as f:
f.writelines(["line1\n", "line2\n", "line3\n"])
with open("data.txt", "r") as f:
print(f.readline())- The entire file content, all three lines
- Just “line1” (with its newline)
- Just “line3”
- An empty string, since writelines() doesn’t actually write anything
Show Answer & Explanation
β Answer: (b) Just “line1” (with its newline)
writelines() writes all three lines to the file. But readline() reads only the first line each time it’s called β so this prints just βline1β (including its trailing newline).
