Python MCQ Quiz Hub

File Handling in Python section 2

Choose a topic to test your knowledge and improve your Python skills

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:





✅ Correct Answer: 1

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.





✅ Correct Answer: 2

Aman jotted down few features of the “write mode”. Help him to identify the valid features.





✅ Correct Answer: 3

Ananya jotted down few features of the “append mode”. Help her to identify the valid features.





✅ Correct Answer: 3

Which of the following methods can be used to write data in the file?





✅ Correct Answer: 3

Write the output of the following: >>> f = open("test.txt","w") >>> f.write("File #Handling")





✅ Correct Answer: 3

Which of the following error is returned by the given code: >>> f = open("test.txt","w") >>> f.write(345)





✅ Correct Answer: 1

Which of the following method is used to clear the buffer?





✅ Correct Answer: 3

Which of the following method does not return the number of characters written in the file.





✅ Correct Answer: 2

Fill in the blank in the given code: >>> fo = open("myfile.txt",'w') >>> lines = ["Hello ", "Writing strings ", "third line"] >>> fo._____________(lines) >>>myobject.close()





✅ Correct Answer: 2

In order to read the content from the file, we can open file in ___________





✅ Correct Answer: 4

Which of the following method is used to read a specified number of bytes of data from a data file.





✅ Correct Answer: 2

Write the output of the following: f=open("test.txt","w+") f.write("File-Handling") a=f.read(5) print(a)





✅ Correct Answer: 4

Write the output of the following: f=open("test.txt","w+") f.write("FileHandling") f.seek(5) a=f.read(5) print(a)





✅ Correct Answer: 1

Write the output of the following: f=open("test.txt","w+") f.write("FileHandling") f.seek(0) a=f.read() print(a)





✅ Correct Answer: 3

Write the output of the following: f=open("test.txt","w+") f.write("FileHandling") f.seek(0) a=f.read(-1) print(a)





✅ Correct Answer: 3

Which of the following method reads one complete line from a file?





✅ Correct Answer: 3

Write the output of the following: f=open("test.txt","w+") f.write("File Handling") f.seek(0) a=f.readline(-1) print(a)





✅ Correct Answer: 3

Write the output of the following: f=open("test.txt","w+") f.write("File Handling") f.seek(0) a=f.readline(2) print(a)





✅ Correct Answer: 3

Select the correct statement about the code given below: >>> myobj=open("myfile.txt", 'r') >>> print(myobj.readlines())





✅ Correct Answer: 2

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))





✅ Correct Answer: 2

Which of the following function return the data of the file in the form of list?





✅ Correct Answer: 4

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





✅ Correct Answer: 3

__________________ function returns an integer that specifies the current position of the file object in the file.





✅ Correct Answer: 2

_____ method is used to position the file object at a particular position in a file.





✅ Correct Answer: 2

In reference to the code given below, the file object will move _______ bytes. file_object.seek(10, 0)





✅ Correct Answer: 2

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.





✅ Correct Answer: 1

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)





✅ Correct Answer: 3

Write the output of the following: f=open("test.txt","r") print(f.tell())





✅ Correct Answer: 4

Write the output of the following: f=open("test.txt","r") print(f.tell(),end="6") f.seek(5) print(f.tell())





✅ Correct Answer: 3

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()





✅ Correct Answer: 3

________ method is used to write the objects in a binary file.





✅ Correct Answer: 2

______ method is used to read data from a binary file.





✅ Correct Answer: 3

Which module is to be imported for working in binary file?





✅ Correct Answer: 2

Which of the following statement open the file “marker.txt” so that we can read existing content from file?





✅ Correct Answer: 1

Which of the following statement open the file “marker.txt” as a blank file?





✅ Correct Answer: 3

Amit has written the following statement. He is working with ______ f = open("data", "rb")





✅ Correct Answer: 3

Which of the following option is correct?





✅ Correct Answer: 3

Correct syntax of tell( ) function is _________. #f is file object





✅ Correct Answer: 1

The syntax of seek() is: file_object.seek(offset [, reference_point]). ___________________ value of reference_point indicate end of file





✅ Correct Answer: 3

Which of the following statement is wrong in reference to Text file?





✅ Correct Answer: 3

Identify the statement which can not be interpret from the given code:f = open("test.dat", "ab+")





✅ Correct Answer: 4

Which module is to be imported for CSV file?





✅ Correct Answer: 3

Write the output of the following code : import csv f = open(“data.csv”, ‘r’) row = csv.reader(f) print(row)





✅ Correct Answer: 1

_______ function help us to read the csv file.





✅ Correct Answer: 1

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





✅ Correct Answer: 2

Which of the following method returns an integer value?





✅ Correct Answer: 3

________ refers to the process of converting the structure to a byte stream before writing it to the file.





✅ Correct Answer: 1

Fill in the blank in the given code : import pickle f = open("data.dat", "rb") l = pickle._______(f) print(l) f.close()





✅ Correct Answer: 2

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()))





✅ Correct Answer: 3

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()))





✅ Correct Answer: 4

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()))





✅ Correct Answer: 2

import pickle f = open("data.dat", "wb") L = [1, 2, 3] pickle._______ f.close()





✅ Correct Answer: 3

Which of the following statement will return attribute error?





✅ Correct Answer: 1

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





✅ Correct Answer: 2

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





✅ Correct Answer: 1

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( )





✅ Correct Answer: 4