Python Text File MCQs: 12 Solved Questions with Explanations

Solve 12 Python text-file questions by tracing modes, cursor movement, spaces, newlines and return types. Two flawed source snippets are handled literally and as intended.

KnowledgeGate Team

Exam prep & CS education

Updated 3 Dec 20268 min read
Python Text File MCQs: 12 Solved Questions with Explanations

Text-file MCQs mix three layers that look similar under time pressure: the open mode, the current file-pointer position, and the return type of read(), readline() or readlines(). One unnoticed space or change of case can alter the result.

For opening rules, exceptions, seek() and tell(), revise Python File Basics MCQs and Practice: 10 Solved Questions with Explanations. Python Text Files: Concepts, Worked Examples and Exam Patterns develops end-to-end reading, writing, malformed-record handling and exam patterns; text-file MCQs add the narrower challenge of return values, chained reads, preserved whitespace and case-sensitive defects.

Solve Python text-file MCQs with a mode, cursor and return-value trace

A text file read trace showing three reads and the resulting output Goo@rnin.

Use a three-column scratch method. First write the active mode. Next record the cursor after every read or seek(). Finally write the returned value and its type. Count spaces and \n as characters instead of skipping them mentally. tell() reports the current position, while seek() changes it. readline(n) stops at a newline or after the requested size, whichever comes first. This scratch trace prevents one read from contaminating the next answer.

For a warm-up, suppose message.txt starts with Good morning. \n, followed by I will be there on time.. At position 0, read(3) returns "Goo" and moves the cursor to 3. Then readline(4) returns "d mo" and moves it to 7. The next read(4) returns "rnin" and leaves it at 11. Therefore print("Goo", "rnin", sep="@") produces Goo@rnin.

File access modes: Questions 1 to 3

Question 1: Default file mode (RSSB Senior Computer Instructor 2026)

Code
In Python, when a text file is opened using the open() function without explicitly specifying a mode, what is the default file access mode used?
Code
A. Read mode ('r')
B. Write mode ('w')
C. Append mode ('a')
D. Read Write mode ('r+')

Answer: A. Read mode ('r').

open("notes.txt") equals open("notes.txt", "r"): read an existing file from the beginning. Mode w truncates before writing, a appends, and r+ reads and writes. Default read mode does not create a missing file. Source

Question 2: Append without deletion (RSSB Senior Computer Instructor 2026)

Code
A programmer wants to add a new line of text to the end of an existing file named logs.txt without deleting the current content. Which mode should they use in the open() function?
Code
A. r
B. w
C. a
D. x

Answer: C. a.

If logs.txt contains START, opening it with a and writing "\nDONE" leaves the two lines START and DONE. Mode w discards old content, r forbids writing, and x fails because the file already exists. Source

Question 3: Read and write from the start (RSSB Senior Computer Instructor 2024)

Code
 In Python language, which of following mode for opening the file, opens the file for both reading and writing and file pointer will be placed at the start of the file?
Code
A. ab
B. rb
C. r+
D. a+

Answer: C. r+.

The mode must allow reading and writing, with the pointer initially at the beginning. For OLD\n, r+ starts at 0; read(3) returns OLD. After seek(0), writing NEW produces NEW\n without automatic truncation. Mode a+ writes at the end, while the b choices do not meet both clues. Source

readline() and readlines() return values: Questions 4 to 6

Question 4: readline() return type (RSSB Senior Computer Instructor 2023)

Consider the following Python program segment :

Code
 File = open('Poem.txt', 'r')
 T=File.readline()
 Identify the data type of T:
Code
A. String (str) / String (str)
B. List / List
C. Tuple / Tuple
D. Dictionary / Dictionary

Answer: A. String (str) / String (str).

readline() returns one line as a str, not a list. For a first line Autumn wind\n, T is "Autumn wind\n", including the stored newline. At end-of-file it returns "", still a str. Source

Question 5: List of lines (RSSB Senior Computer Instructor 2026)

Code
Which of the following methods returns a list of strings, where each string represents a single line from the text file?
Code
A. read()
B. readline()
C. readlines()
D. list_read()

Answer: C. readlines().

For red\nblue\ngreen, readlines() returns ['red\n', 'blue\n', 'green']. The last string lacks \n because the file does not end with one. read() returns one string, readline() one line string, and list_read() is not a standard file method. Source

Question 6: Count the lines (RSSB Senior Computer Instructor 2023)

Consider the following Python code and answer the question given below:

Code
 with open("NOTES.TXT", "r") as F:
     Line = F.readlines()
 Which of the following Python statements will find and display the number of lines present in the file "NOTES.TXT"?
Code
A. print(len(LINE))
B. print(LINE.count())
C. print(LINE.count('\n'))
D. print(LINE.len())

Answer: A. print(len(LINE)).

Literally, no option works: the code defines Line, but every option uses LINE. The intended statement is print(len(Line)). For ['one\n', 'two\n', 'three'], it prints 3. list.count() needs a value; len is a built-in function, not a list method. Source

