Python/File Handling in Python section 2 Sample Test,Sample questions

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

1. If we open an existing file in append mode, the previous data will remain there.

2. In append mode the file object will be positioned at the end of the file.

3.In append mode, if the file does not exist then the new file will be created.

4.All the above


Question:
 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

1.import

2.create

3.data

4.Import


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

1. f.tell( )

2.tell(f)

3. f_tell( )

4.None of the above


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

1.write( )

2. writelines( )

3.. writeline( )

4.None of the above


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

1.3

2.4

3.5

4.6


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

1.test.dat is a binary file

2.reading operation can be done on test.dat

3.appending operation can be done on test.dat

4.test.dat contains records about books


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

1.2

2.3

3.4

4.5


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

1.4

2.5

3.6

4.7


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

1.andli

2.Handl

3.eHand

4.No Output


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

1. If we open an already existing file in write mode, the previous data will be erased

2.In write mode, the file object will be positioned at the beginning of the file.

3.In write mode, if the file does not exist then the new file will be created.

4.All the above


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

1.Text File

2.CSV File

3.Binary File

4.None of the above


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

1.r

2.rb+

3.wa

4.w


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

1.dump

2.load

3.write

4.list


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

1.dump(f, L)

2.load(f, L)

3.dump(L, f)

4.load(L, f)


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

1.“r” mode

2.“r+” mode

3.“w+” mode

4.All the above


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

1.0

2.10

3.5

4.4


Question:
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.

1. read mode

2.append mode

3. write mode

4.All the above


Question:
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

1.“data.txt”

2. data.txt

3.“data”

4.data


Question:
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.

1.file_object.seek(5, 1)

2.file_object.seek(1, 5)

3. file_object.seek(5, 0)

4.file_object.seek(5, 2)


Question:
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:

1.In case the user forgets to close the file explicitly the file will closed automatically.

2. file handle will always be present in the beginning of the file even in append mode.

3. File will be processed faster

4.None of the above


Question:
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

1.. Statement1

2.. Statement2

3.. Statemen3

4.. Statement4


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

1.This code will read one line from the file.

2. This code will read all the lines from the file.

3.This code will read only last line from the file.

4.None of the above


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

1.0

2.1

3.2

4.3


Question:
Which module is to be imported for CSV file?

1. pickle

2.unpickle

3.csv

4.pandas


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

1.unpickle

2.pickle

3.pickling

4.unpickling


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

1. Syntax Error

2.TypeError

3. StringError

4.Run Time Error


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

1.read( )

2. read(n)

3.readline( )

4.readlines( )


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

1.write( )

2.writelines( )

3.Both of the above

4.None of the above


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

1. clear( )

2.buffer( )

3. flush( )

4.clean( )


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

1.read( )

2.read(n)

3.readlines( )

4. reading(n)


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

1.read( )

2.read(n)

3. readline( )

4.readlines( )


Question:
Which of the following method returns an integer value?

1. seek( )

2. read( )

3. tell( )

4.readline( )


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

1.write( )

2.writelines( )

3.Both of the above

4.None of the above


Question:
Which of the following option is correct?

1.if we try to write in a text file that does not exist, an error occurs

2.if we try to read a text file that does not exist, the file gets created.

3.if we try to write on a text file that does not exist, the file gets Created.

4.None of the above


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

1.A text file is usually considered as a sequence of characters consisting of alphabets, numbers and other special symbols.

2. Each line of a text file is terminated by a special character.

3. Text files are not in Human readable form.

4. Text files can be opened in Text editor.


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

1.f = open(“marker.txt”, “r”)

2. f = open(“marker.txt”, “rb”)

3.f = open(“marker.txt”, “w”)

4.None of the above


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

1. f = open(“marker.txt”, “r”)

2. f = Open(“marker.txt”, “r”)

3. f = open(“marker.txt”, “w”)

4.None of the above


Question:
Which of the following statement will return attribute error?

1.print(len(f.readlines( ).split( ))) #f is file handle

2.print(len(f.readline( ).split( ))) #f is file handle

3.print(len(f.read( ).split( ))) #f is file handle

4.None of the above


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

1.It prints the memory address where csv.reader object is stored

2. It prints the first record of data.csv

3. It prints all the records of data.csv

4.None of the above


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

1.13

2.14

3.15

4.16


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

1.me i

2.. ame is

3.me is

4.Error


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

1.17

2.16

3.14

4.15


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

1.1

2.2

3.-1

4.0


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

1.165

2.650

3.065

4.506


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

1. File Handling

2. FileHandling

3.File

4.No Output


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

1.File Handling

2. FileHandling

3.Fi

4.No Output


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

1.File-

2.File

3.File-H

4. No Output


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

1.File

2.Handling

3.FileHandling

4.No Output


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

1.File

2.Handling

3. FileHandling

4.No Output


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

1.<class ‘tuple’>

2.<class ‘list’>

3.<class ‘string’>

4.None of the above


Question:
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

1. isAmit

2.sAmit

3.Amit

4. eisAmit


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

1.tell( )

2.seek( )

3.put( )

4.None of the above


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

1.a. write( )

2.dump( )

3.load( )

4. writer( )


Question:
_______ function help us to read the csv file.

1.reader( )

2.read( )

3.read(n)

4.readline( )


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

1. write( )

2.dump( )

3. load( )

4.writer( )


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

1. pickling

2.unpickling

3.reading

4.writing


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

1.seek( )

2.tell( )

3.disp( )

4.None of the above


More MCQS

  1. Python MCQS - Function
  2. Python MCQS - GUI in python
  3. Python MCQS - Operators
  4. Python MCQS - Data type in python
  5. Python MCQS - loops in python
  6. Python MCQS - Numpy
  7. Python MCQS - sqlite3
  8. Python MCQS - Library
  9. Python MCQS - Pandas
  10. Python MCQs
  11. Dictionary Python MCQ set 1
  12. Dictionary Python MCQ set 2
  13. MCQ For Python Fundamentals
  14. MCQ Introduction to Python Section 1
  15. MCQ Introduction to Python Section 2
  16. MCQ Introduction to Python Section 3
  17. MCQ on Flow of Control in Python Set 1
  18. MCQ on Flow of Control in Python Set 2
  19. MCQ on Python String Set 1
  20. File Handling in Python section 1
  21. File Handling in Python section 2
  22. Python Functions MCQS Set 1
  23. Python Functions MCQS Set 2
  24. MCQ on List in Python
  25. Pandas MCQ Questions Set 1
  26. Pandas MCQ Questions Set 2
  27. Tuple MCQ in Python
  28. Python dataframe MCQ
  29. Python Mcq Set 1
  30. Python Mcq Set 2
  31. Python Mcq Set 3
  32. Python Mcq Set 4
  33. Python Mcq Set 5
  34. Python Mcq Set 6
  35. Python Mcq Set 7
  36. Python Mcq Set 8
  37. Python Mcq Set 9
  38. Python Mcq Set 10
  39. Python Mcq Set 11
  40. Python Mcq Set 12
  41. Python Mcq Set 13
  42. Python Mcq Set 14
  43. Python Mcq Set 15
  44. Python Mcq Set 16
  45. Python Mcq Set 17
  46. Python Mcq Set 18
  47. Python Mcq Set 19
  48. Python Mcq Set 20
  49. Python Mcq Set 21
  50. Python MCQ
Search
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!