Completed

Class 12 CS – Text File Handling Live Challenge

Class 12 · Computer Science Sat, 19 Sep 2026 10:00 PM 60s / question 15 questions · 15 marks

Questions From This Challenge

Q1output1 mark
What will be the output of the following Python code if data.txt initially contains ABCDE?
with open("data.txt", "r+") as f:
    print(f.read(2))
    f.write("X")
    f.seek(0)
    print(f.read())
Correct output: AB ABXDE
Q2output1 mark
What will be the output of the following Python code?
with open("data.txt", "w+") as f:
    f.write("COMPUTER")
    f.seek(3)
    print(f.read(3))
Correct output: PUT
Q3output1 mark
What will be the output of the following Python code if data.txt initially contains HELLO?
with open("data.txt", "a+") as f:
    print(f.read())
    f.seek(0)
    print(f.read(2))
Correct output: HE
Q4output1 mark
A file contains the following text:
Python
Java
C++

What will be the output of the following code?

with open("data.txt", "r") as f:
    first = f.readline()
    second = f.readline()
    print(first.strip() + second.strip())
Correct output: PythonJava
Q5output1 mark
What will be the output of the following Python code if data.txt contains exactly:
ABCDE
FGHIJ
KLMNO

with open("data.txt", "r") as f:
    f.read(3)
    print(f.readline().strip())
Correct output: DE
Q6output1 mark
What will be the output of the following Python code?
with open("data.txt", "w") as f:
    f.write("ABCDEFGH")

with open("data.txt", "r") as f:
    f.seek(4)
    print(f.tell())
    print(f.read(2))
Correct output: 4 EF
Q7output1 mark
What will be the output of the following Python code if data.txt contains ABCDEFG?
with open("data.txt", "r") as f:
    f.read(2)
    print(f.tell())
    f.read(3)
    print(f.tell())
Correct output: 2 5
Q8output1 mark
What will be the output of the following Python code if data.txt initially contains PYTHON?
with open("data.txt", "r+") as f:
    f.seek(2)
    f.write("AB")
    f.seek(0)
    print(f.read())
Correct output: PYABON
Q9output1 mark
What will be the output of the following Python code?
with open("data.txt", "w+") as f:
    f.write("ABCDEFG")
    f.seek(2)
    f.write("XY")
    f.seek(0)
    print(f.read())
Correct output: ABXYEFG
Q10mcq1 mark
A file initially contains the text 12345. What will be the final content of the file after executing the following code? with open("data.txt", "a") as f: f.seek(0) f.write("AB")
AB12345
12345AB ✓
AB45
12345
Q11output1 mark
What will be the output of the following Python code?
with open("data.txt", "w") as f:
    f.writelines(["ABC\n", "DEF\n", "GHI"])

with open("data.txt", "r") as f:
    lines = f.readlines()
    print(len(lines))
    print(lines[1].strip())
Correct output: 3 DEF
Q12output1 mark
What will be the output of the following Python code if data.txt contains:
ABC
DEF
GHI

with open("data.txt", "r") as f:
    print(f.read(5))
    print(f.readline().strip())
Correct output: ABC D EF
Q13output1 mark
What will be the output of the following Python code?
with open("data.txt", "w") as f:
    f.write("123456")

with open("data.txt", "r+") as f:
    f.seek(3)
    print(f.read(2))
    f.seek(1)
    f.write("XY")
    f.seek(0)
    print(f.read())
Correct output: 45 1XY456
Q14output1 mark
What will be the output of the following Python code if data.txt initially contains ABCDE?
with open("data.txt", "a+") as f:
    f.write("XY")
    f.seek(2)
    print(f.read(3))
Correct output: CDE
Q15output1 mark
What will be the output of the following Python code?
with open("data.txt", "w") as f:
    f.write("Python Programming")

with open("data.txt", "r+") as f:
    text = f.read()
    text = text.replace("Python", "Java")
    f.seek(0)
    f.write(text)

with open("data.txt", "r") as f:
    print(f.read())
Correct output: Java Programmingng
Next Live Challenge
Mixed: DML + Retrieval - Combined Application
Class 12 · Computer Science Fri, 23 Oct 2026, 9:00 PM
View Live Challenges →

← Back to Live Challenge History · Practice MCQs

CodeBuddy
Hi there! 👋 I'm CodeBuddy, your KV Coders assistant.

To help me give you the most accurate answers, please select your class and subject below, or just type your question!
Buddy is typing...