sInfoEra
Home
MCQs
Python — Quiz
File Handling in Python section 2
57 questions · Test your knowledge
Home
/
MCQs
/
File Handling in Python section 2
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:
A
In case the user forgets to close the file explicitly the file will closed automatically.
B
file handle will always be present in the beginning of the file even in append mode.
C
File will be processed faster
D
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.
A
read mode
B
append mode
C
write mode
D
All the above
3
Aman jotted down few features of the “write modeâ€. Help him to identify the valid features.
A
If we open an already existing file in write mode, the previous data will be erased
B
In write mode, the file object will be positioned at the beginning of the file.
C
In write mode, if the file does not exist then the new file will be created.
D
All the above
4
Ananya jotted down few features of the “append modeâ€. Help her to identify the valid features.
A
If we open an existing file in append mode, the previous data will remain there.
B
In append mode the file object will be positioned at the end of the file.
C
In append mode, if the file does not exist then the new file will be created.
D
All the above
5
Which of the following methods can be used to write data in the file?
A
write( )
B
writelines( )
C
Both of the above
D
None of the above
6
Write the output of the following: >>> f = open("test.txt","w") >>> f.write("File #Handling")
A
17
B
16
C
14
D
15
7
Which of the following error is returned by the given code: >>> f = open("test.txt","w") >>> f.write(345)
A
Syntax Error
B
TypeError
C
StringError
D
Run Time Error
8
Which of the following method is used to clear the buffer?
A
clear( )
B
buffer( )
C
flush( )
D
clean( )
9
Which of the following method does not return the number of characters written in the file.
A
write( )
B
writelines( )
C
Both of the above
D
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()
A
write( )
B
writelines( )
C
. writeline( )
D
None of the above
11
In order to read the content from the file, we can open file in ___________
A
“r†mode
B
“r+†mode
C
“w+†mode
D
All the above
12
Which of the following method is used to read a specified number of bytes of data from a data file.
A
read( )
B
read(n)
C
readlines( )
D
reading(n)
13
Write the output of the following: f=open("test.txt","w+") f.write("File-Handling") a=f.read(5) print(a)
A
File-
B
File
C
File-H
D
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)
A
andli
B
Handl
C
eHand
D
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)
A
File
B
Handling
C
FileHandling
D
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)
A
File
B
Handling
C
FileHandling
D
No Output
17
Which of the following method reads one complete line from a file?
A
read( )
B
read(n)
C
readline( )
D
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)
A
File Handling
B
FileHandling
C
File
D
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)
A
File Handling
B
FileHandling
C
Fi
D
No Output
20
Select the correct statement about the code given below: >>> myobj=open("myfile.txt", 'r') >>> print(myobj.readlines())
A
This code will read one line from the file.
B
This code will read all the lines from the file.
C
This code will read only last line from the file.
D
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))
A
<class ‘tuple’>
B
<class ‘list’>
C
<class ‘string’>
D
None of the above
22
Which of the following function return the data of the file in the form of list?
A
read( )
B
read(n)
C
readline( )
D
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
A
. Statement1
B
. Statement2
C
. Statemen3
D
. Statement4
24
__________________ function returns an integer that specifies the current position of the file object in the file.
A
seek( )
B
tell( )
C
disp( )
D
None of the above
25
_____ method is used to position the file object at a particular position in a file.
A
tell( )
B
seek( )
C
put( )
D
None of the above
26
In reference to the code given below, the file object will move _______ bytes. file_object.seek(10, 0)
A
0
B
10
C
5
D
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.
A
file_object.seek(5, 1)
B
file_object.seek(1, 5)
C
file_object.seek(5, 0)
D
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)
A
me i
B
. ame is
C
me is
D
Error
29
Write the output of the following: f=open("test.txt","r") print(f.tell())
A
1
B
2
C
-1
D
0
30
Write the output of the following: f=open("test.txt","r") print(f.tell(),end="6") f.seek(5) print(f.tell())
A
165
B
650
C
065
D
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()
A
3
B
4
C
5
D
6
32
________ method is used to write the objects in a binary file.
A
write( )
B
dump( )
C
load( )
D
writer( )
33
______ method is used to read data from a binary file.
A
a. write( )
B
dump( )
C
load( )
D
writer( )
34
Which module is to be imported for working in binary file?
A
unpickle
B
pickle
C
pickling
D
unpickling
35
Which of the following statement open the file “marker.txt†so that we can read existing content from file?
A
f = open(“marker.txtâ€, “râ€)
B
f = Open(“marker.txtâ€, “râ€)
C
f = open(“marker.txtâ€, “wâ€)
D
None of the above
36
Which of the following statement open the file “marker.txt†as a blank file?
A
f = open(“marker.txtâ€, “râ€)
B
f = open(“marker.txtâ€, “rbâ€)
C
f = open(“marker.txtâ€, “wâ€)
D
None of the above
37
Amit has written the following statement. He is working with ______ f = open("data", "rb")
A
Text File
B
CSV File
C
Binary File
D
None of the above
38
Which of the following option is correct?
A
if we try to write in 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, the file gets Created.
D
None of the above
39
Correct syntax of tell( ) function is _________. #f is file object
A
f.tell( )
B
tell(f)
C
f_tell( )
D
None of the above
40
The syntax of seek() is: file_object.seek(offset [, reference_point]). ___________________ value of reference_point indicate end of file
A
0
B
1
C
2
D
3
41
Which of the following statement is wrong in reference to Text file?
A
A text file is usually considered as a sequence of characters consisting of alphabets, numbers and other special symbols.
B
Each line of a text file is terminated by a special character.
C
Text files are not in Human readable form.
D
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+")
A
test.dat is a binary file
B
reading operation can be done on test.dat
C
appending operation can be done on test.dat
D
test.dat contains records about books
43
Which module is to be imported for CSV file?
A
pickle
B
unpickle
C
csv
D
pandas
44
Write the output of the following code : import csv f = open(“data.csvâ€, ‘r’) row = csv.reader(f) print(row)
A
It prints the memory address where csv.reader object is stored
B
It prints the first record of data.csv
C
It prints all the records of data.csv
D
None of the above
45
_______ function help us to read the csv file.
A
reader( )
B
read( )
C
read(n)
D
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
A
isAmit
B
sAmit
C
Amit
D
eisAmit
47
Which of the following method returns an integer value?
A
seek( )
B
read( )
C
tell( )
D
readline( )
48
________ refers to the process of converting the structure to a byte stream before writing it to the file.
A
pickling
B
unpickling
C
reading
D
writing
49
Fill in the blank in the given code : import pickle f = open("data.dat", "rb") l = pickle._______(f) print(l) f.close()
A
dump
B
load
C
write
D
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()))
A
13
B
14
C
15
D
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()))
A
4
B
5
C
6
D
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()))
A
2
B
3
C
4
D
5
53
import pickle f = open("data.dat", "wb") L = [1, 2, 3] pickle._______ f.close()
A
dump(f, L)
B
load(f, L)
C
dump(L, f)
D
load(L, f)
54
Which of the following statement will return attribute error?
A
print(len(f.readlines( ).split( ))) #f is file handle
B
print(len(f.readline( ).split( ))) #f is file handle
C
print(len(f.read( ).split( ))) #f is file handle
D
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
A
“data.txtâ€
B
data.txt
C
“dataâ€
D
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
A
import
B
create
C
data
D
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( )
A
r
B
rb+
C
wa
D
w
Submit Quiz
Back to All Quizzes