MULTIPLE CHOICE QUESTIONS
1. Select the correct output of the following code - fp.seek(5, 1)
a) Move file pointer five characters ahead from the current position.
b) Move file pointer five characters ahead from the beginning of a file.
c) Move file pointer five characters behind from the current position.
d) Move file pointer five characters behind ahead from the end of a file.
2. If the file is opened in write mode and already exists, it truncates the existing content and places the
filehandle at the beginning of the file.
a) True
b) False
3. Which method is used to read a text file line by line
a) read(1)
b) readlines(1)
c) readline()
d) line()
4. Select the correct method to write a list of lines to a file
a) write(list)
b) writelines(list)
c) writelist(list)
d) None of these
5. Select the incorrect file access mode
a) r
b) ab+
c) rw+
d) wb+
6. Which method is used to sets the position of a file pointer
a) ftell()
b) fseek()
c) tell()
d) seek()
7. Select the correct mode to open a file for appending as well as reading
a) a+
b) ar
c) rw
d) ar+
8. Select true statement when a file is opened using the with statement
a) The with statement makes exception handling complex
b) The file is automatically closed after leaving the block, and all the resources that are tied up with the
file are released.
c) File reading and writing are faster using the with statement.
d) None of these
9. Processing of a text file is faster than binary files
a) True
b) False
10. Which mode creates new file if the file does not exist?
a) Write
b) Append
c) Both a and b
d) None of the above
11. readlines() method returns
a) String
b) List
c) Dictionary
d) Tuple
12. Which function is used to read data from a binary file?
a) read()
b) readlines()
c) dump()
d) load()
13. If the file pointer is at the end of 5th line in the file “Summary.txt”, then which of the following
options can be used to read the remaining lines?
a) f.read()
b) f.readlines()
c) f.read(all)
d) f.readline()
14. module is used for serializing and de-serializing any Python object structure
a) csv
b) math
c) pickle
d) pandas
15. function returns string
a) read()
b) readline()
c) Both a and b
d) None of the above
16. Which of the following function takes 2 arguments?
a) load()
b) dump()
c) Both a and b
d) None of the above
17. Almost all the files in our computer is stored as file
a) Text
b) Binary
c) CSV
d) None
18. There is no delimiter to end a line in binary files.
a) True
b) False
19. function returns the current position of the file pointer.
a) seek()
b) tell()
c) cur()
d) get()
20. Which statement will move file pointer 10 bytes backward from the current position?
a) f.seek(-10,0)
b) f.seek(10,1)
c) f.seek(-10,1)
d) f.seek(-10,2)
CASE-BASED/ SOURCE-BASED INTEGRATED QUESTIONS
1. Madhur, a student of class 12th, is learning CSV File Module in Python. During examination, he
has been assigned an incomplete python code (shown below) to create a CSV File ‘Student.csv’
(content shown below). Help him in completing the code which creates the desired CSV File.
CSV File – student.csv
1,AKSHAY,XII,A
2,ABHISHEK,XII,A
3,ARVIND,XII,A
4,RAVI,XII,A
5,ASHISH,XII,A
Incomplete Code
import_____ #Statement-1
fh = open( , , newline=”) #Statement-2
stuwriter = csv. #Statement-3
data = []
header = [‘ROLL_NO’, ‘NAME’, ‘CLASS’, ‘SECTION’]
data.append(header)
for i in range(5):
roll_no = int(input(“Enter Roll Number : “))
name = input(“Enter Name : “)
Class = input(“Enter Class : “)
section = input(“Enter Section : “)
rec = [ ____ ] #Statement-4
data.append(rec)
stuwriter. (data) #Statement-5
fh.close()
(i) Identify the suitable code for blank space in line marked as Statement-1.
(ii) Identify the missing code for blank space in line marked as Statement-2?
(iii) Give the function name (with argument) that should be used in the blank space of line marked as
Statement-3
(iv) Identify the suitable code for blank space in line marked as Statement-4.
(v) Choose the function name that should be used in the blank space of line marked as Statement-5 to
create the desired CSV File?
2. Amaira’s teacher asked her to count the no. of times words ‘he’ and ‘she’ comes in a text file
“poem.txt”. She wrote the code, but got confused in few statements. Help her complete the
following code.
f=open("poem.txt", “ ”) #Statement-1
data=f. #Statement-2
data=data. #Statement-3
c=0
c1=0
for ch in data:
ch=ch #Statement-4
if ch=="HE" :
c=c+1
elif ch=="SHE":
c1+=1
print("No of She",c1)
print("No of he",c)
f. #Statement-5
(i) Which of the following modes to be used in Statement-1while opening the file?
(ii) What should come in statement-2 to read all the contents of the file as a single string?
(iii) Which function should come in Statement-3 to get a list of words?
(iv) Which function should be used in Statement-4 to convert the string in uppercase
(v) What should be written in Statement-5 to close the file?
3. Amit Kumar of class 12 is writing a program to store roman numbers and find their equivalents
using a dictionary. He has written the following code. As a programmer, help him to successfully
execute the given task.
import___________#Line 1
numericals = {1: ‘I’, 4 : ‘IV’, 5: ‘V’ , 9: ‘IX’, 10:’X’, 40:’XL’,50:’L’, 90:’XC’, 100:’C’,
400:’CD’,500:’D’,900:’CM’,1000:’M’}
file1 = open(“roman.log”,” ______ ”) #Line 2
pickle.dump(numerals,file1)
file1.close()
file2 = open(“roman.log”,’ ________ ”) #Line 3
num = pickle. (file2) #Line 4
file2 __________ #Line 5
n=0
while n!=-1:
print(“Enter 1,4,5,9,10,40,50,90,100,400,500,900,1000:”)
print(“or enter -1 to exit”)
n = int(input(“Enter numbers”))
if n!= -1:
print(“Equivalent roman number of this numeral is:”,num[n])
else:
print(“Thank You”)
(i) Name the module he should import in Line 1
(ii) In which mode, Amit should open the file to add data into the file in Line #2 without losing the
previous contents
(iii) Fill in the blank in Line 3 to read the data from a binary file.
(iv) Fill in the blank in Line 4 to read the contents of the file “roman.log”
(v) Fill in the blank in Line 4 to close the file.
4. Manisha has been asked to write a code to count and display the no. of lines that starts with ‘S’
in a text file MyFile.txt. She is not confident with few statements and left it blank. Help her
complete the code.
myfile = open( ) # line1
line_count = 0
data = _______________ # line2
for line in data:
if == 'S': # line3
line_count += 1
print(“No. of lines that start with S:”, ) # line4
myfile ____________ # line5
(i) Select correct option to open file in read mode in line1.
(ii) Write the function in line2 to read all the lines of the file.
(iii) What should come at line3 to compare the first character of the line.
(iv) Select the correct option for line4 to display no. of records that starts with ‘S’
(v) Which statement should be used in line5 to close the file ‘Myfile.txt’?
FILL IN THE BLANKS
1. A collection of bytes stored in computer’s secondary memory is known as .
2. is a process of storing data into files and allows to performs various tasks such as read,
write, append, search and modify in files.
3. The transfer of data from program to memory (RAM) to permanent storage device (hard disk) and vice
versa are known as .
4. A is a file that stores data in a specific format on secondary storage devices.
5. In files each line terminates with EOL or ‘\n’ or carriage return, or ‘\r\n’.
6. To open file data.txt for reading, open function will be written as f = .
7. To open file data.txt for writing, open function will be written as f = .
8. In f=open(“data.txt”,”w”), f refers to .
9. To close file in a program function is used.
10. A function reads first 15 characters of file.
11. A function reads all bytes in the form of a string.
12. A function reads all lines from the file.
13. A function requires a string (File_Path) as parameter to write in the file.
14. A function requires a sequence of lines, lists, tuples etc. to write data into file.
15. To add data into an existing file mode is used.
16. A function is used to write contents of buffer onto storage.
17. A text file stores data in or form.
18. A is plain text file which contains list of data in tabular form.
19. You can create a file using function in python.
20. A symbol is used to perform reading as well as writing on files in python.
SHORT ANSWER QUESTIONS(2/3 marks)
1. What is the significance of ‘r+’ in file handling?
2. Write a statement in Python to open a binary file named “First.dat” and add contents to it
without losing its contents.
3. What is the advantage of using ‘with’ clause in file handling for opening a file?
4. Read the following Python code carefully and answers the question given after the code.
import pickle
infile = open('phonebook.dat', ' ____ ') #Line 1
phonebook = _________ (infile) #Line 2
print(phonebook)
infile.close()
a. Fill in the blank in line 1 to open file in binary mode for reading.
b. Fill in the blank in line 2 to read object from file.
5. Suppose content of 'Myfile.txt' is:
Twinkle twinkle little star
How I wonder what you are
Up above the world so high
Like a diamond in the sky
What will be the output of the following code?
myfile = open("Myfile.txt")
data = myfile.readlines()
print(len(data))
myfile.close()
6. What is the difference between readline() and readlines() function in file handling?
7. Write a Python program to find the number of lines in a text file ‘diary.txt’.
8. Write a function that counts and display the number of 5 letter words in a text file “Sample.txt”
9. How are the following codes different from one another?
1) M=open(“poem.txt”,”r”)
M.read()
2) M=open(“poem.txt”,”r”)
M.read(10)
10. Write a python program to create and read the city.txt file in one go and print the contents on
the output screen.
11. Consider following lines for the file friends.txt and predict the output:
Friends are crazy, Friends are naughty !
Friends are honest, Friends are best !
Friends are like keygen, friends are like license key !
We are nothing without friends, Life is not possible without friends !
f = open("friends.txt")
l = f.readline()
l2 = f.readline(18)
ch3=f.read(10)
print(l2)
print(ch3)
print(f.readline())
f.close()
12. Write a function display_oddLines() to display odd number lines from the text file. Consider the
file – friends.txt.
13. Write a function cust_data() to ask user to enter their names and age to store data in
customer.txt file.
14. Write a function VowelCount() in Python, which should read each character of a text file
Story.txt, should count and display the occurrence of alphabets vowels.
Example: If the file content is as follows:
Updated information As simplified by official websites.
The VowelCount() function should display the output as:
A or a:4
E or e :4
I or i :8
O or o : 0
U or u: 1
15. A binary file “STUDENT.DAT” has structure [admission_number, Name, Percentage]. Write a
function countrec() in Python that would read contents of the file “STUDENT.DAT” and display the
details of those students whose percentage is above 75. Also display number of students scoring above
75%.