Completed
Class 12 CS – Text File Handling Live Challenge
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
ABXDEQ2output1 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:
PUTQ3output1 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:
HEQ4output1 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:
PythonJavaQ5output1 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:
DEQ6output1 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
EFQ7output1 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
5Q8output1 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:
PYABONQ9output1 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:
ABXYEFGQ10mcq1 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
DEFQ12output1 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
EFQ13output1 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
1XY456Q14output1 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:
CDEQ15output1 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