R4RIN
MCQS
Python MCQ Quiz Hub
File Handling in Python section 2
Choose a topic to test your knowledge and improve your Python skills
1. Rohan opened the file “myfile.txt” by using the following syntax. His friend told him few advantages of the given syntax. Help him to identify the correct advantage. with open ("myfile.txt", "a") as file_object:
In case the user forgets to close the file explicitly the file will closed automatically.
file handle will always be present in the beginning of the file even in append mode.
File will be processed faster
None of the above
2. Mohan wants to open the file to add some more content in the already existing file. Suggest him the suitable mode to open the file.
read mode
append mode
write mode
All the above
3. Aman jotted down few features of the “write mode”. Help him to identify the valid features.
If we open an already existing file in write mode, the previous data will be erased
In write mode, the file object will be positioned at the beginning of the file.
In write mode, if the file does not exist then the new file will be created.
All the above
4. Ananya jotted down few features of the “append mode”. Help her to identify the valid features.
If we open an existing file in append mode, the previous data will remain there.
In append mode the file object will be positioned at the end of the file.
In append mode, if the file does not exist then the new file will be created.
All the above
5. Which of the following methods can be used to write data in the file?
write( )
writelines( )
Both of the above
None of the above
6. Write the output of the following: >>> f = open("test.txt","w") >>> f.write("File #Handling")
17
16
14
15
7. Which of the following error is returned by the given code: >>> f = open("test.txt","w") >>> f.write(345)
Syntax Error
TypeError
StringError
Run Time Error
8. Which of the following method is used to clear the buffer?
clear( )
buffer( )
flush( )
clean( )
9. Which of the following method does not return the number of characters written in the file.
write( )
writelines( )
Both of the above
None of the above
10. Fill in the blank in the given code: >>> fo = open("myfile.txt",'w') >>> lines = ["Hello ", "Writing strings ", "third line"] >>> fo._____________(lines) >>>myobject.close()
write( )
writelines( )
. writeline( )
None of the above
11. In order to read the content from the file, we can open file in ___________
“r” mode
“r+” mode
“w+” mode
All the above
12. Which of the following method is used to read a specified number of bytes of data from a data file.
read( )
read(n)
readlines( )
reading(n)
13. Write the output of the following: f=open("test.txt","w+") f.write("File-Handling") a=f.read(5) print(a)
File-
File
File-H
No Output
14. Write the output of the following: f=open("test.txt","w+") f.write("FileHandling") f.seek(5) a=f.read(5) print(a)
andli
Handl
eHand
No Output
15. Write the output of the following: f=open("test.txt","w+") f.write("FileHandling") f.seek(0) a=f.read() print(a)
File
Handling
FileHandling
No Output
16. Write the output of the following: f=open("test.txt","w+") f.write("FileHandling") f.seek(0) a=f.read(-1) print(a)
File
Handling
FileHandling
No Output
17. Which of the following method reads one complete line from a file?
read( )
read(n)
readline( )
readlines( )
18. Write the output of the following: f=open("test.txt","w+") f.write("File Handling") f.seek(0) a=f.readline(-1) print(a)
File Handling
FileHandling
File
No Output
19. Write the output of the following: f=open("test.txt","w+") f.write("File Handling") f.seek(0) a=f.readline(2) print(a)
File Handling
FileHandling
Fi
No Output
20. Select the correct statement about the code given below: >>> myobj=open("myfile.txt", 'r') >>> print(myobj.readlines())
This code will read one line from the file.
This code will read all the lines from the file.
This code will read only last line from the file.
None of the above
21. Write the output of the following: f=open("test.txt","w+") L = ["My name ", "is ", "Amit"] f.writelines(L) f.seek(0) a=f.readlines() print(type(a))
<class ‘tuple’>
<class ‘list’>
<class ‘string’>
None of the above
22. Which of the following function return the data of the file in the form of list?
read( )
read(n)
readline( )
readlines( )
23. Sanjeev has written a program which is showing an error. As a friend of Sanjeev, help him to identify the wrong statement. f=open("test.txt","w") #Statement1 L = ["My name ", "is ", "Amit"] #Statement2 f.writeline(L) #Statement3 f.close() #Statement4
. Statement1
. Statement2
. Statemen3
. Statement4
24. __________________ function returns an integer that specifies the current position of the file object in the file.
seek( )
tell( )
disp( )
None of the above
25. _____ method is used to position the file object at a particular position in a file.
tell( )
seek( )
put( )
None of the above
26. In reference to the code given below, the file object will move _______ bytes. file_object.seek(10, 0)
0
10
5
4
27. Ravi wants to move the file object 5 bytes from the current position of the file object. As a friend of Ravi, help him to write the code.
file_object.seek(5, 1)
file_object.seek(1, 5)
file_object.seek(5, 0)
file_object.seek(5, 2)
28. Write the output of the following code: f=open("test.txt","w+") f.write("My name is Amit ") f.seek(5,0) a=f.read(5) print(a)
me i
. ame is
me is
Error
29. Write the output of the following: f=open("test.txt","r") print(f.tell())
1
2
-1
0
30. Write the output of the following: f=open("test.txt","r") print(f.tell(),end="6") f.seek(5) print(f.tell())
165
650
065
506
31. How many functions are used in the given code? fileobject=open("practice.txt","r") str = fileobject.readline() while str: print(str) str=fileobject.readline() fileobject.close()
3
4
5
6
32. ________ method is used to write the objects in a binary file.
write( )
dump( )
load( )
writer( )
33. ______ method is used to read data from a binary file.
a. write( )
dump( )
load( )
writer( )
34. Which module is to be imported for working in binary file?
unpickle
pickle
pickling
unpickling
35. Which of the following statement open the file “marker.txt” so that we can read existing content from file?
f = open(“marker.txt”, “r”)
f = Open(“marker.txt”, “r”)
f = open(“marker.txt”, “w”)
None of the above
36. Which of the following statement open the file “marker.txt” as a blank file?
f = open(“marker.txt”, “r”)
f = open(“marker.txt”, “rb”)
f = open(“marker.txt”, “w”)
None of the above
37. Amit has written the following statement. He is working with ______ f = open("data", "rb")
Text File
CSV File
Binary File
None of the above
38. Which of the following option is correct?
if we try to write in a text file that does not exist, an error occurs
if we try to read a text file that does not exist, the file gets created.
if we try to write on a text file that does not exist, the file gets Created.
None of the above
39. Correct syntax of tell( ) function is _________. #f is file object
f.tell( )
tell(f)
f_tell( )
None of the above
40. The syntax of seek() is: file_object.seek(offset [, reference_point]). ___________________ value of reference_point indicate end of file
0
1
2
3
41. Which of the following statement is wrong in reference to Text file?
A text file is usually considered as a sequence of characters consisting of alphabets, numbers and other special symbols.
Each line of a text file is terminated by a special character.
Text files are not in Human readable form.
Text files can be opened in Text editor.
42. Identify the statement which can not be interpret from the given code:f = open("test.dat", "ab+")
test.dat is a binary file
reading operation can be done on test.dat
appending operation can be done on test.dat
test.dat contains records about books
43. Which module is to be imported for CSV file?
pickle
unpickle
csv
pandas
44. Write the output of the following code : import csv f = open(“data.csv”, ‘r’) row = csv.reader(f) print(row)
It prints the memory address where csv.reader object is stored
It prints the first record of data.csv
It prints all the records of data.csv
None of the above
45. _______ function help us to read the csv file.
reader( )
read( )
read(n)
readline( )
46. Write the output of the second print statement of the given code: f=open("test.txt","r") print(f.read()) f.seek(7) print(f.read()) Output of first print statement is : MynameisAmit
isAmit
sAmit
Amit
eisAmit
47. Which of the following method returns an integer value?
seek( )
read( )
tell( )
readline( )
48. ________ refers to the process of converting the structure to a byte stream before writing it to the file.
pickling
unpickling
reading
writing
49. Fill in the blank in the given code : import pickle f = open("data.dat", "rb") l = pickle._______(f) print(l) f.close()
dump
load
write
list
50. Write the output of the following code: f = open("data.txt","w") L=["My ","name ","is ","amit"] f.writelines(L) f.close() f = open("data.txt","r") print(len(f.read()))
13
14
15
16
51. Write the output of the following code: f = open("data.txt","w") L=["My ","name ","is ","amit"] f.writelines(L) f.close() f = open("data.txt","r") print(len(f.readlines()))
4
5
6
7
52. Write the output of the following code: f = open("data.txt","w") L=["My ","name ","is ","amit"] f.writelines(L) f.close() f = open("data.txt","r") print(len(f.readline()))
2
3
4
5
53. import pickle f = open("data.dat", "wb") L = [1, 2, 3] pickle._______ f.close()
dump(f, L)
load(f, L)
dump(L, f)
load(L, f)
54. Which of the following statement will return attribute error?
print(len(f.readlines( ).split( ))) #f is file handle
print(len(f.readline( ).split( ))) #f is file handle
print(len(f.read( ).split( ))) #f is file handle
None of the above
55. Ravi is writing a program for counting the number of words in a text file named “data.txt”. He has written the incomplete code. Help him to complete the code. f = open("_________","r") # Statement 1 d = f._________ # Statement 2 nw = d._________ # Statement 3 print("Number of words are", _________(nw)) # Statement 4 Identify the suitable code for blank space in the line marked as Statement 1
“data.txt”
data.txt
“data”
data
56. Ananya was writing a program of reading data from csv file named “data.csv”. She writes the incomplete code. As a friend of Ananya help her to complete the code. ________ csv #Statement1 f=open("data.csv", '_______') #Statement2 d=csv.___________(f) #Statement3 for __________ in d: #Statement4 print(row) Identify the suitable option for Statement 1
import
create
data
Import
57. Chinki is writing a program to copy the data from “data.csv” to “temp.csv”. However she is getting some error in executing the following program due to some missing commands. Help her to execute it successfully. import csv f=open(“data.csv”,”r”) f1=open(“temp.csv”,’__________’) #Statement 1 d=csv._________(f) #Statement 2 d1=csv.writer(f1) for i in d: ___________.writerow(i) #Statement3 f.close( ) f1.close( )
r
rb+
wa
w
Submit