Text File Quiz Class 12

πŸ“– 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πŸ”§ File Modes✍️ Writing FilesπŸ“– Reading Files🎯 seek() and tell()βœ… Best Practices⭐ Board-Style Programs
Q1
πŸ“‚ Opening Files

How do you open a text file in read mode in Python?

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

Q2
πŸ”§ File Modes

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

  1. r
  2. r+
  3. w
  4. 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.)

Q3
πŸ”§ File Modes

What is the purpose of the “a” mode when opening a text file?

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

Q4
πŸ”’ Closing Files

How can you close an open text file in Python?

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

Q5
βœ… Best Practices

What is the benefit of using the ‘with’ statement when opening a file?

  1. It automatically creates a new file
  2. It ensures that the file is closed properly after use
  3. It speeds up file operations
  4. 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.

Q6
✍️ Writing Files

Which method is used to write a single string to a text file in Python?

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

Q7
🎯 seek() and tell()

How can you move the cursor position in a text file to a specific byte using Python?

  1. file.jump(offset)
  2. file.seek(offset)
  3. file.move(offset)
  4. 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.

Q8
🎯 seek() and tell()

What does the tell() method return for a text file?

  1. The file’s size
  2. The current line number
  3. The cursor’s current position
  4. 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.

Q9
πŸ“– Reading Files

In the context of text files, what does the readline() method do?

  1. Reads a specific line by line number
  2. Reads the entire file at once
  3. Reads a single character
  4. 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.

Q10
✍️ Writing Files

How can you write (append) multiple lines to a text file at once in Python?

  1. file.writelines(“line1”, “line2”)
  2. file.write(“line1\nline2”)
  3. file.append(“line1”, “line2”)
  4. 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.

Q11
πŸ“– Reading Files

Which method is used to read the ENTIRE content of a text file into a single string?

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

Q12
πŸ”§ File Modes

What is the purpose of the “w+” mode when opening a text 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)

“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.

Q13
πŸ–₯️ os Module

How can you check if a file exists before attempting to open it in Python?

  1. file.exists(“example.txt”)
  2. file.open(“example.txt”)
  3. os.path.exists(“example.txt”)
  4. 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.

Q14
🎯 seek() and tell()

What is the purpose of the seek(0) method call on a text file?

  1. Move the cursor to the end of the file
  2. Move the cursor to the very beginning of the file
  3. Move the cursor to a random position
  4. 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.

Q15
πŸ“– Reading Files

How can you read all lines from a text file into a list in Python?

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

Q16
πŸ“– Reading Files

What is the most Pythonic (idiomatic) way to read a text file line by line, one at a time?

  1. Calling file.readline() manually in an infinite while loop
  2. Using a for loop directly on the file object: for line in file:
  3. Calling file.read() and then splitting on ‘\n’
  4. 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.

Q17
⭐ 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)
  1. Hello 5
  2. Hello World 11
  3. World 5
  4. 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.

Q18
⭐ 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())
  1. XXXXX (the entire file is replaced)
  2. XXXXX World (only the first 5 characters are overwritten)
  3. Hello WorldXXXXX (XXXXX is appended)
  4. 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”.

Q19
⭐ 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()))
  1. The full original 3 lines of text
  2. An empty string: ”
  3. Only the first line
  4. 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.

Q20
⭐ 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)
  1. 6
  2. 8
  3. 10
  4. 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.

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 β†’