File pointers and chained reads: Questions 7 to 9

Question 7: Moving the file pointer (RSSB Senior Computer Instructor 2023)

Code
Which of the following method is used to move the file pointer to read the content from a specific position in a text file inside a Python program?
Code
A. move
B.  tell
C. find
D. seek

Answer: D. seek.

Use seek() to change position. For ASCII text ABCDEFG, read(3) returns ABC, tell() reports 3, and seek(0) rewinds so read(2) returns AB. find() searches strings; a standard file object has no move() method for this task. Source

Question 8: Chained reads (BPSC 2025)

A text file song.txt contains the following contents in it:

Code
 Life goes on as it never ends
 What will be the output of the following code snippet?
 f1 = open("song.txt", "r")
 s1 =  f1.read (5)
 s2 =  f1.read (4)
 s3 =  f1.read (3)
 print(s1, s3, sep = "#")
Code
A. goes # on
B. Life #goes # on
C. Life # on
D. Error

Answer: C. Life # on.

Step

Returned value

Cursor after read

s1 = f1.read(5)

'Life '

5

s2 = f1.read(4)

'goes'

9

s3 = f1.read(3)

' on'

12

s2 is consumed but not printed. Because s1 ends with a space and s3 begins with one, sep='#' produces exactly Life # on. Source

Question 9: readline(4) and the cursor (RSSB Senior Computer Instructor 2026)

A text file named message.txt contains the following lines:

Code
Good morning. 
I will be there on time.
Consider the following Python code:
F = open('message.txt')
S1 = F.read(3)
S2 = F.readline(4)
S3 = F.read(4)
print(S1, S3, sep='@')
Which of the following will be the correct output of the above Python code?
Code
A. Goo@rnin
B. Goo@I wi
C. ValueError
D. EOFError

Answer: A. Goo@rnin.

The trace is S1 = 'Goo' at cursor 3, S2 = 'd mo' at 7, and S3 = 'rnin' at 11. Only S1 and S3 print. readline(4) consumes four characters from the current position; it does not jump to the next line. Source

Practise more state tracing with Python Output-Based Questions: Lists, Slicing and Mutability. Lists and files differ, but both reward tracking operations in order.

Transforming and splitting file content: Questions 10 and 11

Question 10: Uppercase conversion defect (RSSB Senior Computer Instructor 2023)

Examine the given Python program and select the purpose of the program from the following options:

Code
 F=open('story.txt', 'r+')
 S=F.read()
 S=S.upper()
 F.seek(0)
 F.write(s)
  F.close()
Code
A. to count the upper case alphabets present in the file
B. to convert all alphabets into uppercase in the file /
C. to copy only the upper case alphabets from the file
D. to remove all lower case alphabets from the file

Answer: B. to convert all alphabets into uppercase in the file /.

Literally, this raises NameError: only S was assigned, but F.write(s) uses lowercase s. Correcting it to F.write(S) makes the program read all content, apply upper(), rewind, and write the same-length uppercase string. Red fox becomes RED FOX; nothing is counted or removed. Source

Question 11: First-line word count (RSSB Senior Computer Instructor 2023)

Examine the given Python program and select the best purpose of the program from the given options:

Code
 File = open("Poem.txt", "r")
 print(len(File.readline().split()))
 File.close()
Code
A. to count the number of words present in the file
B. to count the number of lines present in the file
C. to count the number of characters present in the file
D. to count the number of words present in the first line of the file

Answer: D. to count the number of words present in the first line of the file.

For first line Life goes on\n and second line Tomorrow starts now, readline() reads only the first. split() gives ['Life', 'goes', 'on'], so len(...) prints 3. Option A is too broad because line two is never read. Source

Recognising text-file examples: Question 12

Question 12: Extensions and text files (RSSB Senior Computer Instructor 2024)

Code
Extensions like .txt, .py, .CSV, etc in python language are example of _____.
Code
A. text files
B. text editor
C. UNI CODE
D. ASCII

Answer: A. text files.

At the tested level, .txt, .py and CSV files commonly store character text. A text editor is a program; Unicode and ASCII are encoding concepts, not the requested category. An extension alone does not prove a file's contents or encoding. Source

The short version and your next practice step

Use this checklist on every text-file output question:

  1. Choose the active mode.

  2. Mark the starting pointer.

  3. Advance it after each read, preserving spaces and newlines.

  4. Check the returned value and its type.

  5. Verify identifier case before accepting the intended result.

Questions 6 and 10 are useful only when you state both the literal code result and the intended correction. For a final 30-second check, take file content AB CD\n. read(2) gives AB, and tell() then reports 2. After seek(0), readline().split() gives ['AB', 'CD'], whose length is 2.

Continue with the Python Course for structured lessons and practice across file handling, functions and core syntax. Use Coding & DSA for broader programming practice beyond Python file questions.