π Text File Quiz β Class 12 Computer Science (Code 083)
Practice 20 CBSE Class 12 Computer Science (Code 083) MCQs on text file handling β opening a file in different modes (r, r+, w, w+, a), closing files, the with statement, write()/writelines(), read()/readline()/readlines(), iterating a file with a for loop, and the seek()/tell() methods.
This set includes 4 board-style predict-the-output questions with full working code β including the classic r+ vs w+ truncation trap and a vowel-counting program. 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 text file in read mode in Python?
- file = open(“example.txt”, “r”)
- file = open(“example.txt”, “w”)
- file = open(“example.txt”, “a”)
- file = open(“example.txt”, “rb”)
Show Answer & Explanation
β Answer: (a) file = open(“example.txt”, “r”)
“r” is the mode for reading a text file β it’s also the default mode if you don’t specify one at all.
π§ File Modes
Which open mode allows both reading and writing in a text file WITHOUT truncating (emptying) the file?
- r
- r+
- w
- w+
Show Answer & Explanation
β Answer: (b) r+
r+ opens an existing file for both reading and writing, but keeps its existing content intact. (w+ also allows both, but it truncates the file to empty first β a key difference tested below.)
π§ File Modes
What is the purpose of the “a” mode when opening a text file?
- Read mode
- Write mode
- Append mode
- Read and Write mode
Show Answer & Explanation
β Answer: (c) Append mode
“a” (append mode) opens the file for writing but adds new content to the end, preserving everything already there.
π Closing Files
How can you close an open text file in Python?
- file.close()
- close(file)
- file.close(file)
- close(“file.txt”)
Show Answer & Explanation
β Answer: (a) file.close()
file.close() β close() is a method called on the file object itself, not a standalone function.
β Best Practices
What is the benefit of using the ‘with’ statement when opening a file?
- It automatically creates a new file
- It ensures that the file is closed properly after use
- It speeds up file operations
- It allows direct manipulation of file data
Show Answer & Explanation
β Answer: (b) It ensures that the file is closed properly after use
The with statement is a context manager that guarantees the file gets closed once the block finishes β even if an error occurs partway through β so you never have to remember to call close() yourself.
βοΈ Writing Files
Which method is used to write a single string to a text file in Python?
- file.write()
- file.writeline()
- file.append()
- file.add()
Show Answer & Explanation
β Answer: (a) file.write()
file.write() writes exactly the string you give it. (writeline(), append(), and add() aren’t real file methods in Python.)
π― seek() and tell()
How can you move the cursor position in a text file to a specific byte using Python?
- file.jump(offset)
- file.seek(offset)
- file.move(offset)
- file.position(offset)
Show Answer & Explanation
β Answer: (b) file.seek(offset)
file.seek(offset) moves the file’s internal read/write cursor to the given byte position, letting you jump around the file instead of always reading sequentially.
π― seek() and tell()
What does the tell() method return for a text file?
- The file’s size
- The current line number
- The cursor’s current position
- The file’s creation time
Show Answer & Explanation
β Answer: (c) The cursor’s current position
file.tell() returns the current position of the cursor (as a byte offset from the start), telling you exactly where the next read or write will happen.
π Reading Files
In the context of text files, what does the readline() method do?
- Reads a specific line by line number
- Reads the entire file at once
- Reads a single character
- Reads one line, up to and including the next newline character
Show Answer & Explanation
β Answer: (d) Reads one line, up to and including the next newline character
readline() reads and returns just one line β from the current cursor position up through the next newline character β each time it’s called.
βοΈ Writing Files
How can you write (append) multiple lines to a text file at once in Python?
- file.writelines(“line1”, “line2”)
- file.write(“line1\nline2”)
- file.append(“line1”, “line2”)
- file.writelines([“line1”, “line2”])
Show Answer & Explanation
β Answer: (d) file.writelines([“line1”, “line2”])
writelines() takes a single iterable (like a list of strings) as its argument: file.writelines(["line1", "line2"]). It does NOT accept multiple separate string arguments the way the other options suggest.
π Reading Files
Which method is used to read the ENTIRE content of a text file into a single string?
- file.read()
- file.readline()
- file.readlines()
- file.readall()
Show Answer & Explanation
β Answer: (a) file.read()
file.read() reads everything from the current cursor position to the end of the file, returning it all as one string. (readall() isn’t a real Python file method.)
π§ File Modes
What is the purpose of the “w+” mode when opening a text 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)
“w+” allows both reading and writing, but β unlike "r+" β it immediately truncates the file to empty the moment it’s opened, discarding any existing content.
π₯οΈ os Module
How can you check if a file exists before attempting to open it in Python?
- file.exists(“example.txt”)
- file.open(“example.txt”)
- os.path.exists(“example.txt”)
- open.file(“example.txt”)
Show Answer & Explanation
β Answer: (c) os.path.exists(“example.txt”)
os.path.exists(“example.txt”) returns True or False depending on whether the file is actually present β a safe check before trying to open it.
π― seek() and tell()
What is the purpose of the seek(0) method call on a text file?
- Move the cursor to the end of the file
- Move the cursor to the very beginning of the file
- Move the cursor to a random position
- Move the cursor to the start of the next line
Show Answer & Explanation
β Answer: (b) Move the cursor to the very beginning of the file
seek(0) resets the cursor to byte position 0 β the very start of the file β commonly used to re-read a file from the beginning after already reading through it once.
π Reading Files
How can you read all lines from a text file into a list in Python?
- file.read()
- file.readlist()
- file.readlines()
- file.readall()
Show Answer & Explanation
β Answer: (c) file.readlines()
file.readlines() reads every line and returns them as a list of strings, e.g. ['Hello World\n', 'This is Python\n'].
π Reading Files
What is the most Pythonic (idiomatic) way to read a text file line by line, one at a time?
- Calling file.readline() manually in an infinite while loop
- Using a for loop directly on the file object: for line in file:
- Calling file.read() and then splitting on ‘\n’
- There is no way to do this in Python
Show Answer & Explanation
β Answer: (b) Using a for loop directly on the file object: for line in file:
for line in file: iterates the file object directly, giving you one line per loop iteration β it’s memory-efficient (doesn’t load the whole file at once, unlike readlines()) and is the standard, idiomatic approach.
β Board-Style Programs
What does the following program print?
with open("t.txt", "w") as f:
f.write("Hello World\n")
with open("t.txt", "r") as f:
data = f.read(5)
pos = f.tell()
print(data, pos)- Hello 5
- Hello World 11
- World 5
- Hello 0
Show Answer & Explanation
β Answer: (a) Hello 5
f.read(5) reads only the first 5 characters: βHelloβ. After reading those 5 characters, the cursor sits at position 5, which is what tell() reports.
β Board-Style Programs
What does the following program print? (The file already contains “Hello World\n” before this code runs.)
with open("t.txt", "r+") as f:
f.write("XXXXX")
with open("t.txt", "r") as f:
print(f.read())- XXXXX (the entire file is replaced)
- XXXXX World (only the first 5 characters are overwritten)
- Hello WorldXXXXX (XXXXX is appended)
- Error, ‘r+’ cannot be used to write
Show Answer & Explanation
β Answer: (b) XXXXX World (only the first 5 characters are overwritten)
r+ mode does not erase the file β it just positions the cursor at the start and lets you write, which overwrites only as many characters as you write. Since βXXXXXβ is 5 characters, it replaces βHelloβ, leaving the rest untouched: βXXXXX Worldβ.
β Board-Style Programs
What does the following program print? (The file already contains 3 lines of text before this code runs.)
with open("t.txt", "w+") as f:
print(repr(f.read()))- The full original 3 lines of text
- An empty string: ”
- Only the first line
- Error, since w+ can’t be used with read()
Show Answer & Explanation
β Answer: (b) An empty string: ”
Opening in “w+” mode immediately truncates the file to empty the moment it’s opened β so even though w+ technically supports reading, there’s nothing left to read. This is the classic difference from "r+", which preserves content.
β Board-Style Programs
What does the following program print? (Counting vowels in a text file that contains: “Hello World\nThis is Python\nCBSE Class 12\n”)
with open("t.txt", "r") as f:
content = f.read()
vowels = "aeiouAEIOU"
count = sum(1 for ch in content if ch in vowels)
print(count)- 6
- 8
- 10
- 5
Show Answer & Explanation
β Answer: (b) 8
Counting every vowel (upper or lower case) across all three lines of the file gives a total of 8 β a classic βread a file and count vowels/charactersβ suggested program from the CBSE syllabus.
