Indian Institute of Technology Madras
BS Degree Programme · Examination & Study Paper
Programming in Python
File handling
Examination Instructions & Guidelines
- Solve on notebook/paper first before checking solutions.
- MCQ: Exactly 1 option is correct. Negative marking applies.
- MSQ: One or more options correct. Select all to score.
- NAT: Numerical response. No negative marking.
- Scientific calculator allowed for all calculations.
- Official answer key is sealed at the end of this sheet.
End Term · 31 Aug 2025 (FN)End Term
- Q1NAT2 Marks·Write the number
Consider the following Python code: with open("data.txt", "w") as f: for i in range(3): for j in range(i + 1): f.write(f"Line {i}-{j}" + ("\n" * (j + 1))) with open("data.txt", "r") as f: count = len(f.readlines()) print(count) What is the output of the given code?
Your answer:
End Term · 31 Aug 2025 (AN)End Term
- Q2MSQ3 Marks·Choose all correct options
Consider the following snippet of code: filename = "test.txt" with open(filename, "w") as f: f.write("\n".join((f"Line{i}" for i in range(1,11)))) with open(filename, "r") as f: f.readline() f.readline() print(f.read(5)) f.readline() print(f.read(10)) f.seek(0) print(f.read(5)) f.readline() f.readline() print(f.read(5)) Select all that apply. Note • If a value is passed to _(f.read) , it reads that many number of characters from the current position of the file pointer. • The _(f.seek) method moves the position of the file pointer to the given number of chars after the start of the file.
- AThe first line of the output is _(Line3) .
- BThe second line of the output is _(Line5) .
- CThe second line of the output is _(Line4) .
- DThere are 5 lines in the output.
- EThe output contains empty lines.
Read the passage below, then answer Q3–Q4
Consider the below python code.
def create_and_write_file(filename, num_lines):
with open(filename, "w") as f:
for i in range(1, num_lines + 1):
f.write(f"Line {i}\n")
def random_access_read(filename, seek_pos):
with open(filename, "r") as f:
first_chars = f.read(5)
f.seek(seek_pos)
line = f.readline()
rest = f.readlines()
for line in rest:
print(line.strip())
def overwrite_some_text(filename, overwrite_pos, text):
with open(filename, "r+") as f:
f.seek(overwrite_pos)
original = f.readline()
f.seek(overwrite_pos)
f.write(text + "\n")
filename = "example_file.txt"
num_lines = 6
seek_pos = 10
overwrite_pos = 21
overwrite_text = "OVERWRITTEN_TEXT"
create_and_write_file(filename, num_lines)
overwrite_some_text(filename, overwrite_pos, overwrite_text)
Based on the given code snippet answer the given subquestions.
Q3NAT2 Marks·Write the numberOn executing the given code, how many number of lines will be there in the file example_file.txt ?
Your answer:- Q4MCQ3 Marks·Choose one option
What will be printed if we call random_access_read(filename, seek_pos) immediately after running the given code?
- ALine 1 Line 2 Line 3 OVERWRITTEN_TEXT e 6
- BLine 3 OVERWRITTEN_TEXT e 6
- CLine 3 LinOVERWRITTEN_TEXT
- DLine 1 Line 2 Line 3 Line 4 Line 5 Line 6
End Term · 13 Apr 2025 (FN)End Term
- Q5MCQ3 Marks·Choose one option

- A2
- B3
- C5
- DIt will give an error
- Q6NAT3 Marks·Write the number
Your answer:
End Term · 13 Apr 2025 (AN)End Term
- Q7MCQ3 Marks·Choose one option

- A2
- B3
- C4
- D6
- Q8NAT3 Marks·Write the number
Your answer:
End Term · 22 Dec 2024 (FN)End Term
- Q9MCQ3 Marks·Choose one option

- A

- B

- C

- D

- A
- Q10NAT3 Marks·Write the number
Your answer:
End Term · 22 Dec 2024 (AN)End Term
- Q11MCQ3 Marks·Choose one option

- AAn empty file
- BA file with three lines of student data
- CA file with student names only
- DA file with student grades only
- Q12NAT4 Marks·Write the number
Your answer: