Review each question and reveal answers to strengthen your understanding
1Rohan 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
2Mohan 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
3Aman jotted down few features of the “write modeâ€. Help him to identify the valid features.
✅ Correct Answer: 3
4 Ananya jotted down few features of the “append modeâ€. Help her to identify the valid features.
✅ Correct Answer: 3
5Which of the following methods can be used to write data in the file?
✅ Correct Answer: 3
6Write the output of the following:
>>> f = open("test.txt","w")
>>> f.write("File
#Handling")
✅ Correct Answer: 3
7Which of the following error is returned by the given code:
>>> f = open("test.txt","w")
>>> f.write(345)
✅ Correct Answer: 1
8Which of the following method is used to clear the buffer?
✅ Correct Answer: 3
9Which of the following method does not return the number of characters written in the file.
✅ Correct Answer: 2
10 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
11In order to read the content from the file, we can open file in ___________
✅ Correct Answer: 4
12Which of the following method is used to read a specified number of bytes of data from a data file.
✅ Correct Answer: 2
13Write the output of the following:
f=open("test.txt","w+")
f.write("File-Handling")
a=f.read(5)
print(a)
✅ Correct Answer: 4
14 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
15Write the output of the following:
f=open("test.txt","w+")
f.write("FileHandling")
f.seek(0)
a=f.read()
print(a)
✅ Correct Answer: 3
16Write 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
17Which of the following method reads one complete line from a file?
✅ Correct Answer: 3
18Write 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
19Write 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
20Select the correct statement about the code given below:
>>> myobj=open("myfile.txt", 'r')
>>> print(myobj.readlines())
✅ Correct Answer: 2
21Write 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
22Which of the following function return the data of the file in the form of list?
✅ Correct Answer: 4
23Sanjeev 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
24__________________ function returns an integer that specifies the current position of the file object in the file.
✅ Correct Answer: 2
25_____ method is used to position the file object at a particular position in a file.
✅ Correct Answer: 2
26In reference to the code given below, the file object will move _______ bytes.
file_object.seek(10, 0)
✅ Correct Answer: 2
27Ravi 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
28Write 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
29Write the output of the following:
f=open("test.txt","r")
print(f.tell())
✅ Correct Answer: 4
30Write the output of the following:
f=open("test.txt","r")
print(f.tell(),end="6")
f.seek(5)
print(f.tell())
✅ Correct Answer: 3
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()
✅ Correct Answer: 3
32________ method is used to write the objects in a binary file.
✅ Correct Answer: 2
33______ method is used to read data from a binary file.
✅ Correct Answer: 3
34Which module is to be imported for working in binary file?
✅ Correct Answer: 2
35Which of the following statement open the file “marker.txt†so that we can read existing content from file?
✅ Correct Answer: 1
36Which of the following statement open the file “marker.txt†as a blank file?
✅ Correct Answer: 3
37Amit has written the following statement. He is working with ______
f = open("data", "rb")
✅ Correct Answer: 3
38Which of the following option is correct?
✅ Correct Answer: 3
39 Correct syntax of tell( ) function is _________. #f is file object
✅ Correct Answer: 1
40The syntax of seek() is: file_object.seek(offset [, reference_point]). ___________________ value of reference_point indicate end of file
✅ Correct Answer: 3
41Which of the following statement is wrong in reference to Text file?
✅ Correct Answer: 3
42 Identify the statement which can not be interpret from the given code:f = open("test.dat", "ab+")
✅ Correct Answer: 4
43Which module is to be imported for CSV file?
✅ Correct Answer: 3
44Write the output of the following code :
import csv
f = open(“data.csvâ€, ‘r’)
row = csv.reader(f)
print(row)
✅ Correct Answer: 1
45_______ function help us to read the csv file.
✅ Correct Answer: 1
46Write 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
47Which of the following method returns an integer value?
✅ Correct Answer: 3
48________ refers to the process of converting the structure to a byte stream before writing it to the file.
✅ Correct Answer: 1
49Fill in the blank in the given code :
import pickle
f = open("data.dat", "rb")
l = pickle._______(f)
print(l)
f.close()
✅ Correct Answer: 2
50Write 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
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()))
✅ Correct Answer: 4
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()))
✅ Correct Answer: 2
53import pickle
f = open("data.dat", "wb")
L = [1, 2, 3]
pickle._______
f.close()
✅ Correct Answer: 3
54Which of the following statement will return attribute error?
✅ Correct Answer: 1
55Ravi 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
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
✅ Correct Answer: 1
57Chinki 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( )