100% found this document useful (2 votes)
233 views

TEXT FILE HANDLING - Unlocked

The document contains 30 multiple choice questions about file handling in Python. It covers topics like opening and closing files, reading and writing to files, different file modes, file paths, file methods and functions like read(), readline(), readlines() etc. The questions test concepts like opening files for reading, writing and appending, default file modes, reading/writing strings and lists to files, relative and absolute paths, checking if file exists, positioning file pointer etc.

Uploaded by

lanfury45
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
233 views

TEXT FILE HANDLING - Unlocked

The document contains 30 multiple choice questions about file handling in Python. It covers topics like opening and closing files, reading and writing to files, different file modes, file paths, file methods and functions like read(), readline(), readlines() etc. The questions test concepts like opening files for reading, writing and appending, default file modes, reading/writing strings and lists to files, relative and absolute paths, checking if file exists, positioning file pointer etc.

Uploaded by

lanfury45
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

TEXT FILE HANDLING

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)

c. for x in range(len(f)) d. for f in x


print(x) print(f)
3 Choose the correct code to print size of file (number of characters).
a. f = open('poem.txt') b. f = open(‘poem.txt’)
s = f.readlines() s = f.readline()
print(‘Size of file is ‘,len(s)) print(‘Size of file is ‘,len(s))
c. f = open('poem.txt') d. f = open(‘poem.txt’)
s = f.read() s = read(EOF)
print(‘Size of file is ‘,len(s)) print(‘Size of file is ‘,len(s))
4 Based on given code choose the correct option to fill in code:
_______ open(‘poem.txt’) as F:
a. file
b. with
c. read()
d. flush()
5 Considering the content of file “QUOTES.TXT”, write the output of following
code:
Content of file: India is Best
f = open(“QUOTES.TXT”,”a”)
print(f.tell())
a. 0
b. 14
c. 13
d. 11
HOTS (CHARACTER READING)
1 Polina Raj has used a text editing software to type some text in an article.
After saving the article as MYNOTES.TXT, she realised that she has wrongly
typed alphabet K in place of alphabet C everywhere in the article.
Write a function definition for PURETEXT() in Python that would display the
corrected version of the entire article of the file MYNOTES. TXT with all the
alphabets “K” to be displayed as an alphabet “C” on screen.
NOTE Assuming that MYNOTES. TXT does not contain any C alphabet
otherwise.
Example:
If Polina has stored the following content in the file MYNOTES.TXT:
I OWN A KUTE LITTLE KAR.
I KARE FOR IT AS MY KHILD.
The function PURETEXT() should display the following content:
I OWN A CUTE LITTLE CAR.
I CARE FOR IT AS MY CHILD
2 Write function definition for COUNTNU( ) in Python to read the content of a
text file
CONTENT.TXT, count the characters N and U (in lower case as well as
upper case) present in the file.
e.g. Assume the content of the file CONTENT. TXT is as follows:
“New research shows that our closest evolutionary relatives have all of the
cognitive, capacities required for cooking except an understanding of how to
control fire.”
The function COUNTNU( )should display the following:
13
3 Write a function EUCount() in Python, which should reads each character
of a text file. “IMP.TXT”, should count and display the occurrence of
alphabets E and U (including small cases e and u too)
e.g. If the file contains is as follows:
Update information is simplified by official websites.
The EUCount() function should display the output as:
E:4
U:1
4 Write a function CountDig() in Python, which reads the content of text file
“story.txt” and displays the number of digits in it.
e.g. if the file contains:
Amrapali was a queen of Gareware kingdom in
the year 1911. She had 2 daughters.
Her palace had 200 rooms.
Then the output on the screen should be
Number of digits in story:8
5 Write a function VowelCount() in Python, which should reads each
character of a text file. “IMP.TXT”, should count and display the
occurrence of vowels (including small and capital)
e.g. If the file contains is as follows:
Update information is simplified by official websites.
The VowelCount() function should display the output as:
Total vowels: 20
HOTS(WORD READING)
1 Write a function in python to read lines from file “POEM.txt” and count how
many times the word “Corona” exists in file.
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
O Corona O Corona
Jaldi se tum Go na
Output should be: Number of time word Corona occurs : 4
2 Write a function in python to read contents from file “POEM.txt” and display
all those words, which has two characters in it.
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
O Corona O Corona
Jaldi se tum Go na
Output should be : se Go na ka ki me me ho to se Go na
3 Write a function COUNT() in Python to read contents from file
“REPEATED.TXT”, to count and display the occurrence of the word “Catholic”
or “mother”.
For example:
If the content of the file is “Nory was a Catholic because her mother was a Catholic
, and Nory’s mother was a Catholic because her father was a Catholic , and her
father was a Catholic because his mother was a Catholic , or had been
The function should display:
Count of Catholic, mother is 9
4 Write a function COUNT() in Python to read contents from file
“REPEATED.TXT”, to count and display the occurrence of the word “Catholic”
and “mother”.
For example:
If the content of the file is “Nory was a Catholic because her mother was a Catholic
, and Nory’s mother was a Catholic because her father was a Catholic , and her
father was a Catholic because his mother was a Catholic , or had been
The function should display:
Count of Catholic:6
Count of mother: 3
5 Write function definition fof WORDWCHAR() in Python to read the content of a
text file FUN.TXT and display all those words, begins from letter ‘w’(including
small and capital)
e.g. If the content of the file FUN.TXT is as follows:
“When I was a small child, I used to play in the garden with my grand mom.
Those days were amazingly funful and I remember all the moments of that time”
The function WORDWCHAR() should display the following output:
When#was#with#were#
HOTS (LINE READING)
1 Write a function dispS() in Python to read from text file “POEM.TXT” and
display those lines which starts with “S”
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:
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.

You might also like