OMSAKTHI
G B PUBLIC SCHOOL
CLASS XII COMPUTER SCIENCE
TEXT FILE OUTPTUT QUESTIONS
1. Write the output of the following code segment:
Content of testfp.txt:
This is to demonstrate seek and tell functions
Enjoy programming!
fh=open(“testfp.txt”,”r”)
fh.seek(6)
x=fh.tell()
print(x)
print(fh.read())
2. Assume RHYMES.txt exists with the following content:
Hickory Dickory Dock
The mouse went up the clock
What will be the output of the following code:
with open('rhymes.txt') as f:
L=f.readlines()
x=['the','ock']
for i in L:
for w in i.split():
if w in x: print(w,end='*')
3. Predict the output….
f=open("ppp.txt","w+")
f.write("All the gliters are not gold\n")
f.write("The sky is limit")
f.seek(3)
r=f.readline()
print(r)
4. Given the file content in “numbers.txt”
123
456
789
Python code based on above file is as follows:
file = open("numbers.txt", "r")
data = file.read()
file.close()
print(len(data))
What will be the output of above code?
a. 17 b. 11 c. 9 d. 15
5. Given the content of text file “india.txt”:
Honesty is the best policy
What will be the output of following Python code:
file = open("example.txt", "r")
file.read(5)
file.read(1)
print(file.tell())
file.close()
a. 5 b. 6 c. 1 d. 7
6. Write the output of Python code given below:
file = open("data.txt", "w")
file.write("CBSE Exam 2025")
file.close()
file = open("data.txt", "r")
file.read(5)
print(file.tell())
file.seek(10)
file.read(4)
print(file.tell())
file.close()
7. Predict the output for the following code:
with open("Sample.txt","w+") as F:
F.write("Always Think Positive")
print(F.tell(), end="#")
F.seek(7,0)
print(F.read(5))
8. Assume the following code is executed. Write the contents of the file after statement1
and after statement2 are executed
with open("Notes.txt", "a+") as F:
F.write("The key to success is focus on goals,not obstacles")
with open("Notes.txt", "w+") as F:
F.write("Silence is gold") ---- Statement1
with open("Notes.txt", "r+") as F:
F.seek(5)
F.write("Work hard") ---- Statement2
9. Predict the output for the following code:
with open("rhymes.txt","w+" ) as F:
F.write("Humpty Dumpty sat on a wall\nHumpty Dumpty had a great fall")
F.seek(0)
F.read(10)
X = F.readlines()
for k in X:
print(k.strip(), end= "*")
10. Assume a text file “Rhymes.txt” exists with the following contents:
Twinkle Twinkle little star
How I wonder what you are?
Write the output for the following code; also write the final contents of the file
with open(“rhymes.txt”, “r+”) as F1:
F1.seek(5)
X = F1.readline(5)
Y = X[::-3].title()
print(Y)
F1.seek(0)
F1.write(Y)