TEXT FILE HANDLING - Unlocked
TEXT FILE HANDLING - Unlocked
MCQS
1 Python statement to open a text file “DATA.TXT” so that new contents can be
written on it
a. F = open(DATA.TXT,”w”)
b. F = open(“DATA.TXT”)
c. F = open(“DATA.TXT”,”r”)
d. F = open(“DATA.TXT”,”w”)
2 Python statement to open a text file “DATA.TXT” so that new content can be added
to the end of previous content file.
a. F = open(“DATA.TXT”,”w”)
b. F = open(“DATA.TXT”,”a”)
c. F = open(DATA.TXT,”a”)
d. F = open(“DATA.TXT”)
3 Python statement to open a text file “DATA.TXT” so that existing contents can be
read from file.
a. F = open(“DATA.TXT”)
b. F = open(“DATA.TXT”,”R”)
c. F = open(DATA.TXT)
d. F = open(“DATA.TXT”,”a”)
4 Which of the following Python statement is the correct way to close the file opened
in a file variable (F)
a. close()
b. F.close()
c. F(close)
d. F.close
5 Which of the following is not a method of opening files?
a. Read
b. Write
c. Replace
d. Append
6 Which of the following is the last action to be performed which performing file
handling?
a. Save
b. Close
c. Open
d. Write
7 What is the data type of data return from text file using read()?
a. String
b. Integer
c. Float
d. Boolean
8 Every line of text file terminates with special character known as:
a. delimiter
b. EOF
c. EOL
d. stream
9 Which of the following function can read all the content of file and store it in single
string?
a. read()
b. readline()
c. readlines()
d. readall()
10 To read 10 characters from file, which of the following function will be used?
a. readlines(10)
b. readline(10)
c. read(char=10)
d. read(10)
11 readlines() function read all the lines from file and store it in the form of?
a. Tuple
b. List
c. Dictionary
d. String
12 To open a file Poem.txt ,which is stored at d:\resource, for WRITING , we can use
(Choose all correct answers)
a. F=open("d:\resource\Poem.txt","w")
b. F=open(r"d:\resource\Poem.txt","w")
c. F=open("d:\\resource\Poem.txt","w")
d. F=open("d:\\resource\\Poem.txt","w")
e. F=open(file="d:\resource\Poem.txt","w")
13 Which of the following function will write list of strings in a file?
a. write()
b. writelines()
c. writeline()
d. write(*)
14 If file open mode is not specified while opening file, the default mode will be:
a. read
b. write
c. append
d. will give an error
15 If we want to add more content to any existing file, in that case the file must be
opened in:
a. read
b. write
c. append
d. not allowed to add content in existing file
16 Which of the following statement are TRUE regarding the opening modes of file?
(Choose all correct answer)
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, an error occurs
c. When you open file for append, if the file does not exist, an error occurs
d. When you open file for reading, if the file does not exist, program will open
empty file.
e. When you open file for writing, if the file does not exist, program will create
a new file.
f. When you open file for writing, if the file exist, the existing content will be
overwritten.
17 Which of the following command can be used to read next line of file using file
object MYFILE.
a. MYFILE.read()
b. MYFILE.readline()
c. MYFILE.readlines()
d. MYFILE.read(next)
18 Which of the following function is used to check whether the file “ARTICLE.TXT”
exists or not?
a. os.path.exists(“ARTICLE.TXT”)
b. os.path.exist(“ARTICLE.TXT”)
c. os.path.isFile(“ARTICLE.TXT”)
d. os.path.found(“ARTICLE.TXT”)
19 In Python default EOL character is:
a. \t
b. \r
c. \n
d. \e
20 What does r+ signifies while opening file?
a. It will open file for read mode even if file not exists
b. It will open file for read and write, new file will be created if file not exists
c. It will open file for read only and error occurs if file not exists.
d. It will open file for read and write, error occurs if file not exists.
21 When we open file in read mode the position of file pointer(cursor) is at:
a. The place where last read was performed
b. First character
c. Last character
d. None of the above
22 When we open file in append mode the position of file pointer(cursor) is at:
a. The place where last read was performed
b. First character
c. Last character
d. None of the above
23 Which of the following is not the correct mode to open file?
a. rw
b. r+
c. w+
d. a+
24 Which of the following mode work as: if file not exists, new file will be created, if file
exists, existing content will be overwritten, both read and write operation can be
performed:
a. a+
b. r+
c. w+
d. rw
25 Which of the following function is used to write string in file?
a. write()
b. writeline()
c. writelines()
d. writestr()
26 Which of the following function is used to write list in file?
a. write()
b. writeline()
c. writelines()
d. writestr()
27 In relative path the current working directory is denoted by dot(.) whereas parent
directory is denoted by:
a. Forward slash(/)
b. Double dot (..)
c. Double back slash(\\)
d. Triple dot(…)
28 In file handling what does “a” and “r” means?
a. read and append
b. append and read
c. add and read
d. all and read
29 Which of the following statement is used to set file position (cursor) to an offset
value from the beginning of file?
a. file.seek(offset,0)
b. file.seek(offset,1)
c. file.seek(offset,2)
d. None of the above
30 Which of the following function is used to retrieve current position of file pointer
(cursor) in file?
a. loc()
b. seek()
c. tell()
d. pos()
TRUE/FALSE
1 The default file opening mode is write mode.
2 Opening file in append mode will erase the previous data.
3 When you open file for read, the file must exist otherwise python will raise error.
4 A file mode specifies what type of operation we can perform on file.
5 readlines() function returns a list of string where each line is separated by ‘\n’
6 An absolute path always begins from current working directory
7 It is not necessary to create file in the default folder where Python is installed.
8 with statement ensures that opened file will be automatically closed irrespective of
program execution status.
9 When you open file for writing, if file already exists then existing content will be
overwritten.
10 flush() function is used to clear the file buffer explicitly.
11 w+ mode allows to write and read even if file not exists.
12 read() function can read the entire content of file.
13 File opened for write operation will store the data even if we forget to close the file
after write operation.
14 We can open file stored in d:\newfolder\hello.txt, even if our program is stored in
d:\myprogs\
15 We can read file without opening file.
OUTPUT BASED MCQS
1 Considering the content stored in file “WORLDCUP.TXT”, write the output
India won the Cricket world cup of 1983
f = open(“WORLDCUP.TXT”)
print(f.read(2))
print(f.read(2))
print(f.read(4))
a. In b. In
di di
a wo a won
c. nd d. Ind
ia ia
won won
2 Considering the content stored in file “CORONA.TXT”
O Corona O Corona
Jaldi se tum Go na
Social Distancing ka palan karona
sabse 1 meter ki duri rakhona
Lockdown me ghar me ho to
Online padhai karona
Complete the missing statement using ‘for’ loop to print all the lines of file
f = open(“CORONA.TXT”)
for ______________________ :
print(____)
a. for x in f.read() b. for x in f
print(x) print(x)
O Corona O Corona
Jaldi se tum Go na
Social Distancing ka palan karona
Sabse 1 meter ki duri rakhona
Lockdown me ghar me ho to
online padhai karona
O Corona O Corona
Jaldi se tum Go na
The function should display:
Social Distancing ka palan karona
Sabse 1 meter ki duri rakhona
2 Write a function in python to count the number of lines in “POEM.txt” begins
from Upper case character.
For e.g if the content of file is :
O Corona O Corona
Jaldi se tum Go na
Social Distancing ka palan karona
sabse 1 meter ki duri rakhona
Lockdown me ghar me ho to
online padhai karona
Output should be: Lines starting from Capital letters: 4
3 Write a function dispS() in Python to read from text file “POEM.TXT” and
display those lines which starts with “S” or “J”
For example:
If the content of the file is “
O Corona O Corona
Jaldi se tum Go na
Social Distancing ka palan karona
Sabse 1 meter ki duri rakhona
Lockdown me ghar me ho to
online padhai karona
O Corona O Corona
Jaldi se tum Go na
The function should display:
Jaldi se tum Go na
Social Distancing ka palan karona
Sabse 1 meter ki duri rakhona
Jaldi se tum Go na
4 Write a function ShowDigLines() in Python, which reads the content of text file
“story.txt” and displays all those lines which contains digits and display count of
those lines.
For example if the content of file is:
Amrapali was a queen of Gareware kingdom in
the year 1911. She had 2 daughters.
Her palace had 200 rooms.
Then the output should be displayed as:
the year 1911. She had 2 daughters.
Her palace had 200 rooms.
Total lines with digit: 2
5 Write a function ShowCorrectLine() in Python to read the content of file and display
only those lines which ends with “.”
For example if the content of file is:
Amrapali was a queen of Gareware kingdom in
the year 1911. She had 2 daughters.
Her palace had 200 rooms.
Then the function should display output as:
the year 1911. She had 2 daughters.
Her palace had 200 rooms.