100+ Text File MCQ Question Bank For Class 12 - CBSE Python
100+ Text File MCQ Question Bank For Class 12 - CBSE Python
cbsepython.in/text-file-mcq-question-bank-for-class-12
1. Which of the following command is used to open a file “c:\temp.txt” in read-mode only?
2. Which of the following command is used to open a file “c:\temp.txt” in write-mode only
4. Which of the following commands can be used to read “n” number of characters from a file using the
file object <file>?
a) read(n)
b) n = file.read()
c) file.readline(n)
d) file.readlines()
a) read(n)
5. Which of the following commands can be used to read the entire contents of a file as a string using
the file object <tmpfile>?
a) read(n)
b) read()
c) readline()
d) readlines()
b) read()
6. Which of the following commands can be used to read the next line in a file using the file object
<tmpfile>?
a) read(n)
b) tmpfile.read()
c) readline()
d) tmpfile.readlines()
c) readline()
7. Which of the following commands can be used to read the remaining lines in a file using the file object
<tmpfile>?
a) read(n)
b) read()
c) readline()
d) readlines()
c) readline()
a) Str
b) a list of lines
d) list of integers
b) a list of lines
MCQ Text file, Binary file, CSV file, relative and absolute paths
a) 1
b) 2
c) 3
d) 4
b) 2
a) Text file
b) Binary file
a) Text file
b) End of byte
c) End of line
c) End of line
12. Trying to open a binary file using a text editor will show:
a) Garbage values
b) ASCII values
c) Binary character
d) Unicodes
a) Garbage values
13. In file handling, what does this terms means “r, a”?
a) read, append
b) append, read
c) write, append
a) read, append
a) readline()
b) readlines()
c) readstatement()
d) readfullline()
a) readline()
15. Which function is used to close a file in python?
a) close()
b) stop()
c) end()
d) closefile()
a) close()
16. A file maintains a __________ which tells the current position in the file where writing or reading will
take place.
a) line
b) file pointer
c) list
d)order
b) file pointer
17. Which of the following file modes opens a file for appending and reading in a binary file and moves
the files pointer at the end of the file if the file already exists or creates a new file?
a) a
b) a+
c) ab+
d) ab
c) ab+
18. Which method of pickle module is used to write onto a binary file?
a) dump()
b) load()
c) All of the above
a) dump()
19. Aishwarya is running her own boutique business. She wants to store data of all orders permanently
and fast processing of data through her boutique software. Suggest her to choose the appropriate
technique among the following.
c) She can use Python Lists without the Binary files concept.
d) She can use Python Dictionaries without the Binary file concept.
20. A programmer has confusion in understanding the behaviour of opening a file in “w” mode. Clear
his/her confusion, by suggesting the correct option among the given below. The behaviour of “w” mode
is
c) Opening will be succeeded if file exists with data and keeps the data intact.
d) Opening will be succeeded, if the file exists replaces the contents, do not exist, creates a new file.
d) Opening will be succeeded, if the file exists replaces the contents, do not exist, creates a new file.
21. Aditya is a class 12 student like you. He is practicing text file programming. He has a text file named
names.txt. He wants to display all the names contained in the text file. He has written one of the
following codes and succeeded in getting the output. Guess which among the following option might
have given the correct output.
print(“line”)
print(line)
print(line)
print(line)
22. A Student’s windows O/S got corrupted. He is trying to access his files through Command Prompt,
but unable to find out all his Binary data files pertaining to his project. Help him to find out all his binary
data files by suggesting the suitable extension name among the following.
a) .txt
b) .bin
c).dat
d) .com
c).dat
23. Given the following directory structure. Assume that the CWD is in the root folder where animals
directory resides. What is the relative path to the feline folder?
a) C:/animals/feline
b) animals/feline
c) feline/animals
b) animals/feline
24. Given the directory structure in Q 23. Assume that the CWD is in the feline folder. What is the relative
path to the file bears.gif?
a) C:/animals/ursine
b) animals/ursine
c) ..ursine/bears.gif
c) ..ursine/bears.gif
25. Given the directory structure in Q 23. Assume that the CWD is in the root folder where animals
directory resides. What is the absolute path to bears.gif?
a) C:/animals/ursine
b) ../animals/feline
c) /animals/ursine/bears.gif
c) /animals/ursine/bears.gif
26. Given the directory structure in Q 23. Assume that the CWD is in the feline folder what is the relative
path to lions.gif?
a) C:/animals/ursine
b) lions.gif
c) /animals/ursine/bears.gif
b) lions.gif
27. Master Adithya could able to read contents of a text file part by part. Out of the following which
option he would have been used?
a) read()
b) readlines()
c) readline()
d) readfullline()
c) readline()
28. In a program, there is a need of reading whole content of a textfile in a single shot and store it in the
form of list. Suggest the best option among the following
a) read()
b) readline()
c) readlines()
c) readlines()
Text file: opening a text file, text file open modes (r, r+, w, w+, a, a+), closing a
text file, opening a file using with clause
29. What is the default mode when the file is opened using the open() method ?
a) Write
b) Read
b) Read
31. Which of the following statements is true with respect to closing a file
c) Python automatically closes a file if the reference object of the file is allocated to another
c) Python automatically closes a file if the reference object of the file is allocated to another
a) Python doesn’t write data to the file until you close the file using the close() method
b) If the file is opened for writing and is not closed, data is not written into the target file.
c) Python can write data to the file even if you do not close the file
b) If the file is opened for writing and is not closed, data is not written into the target file.
34. When a file is opened in “w” mode, the file pointer is placed at
35. When a file is opened in “a” mode, the file pointer is placed at
(B): When a file is opened using the “with” statement, it need not be closed using the close() function.
38. When the file content has to be modified, we can use the ____________ mode
a) r
b) w
c) a
d) r+
a) r
39. Rajitha, during Practical Examination of Computer Science, has been assigned an incomplete
search() function to search in a text file “CAMP.txt”. The file “CAMP.txt” is created by his Teacher and the
following information is known about the file
i) File contains details of camp describing events of an adventure camp in text format
ii) File contains details of adventure activities like caving, trekking, paragliding, rafting and rock climbing
Rajitha has been assigned the task to complete the code and print the number of the word trekking
def search():
f = open(“CAMP.txt”,____) #Statement-1
A=____________________ #Statement-2
ct=0
for x in A:
p=x.split()
if p==”trekking”:
ct+=1
a) r
b) r+
c) rb
d) wb
a) r
39.2. Name the function that can be used by Rajitha to read the content of the file in statement -2.
a) f.read( )
b) f.readline ()
c) f.readlines( )
d)f.readl()
c) f.readlines( )
b) close(file)
c) f.close()
d) close()
c) f.close()
40. Rahul is assigned the task of storing the information of his class mates as student records in a text
file “Stu2021.txt”.He wants to create a file to write the records. Which of the following is not a valid
mode to open a file for writing the data?
a) w
b) r+
c) r
d) w+
c) r
41. Rohit of class 12 is asked by his Computer Science teacher to count the number of “the” and “and” in
a text file “Book.txt”. He was able to do the task to some extent, but was confused in some areas. Help
him out to complete the task
def displaytheand():
N=f.read()
M=N.split()
for x in M:
41.1. Which of the following is the correct one to be used in the place of statement 1
a) f=open(“Book.txt”,”w”)
b) f=open(“Book.txt”,”r”)
c) f=open(“Book.txt”,”a”)
d) F=open(“Book.txt”,”r”)
b) f=open(“Book.txt”,”r”)
41.2. Identify the correct statement that may be used in the place of statement 2
a) f.close()
b) f.close(“book.txt”)
c) close()
a) f.close()
42. Ajay is studying in an Engineering College in CSE branch. His sister, a class 12 student of Computer
Science comes to him one day asking him the difference between r+ and w+ modes of a file. What is the
correct answer Ajay would give to his sister?
b) In r+ mode, the file is created if it does not exist and erases the content if the file already exists; w+
raises an error if the file does not exist
c) In w+ mode, the file is created if it does not exist and erases the content if the file already exists; r+
raises an error if the file does not exist
c) In w+ mode, the file is created if it does not exist and erases the content if the file already exists; r+
raises an error if the file does not exist
43. Aparajitha joined an MNC company in Bangalore as a Python Programmer. Her task is to handle the
data available for the company in the form of text files and perform the search operations based on
specific criteria. Now she is asked to count the number of words in the file which start with “a” or “m”
(both upper and lower cases) in a text file “Passion.txt”. She needs your help as she is stuck up with
some statements. Please help her out to complete the task
def filecreate():
f=open(“Passion.txt”,”r”) #Statement 1
f=open(“Passion.txt”,”r”) # Statement 3
A,M=0,0
r=f.read()
for x in r:
if x[0]==”A” or x[0]==”a” :
f.close()
print(“A or a: “,A)
print(“M or m: “,M)
a) f=open(“Passion.txt”,”r”)
b) f=open(“Passion.txt”,”r+”)
c) f=open(“Passion.txt”,”w”)
d) f=open(“Passion.txt”,”a”)
c) f=open(“Passion.txt”,”w”)
a) f=open(“Passion.txt”,”r”)
b) f=open(“Passion.txt”,”w”)
c) f=open(“Passion.txt”,”a”)
d) f=open(“Passion.txt”,”rt”)
a) F.close()
b) f.close()
c) F.close(“passion.txt”)
d) No statement is required
b) f.close()
a) When you open a file for reading, if the file does not exist, an error occurs.
b) When you open a file for writing, if the file does not exist, a new file is created.
c) When you open a file for writing, if the file exists, the existing file is overwritten with the new file. (
b) Python automatically closes a file when the reference object of a file is reassigned to another file.
c) close() method returns a value which ensures the termination of the file stream
c) close() method returns a value which ensures the termination of the file stream
f = None
if i > 2:
break
print(f.closed)
a) True
b) False
c) None
d) Error
a) True
47. Which of the following statements is true with respect to the files in Python?
a) File can be opened with a file handle using open method without any arguments
b) File can be opened with a file handle using open method with one argument to read the file
c) File can be opened with a file handle using open method with one argument to write the file
d) File can be opened with a file handle using open method with one argument to append the file
b) File can be opened with a file handle using open method with one argument to read the file ✓
48. To open a file only for reading which of the following statement cannot be used :
a) f = open(“PYTHON.txt”)
b) f = open(“PYTHON.txt”, “rt”)
c) f = open(“PYTHON.txt”, “r”)
d) f = open(“PYTHON.txt”,”r+”)
d) f = open(“PYTHON.txt”,”r+”)