AMITY INTERNATIONAL SCHOOL, MAYUR VIHAR
CLASS XII
CBSE SOLVED QUESTION BANK
TEXT FILES
1 mark
1) Which of the following mode keeps the file offset position at the end of the file?
(a) r+ (b) r (c) w (d) a
Ans: Option (d) a mode keeps the file offset position at the end of the file.
2) The syntax of seek ( ) is:
file_object.seek(offset[, reference_point])
What is the default value of reference_point ?
(a) 0 (b) 1 (c) 2 (d) 3
Ans: Option (a) 0 The default value of reference point is 0.
3) Why it is important to close a file before exiting?
Ans: Closing a file releases system resources and ensures that all data is saved correctly.
It also prevents potential data loss and avoids file locking issues, maintaining overall
system stability.
4) Which of the following statement is not correct ?
a. We can write content into a text file opened using ‘w’ mode
b. We can write content into a text file opened using ‘w+’ mode
c. We can write content into a text file opened using ‘r’ mode
d. We can write content into a text file opened using ‘r+’ mode
Ans: c. ‘r’ mode is used to open a file for reading, we cannot write content into a text file
opened using ‘r’ mode.
5) Which of the following option is the correct Python statement to read and display the
first 10 characters of a text file “Notes.txt” ?
a. F = open ( ‘Notes.txt’ ); print(F.load(10))
b. F = open ( ‘Notes.txt’ ); print(F.dump(10))
c. F = open ( ‘Notes.txt’ ); print(F.read(10))
d. F = open ( ‘Notes.txt’ ); print(F.write(10))
Ans: c. read ( ) return the read bytes in the form of string. If any number like 10 is given so
it will return 10 characters of a text file, if no any number is specified then it reads the
entire file.
6) Which of the following is not a correct python statement to open a text file
“Notes.txt” to write content into it ?
a. F = open ( ‘ Notes.txt ’, ‘w’)
b. F = open ( ‘ Notes.txt ’, ‘a’)
c. F = open ( ‘ Notes.txt ’, ‘A’)
d. F = open ( ‘ Notes.txt ’, ‘w+’)
Ans: c. ‘A’ mode does not exist in Opening modes in Standard I/O.
7) A text file opened using the following statement:
MyFile = open ( ‘Notes.txt’ )
Which of the following is the correct Python statement to close it ?
a. MyFile = close ( ‘Notes.txt’ )
b. MyFile = close ( ‘Notes.txt’ )
c. Close.MyFile ( )
d. MyFile.close ( )
Ans: d. fileObject.close( ) is used to close a file, Here fileObject is MyFile.
8) Which of the following option is the correct usage for the tell ( ) of a file object?
a. It places the file pointer at a desired offset in a file
b. It returns the entire content of a file
c. It returns the byte position of the file pointer as an integer
d. It tells the details about the file
Ans: c. tell ( ) in python return the byte position of the file pointer as an integer.
9) Which is the significance of the seek ( ) method ?
a. It seeks the absolute path of the file.
b. It tells the current byte position of the file pointer within the file.
c. It places the file pointer at a desired offset within the file.
d. It seeks the entire content of the file.
Ans: c. Seek ( ) is used to change the position of a file pointer in a specific position
10) If the following statement is used to read the contents of text file object F:
X = F.readlines ( )
Which of the following is the correct data type of X ?
a. string b. list c. tuple d. dictionary
Ans: b. readlines( ) returns a list, each line of the text file behaves as a list item.
11) Suppose the content of a text file Notes.txt is:
“The way to get started is to quit talking and begin doing”
What will be the output of the following Python code ?
F = open ( “Notes.txt” )
F.seek (29)
S = F.read()
print ( S )
a. The way to get started is to
b. quit talking and begin doing
c. The way to get started is to quit talking and begin doing
d. Gniod nigeb dna gniklat tiuq ot si detrats teg ot yaw ehT
Ans: b. The Correct output of the following Python statement will be quit talking and
begin doing.
12) Suppose the content of a text file “Rhymes.txt” is as follows:
Jack & Jill
went up the hill
What will be the output of the following Python code ?
F = open( “Rhymes.txt”)
L = F.readLines( )
for i in L:
S = i.split( )
print( len(S), end = “#” )
a. 2#4# b. 3#4# c. 2# d. 7#
Ans: b. The Correct output of the following Python statement will be 3#4#.
13) Suppose the content of “Rhymes.txt” is :
Baa baa black sheep,
Have you any wool?
What will be the output of the following Python code ?
F = open (“Rhymes.txt”)
S = F.read ( )
L = S.split ( )
for i in L :
if len ( i ) %3 != 0:
print (I, end = “ “)
a. Baa baa you any b. black have wool ?
c. black sheep, have wool ? d. Error
Ans: The Correct output of the following Python statement will be Black have wool ?.
Hence, the correct option is ‘b’.
14) Suppose the content of “Rhymes.txt” is
One, two, three, four, five
Once I caught a fish alive.
What will be the output of the following Python code ?
F = open( “ Rhymes.txt”)
S = F.read ( )
print(S.count(‘ e ‘, 20 ))
a. 20 b. 1 c. 3 d. 6
Ans: The Correct output of the following Python statement will be 3. Hence, the correct
option is ‘c’.
15) Suppose the content of “Rhymes.txt” is
Good Morning Madam
What will be the output of the following code
F = open(“ Rhymes.txt”)
L = F.read( ).split ( )
for W in L:
if W.lower ( ) == W[ : : -1].lower ( ) :
print( W )
a. Good b. Morning c. Madam d. Error
Ans: The Correct output of the following Python statement will be Madam. Hence, the
correct option is ‘c’.
16) Suppose the content of “Rhymes.txt” is
Hickory Dickory Dock
The mouse went up the clock
What will be the output of the following Python code ?
F = open ( “Rhymes.txt” )
L = F.readLines ( )
X = [“the”, “ock”]
for i in L:
for W in i.split( )
if W in X:
print ( W, end=” * “)
a) the * b) Dock * The * the * clock * c) Dock * the * clock * d. Error
Ans: The Correct output of the following Python statement will be Dock * the * clock * , if
we ignore the indentation error. Hence, the correct option is ‘c’.
17) Write the missing statement to complete the following code:
file = open("example.txt", "r")
data = file.read(100)
____________________ #Move the file pointer to the beginning of the file
next_data = file.read(50)
file.close()
Ans: file.seek(0) ( OR file.seek(0,0)
18) Which of the following mode in file opening statement results or generates an error if the
file does not exist?
(a) a+ (b) r+ (c) w+ (d) None of the above
Ans: (b) r+
19) The correct syntax of seek() is:
(a) file_object.seek(offset [, reference_point])
(b) seek(offset [, reference_point])
(c) seek(offset, file_object)
(d) seek.file_object(offset)
Ans: (a) file_object.seek(offset [, reference_point])
20) Which of the following functions changes the position of file pointer and returns its new
position?
a. flush() b. tell() c. seek() d. offset()
Ans: b. tell()
21) Differentiate between r+ and w+ file modes in Python.
Ans: r+ Mode (Read and Write):
File Existence: File must exist.
Behavior: Allows reading and writing; does not truncate the file (keeps existing
content).
w+ Mode (Write and Read):
File Existence: Creates a new file if it doesn't exist; truncates the file if it exists
(removes content).
Behavior: Allows reading and writing; truncates the file before writing.
Key Difference:
r+ preserves existing data, while w+ overwrites the file.
22) Which of the following file opening mode in Python, generates an error if the file does not
exist?
a. a b. r c. w d. w+
Ans: b. r
23) Which of the following option is not correct?
a. if we try to read a text file that does not exist, an error occurs.
b. if we try to read a text file that does not exist, the file gets created.
c. if we try to write on a text file that does not exist, no error occurs.
d. if we try to write on a text file that does not exist, the file gets
Created.
Ans: b. if we try to read a text file that does not exist, the file gets created.
24) Which of the following options can be used to read the first line of a text file Myfile.txt?
a. myfile = open('Myfile.txt'); myfile.read()
b. myfile = open('Myfile.txt','r'); myfile.read(n)
c. myfile = open('Myfile.txt'); myfile.readline()
d. myfile = open('Myfile.txt'); myfile.readlines()
Ans: c. myfile = open('Myfile.txt'); myfile.readline()
25) Assume that the position of the file pointer is at the beginning of 3rd line in a text file.
Which of the following option can be used to read all the remaining lines?
a. myfile.read()
b. myfile.read(n)
c. myfile.readline()
d. myfile.readlines()
Ans: d. myfile.readlines()
26) A text file student.txt is stored in the storage device. Identify the correct option out of the
following options to open the file in read mode.
i. myfile = open('student.txt','rb')
ii. myfile = open('student.txt','w')
iii. myfile = open('student.txt','r')
iv. myfile = open('student.txt')
a. only i
b. both i and iv
c. both iii and iv
d. both i and iii
Ans: c. both iii and iv
27) What is the significance of the tell() method?
a. tells the path of file
b. tells the current position of the file pointer within the file
c. tells the end position within the file
d. checks the existence of a file at the desired location
Ans: b. tells the current position of the file pointer within the file
28) Suppose content of 'Myfile.txt' is:
What will be the output of the following code?
myfile = open("Myfile.txt")
data = myfile.readlines()
print(len(data))
myfile.close()
a. 3 b. 4 c. 5 d. 6
Ans: b. 4
29) Suppose content of 'Myfile.txt' is
What will be the output of the following code?
myfile = open("Myfile.txt")
record = myfile.read().split()
print(len(record))
myfile.close()
a. 24 b. 25 c. 26 d. 27
Ans: c. 26
30) Suppose content of 'Myfile.txt' is
What will be the output of the following code?
myfile = open("Myfile.txt")
x = myfile.read()
print(len(x))
myfile.close()
a. 5 b. 25 c. 26 d. 27
Ans: d. 27
31) Suppose content of 'Myfile.txt' is
What will be the output of the following code?
myfile = open("Myfile.txt")
x = myfile.read()
y = x.count('the')
print(y)
myfile.close()
a. 2 b. 3 c. 4 d. 5
Ans: b. 3
32) Suppose content of 'Myfile.txt' is
What will be the output of the following code?
myfile = open("Myfile.txt")
vlist = list("aeiouAEIOU")
vc=0
x = myfile.read()
for y in x:
if(y in vlist):
vc+=1
print(vc)
myfile.close()
a. 6 b. 7 c. 8 d. 9
Ans: b. 7
33) Suppose content of 'Myfile.txt' is
What will be the output of the following code?
myfile = open("Myfile.txt")
line_count = 0
data = myfile.readlines()
for line in data:
if line[0] == 'T':
line_count += 1
print(line_count)
myfile.close()
a. 2 b. 3 c. 4 d. 5
Ans: a. 2
34) Consider the following directory structure.
Suppose root directory (School) and present working directory are the same. What will be
the absolute path of the file Syllabus.jpg?
a. School/syllabus.jpg b. School/Academics/syllabus.jpg
c. School/Academics/../syllabus.jpg d. School/Examination/syllabus.jpg
Ans: b. School/Academics/syllabus.jpg
35) Assume the content of text file, 'student.txt' is:
What will be the data type of data_rec?
myfile = open("Myfile.txt")
data_rec = myfile.readlines()
myfile.close()
a. string b. list c. tuple d. dictionary
Ans: b. list
2 marks
36) Differentiate between "w" and "r" file modes used in Python. Illustrate the
difference using suitable examples.
Ans: A file is opened using “w” mode to write content into the file.
A file is opened using “r” mode to read content into the file.
Example:
def Create():
file = open('NOTES.TXT', 'w')
S = "This is a sample"
file.write(S)
file.close()
def Read():
file = open('NOTES.TXT', 'r')
Lines = file.readline()
print(Lines)
file.close()
Create()
Read()
37) Write a function Show_words() in Python to read the content of a text file NOTES.TXT and
display the entire content in capital letters.
Example, if the file contains:
This is a test file
Then the function should display the output as:
THIS IS A TEST FILE
Ans: def Show_words():
file=open('NOTES.TXT', 'r')
Lines = file.readlines()
for L in Lines:
print(L.upper())
file.close()
38) Write a function Show_words() in Python to read the content of a text file NOTES.TXT and
display only such lines of the file which have exactly 5 words in them.
Example, if the file contains:
This is a sample file.
The file contains many sentences.
But needs only sentences which have only 5 words.
Then the function should display the output as:
This is a sample file.
The file contains many sentences.
Ans: def Show_words():
file=open('NOTES.TXT', 'r')
Lines = file.readlines()
for L in Lines:
W=L.split()
if (len(W)==5):
print(L)
file.close()
3 marks
39) Write the definition of a Python function names LongLines( ) which reads the
contents of a text file named ‘LINES.TXT’ and displays those lines from which have at
least 10 words in it.
For example, if the content of ‘LINES.TXT’ is as follows:
Once upon a times, there was a woodcutter
He lived in a little house in a beautiful, green wood.
One day, he was merrily chopping some wood.
He saw a little girl skipping through the woods, whistling happily.
The girl was followed by a big gray wolf.
Then the function should display output as:
He lived in a little house in a beautiful, green wood.
He saw a little girl skipping through the woods, whistling happily.
Ans:
def LongLines():
with open('LINES.TXT', 'r') as file:
for line in file:
if len(line.split()) >= 10:
print(line.strip())
LongLines()
40) Write a function COUNT_DWORDS() in Python to count the words ending with a digit
in a text file “Details.txt”.
Example:
If the file content is as follows:
On seat2 VIP1 will sit and
On seat 1 VIP2 will be sitting
Output will be:
Number of words ending with a digit are 4
Ans:
def Count_Dwords():
f=open("Details.txt",'r")
s=f.read()
l=s.split()
count=0
for word in l:
if word[-1].isdigit():
count += 1
print("Number of words ending with a digit are ", count)
f.close()
41) Write a function in Python to read a text file, Alpha.txt and displays those lines which
begin with the word ‘You’.
Ans:
42) Write a function, vowelCount() in Python that counts and displays the number of vowels
in the text file named Poem.txt.
Ans:
43) Write a Python function that displays all the words containing @cmail from a text file
"Emails.txt".
Ans:
44) Write a Python function that finds and displays all the words longer than 5 characters
from a text file "Words.txt".
Ans:
45) Write a method COUNTLINES() in Python to read lines from text file ‘TESTFILE.TXT’ and
display the lines which are not starting with any vowel.
Example:
If the file content is as follows:
An apple a day keeps the doctor away.
We all pray for everyone’s safety.
A marked difference will come in our country.
The COUNTLINES() function should display the output as:
The number of lines not starting with any vowel - 1
Ans:
46) Write a function ETCount() in Python, which should read each character of a text file
“TESTFILE.TXT” and then count and display the count of occurrence of alphabets E and T
individually (including small cases e and t too).
Example:
If the file content is as follows:
Today is a pleasant day.
It might rain today.
It is mentioned on weather sites
The ETCount() function should display the output as:
The number of E or e: 6
The number of T or t : 9
Ans:
def ETCount() :
47) Write the definition of a function ChangeGender() in Python, which reads the contents of
a text file "BIOPIC.TXT" and displays the content of the file withevery occurrence of the
word 'he' replaced by 'she'.
For example, if the content of the file "BIOPIC.TXT" is as follows :
Last time he went to Agra,
there was too much crowd, which he did not like.
So this time he decided to visit some hill station.
The function should read the file content and display the output as follows :
Last time she went to Agra,
there was too much crowd, which she did not like.
So this time she decided to visit some hill station.
Ans:
48) Write the definition of a function Count_Line() in Python, which should read
each line of a text file "SHIVAJI.TXT" and count total number of lines present in
text file.
For example, if the content of the file "SHIVAJI.TXT" is as follows :
Shivaji was born in the family of Bhonsle.
He was devoted to his mother Jijabai.
India at that time was under Muslim rule.
The function should read the file content and display the output as follows :
Total number of lines : 3
Ans:
49) Write a function COUNT() in Python to read from a text file 'Gratitude.txt' and display the
count of the letter 'e' in each line
Example: If the file content is as follows:
Gratitude is a humble heart's radiant glow,
A timeless gift that nurtures and bestows.
It's the appreciation for the love we're shown,
In moments big and small, it's truly known.
Ans:
50) Write a function Start_with_I() in Python, which should read a text file 'Gratitude.txt' and
then display lines starting with 'I'.
Example: If the file content is as follows:
Gratitude is a humble heart's radiant glow,
A timeless gift that nurtures and bestows.
It's the appreciation for the love we're shown,
In moments big and small, it's truly known.
Then the output should be
It's the appreciation for the love we're shown,
In moments big and small, it's truly known.
Ans: