File Handling
File Handling
1
9. If the following statement is used to read the contents of a 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
10. 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.
11. 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()
12. 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()
13. 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
14. Which of the following statement is incorrect in the context of binary files?
a. Information is stored in the same format in which the information is held in memory.
b. No character translation takes place
c. Every line ends with a new line character d. pickle module is used for reading and writing
15. 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
16. Which of the following statement is true?
a. pickling creates an object from a sequence of bytes b. pickling is used for object serialization
c. pickling is used for object deserialization d. pickling is used to manage all types of files in Python
2
17. Syntax of seek function in Python is myfile.seek(offset, reference_point) where myfile is the file
object. What is the default value of reference_point?
a. 0 b. 1 c. 2 d. 3
18. Which of the following character acts as default delimiter in a csv file?
a. (colon) : b. (hyphen) - c. (comma) , d. (vertical line) |
19. Syntax for opening Student.csv file in write mode is
myfile = open("Student.csv","w",newline='').
What is the importance of newline=''?
a. A newline gets added to the file b. Empty string gets appended to the first line.
c. Empty string gets appended to all lines. d. EOL translation is suppressed
20. Which of the following statement opens a binary file record.bin in write mode and writes data
from a list lst1 = [1,2,3,4] on the binary file?
a. with open('record.bin','wb') as myfile:
pickle.dump(lst1,myfile)
b. with open('record.bin','wb') as myfile:
pickle.dump(myfile,lst1)
c. with open('record.bin','wb+') as myfile:
pickle.dump(myfile,lst1)
d. with open('record.bin','ab') as myfile:
pickle.dump(myfile,lst1)
21. 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)
Ans:-
22. Which of the following python modules is imported to store and retrieve objects using the
process of serialization and deserialization?
a.csv b. binary c. math d.pickle
3
23. Which of the following function is used with the csv module in python to read the contents of a
csv file into an object?
a.readrow() b. readrows() c. reader() d. load()
24. Suppose the content of a text file Rhymes.txt is:
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=”#”)
Ans:-
29. Raghav is trying to write a tuple tup1 = (1,2,3,4,5) on a binary file test.bin. Consider the
following code written by him.
import pickle
tup1 = (1,2,3,4,5)
myfile = open("test.bin",'wb')
pickle._______ #Statement 1
myfile.close()
Identify the missing code in Statement 1.
Ans:-
5
30. Suppose content of 'Myfile.txt' is
Humpty Dumpty sat on a wall
Humpty Dumpty had a great fall
All the king's horses and all the king's men
Couldn't put Humpty together again
What will be the output of the following code?
myfile = open("Myfile.txt")
record = myfile.read().split()
print(len(record))
myfile.close()
Ans:-
6
33. Suppose content of 'Myfile.txt' is
Ek Bharat Shreshtha Bharat
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()
Ans:-
7
35. 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?
Ans:-