CBSE CS

cbse cs logo

Home

Files in Python

MCQ on File Handling


Question 1. Which statement will read 5 characters from a file(file object 'f')?

Option a.) f.read()
Option b.) f.read(5)
Option c.) f.reads(5)
Option d.) None of the above
Answer

#Ans. b. f.read(5)




Question 2. Which function open file in python?

Option a.) open( )
Option b.) new( )
Option c.) Open( )
Option d.) None of the above

Answer

#Ans. a. open( )




Question 3. Processing of Text file is faster than binary files.(T/F)

Option a.) True
Option b.) False

Answer

#Ans. b. False




Question 4. Which mode create new file if the file does not exist?

Option a.) write mode
Option b.) append mode
Option c.) Both of the above
Option d.) None of the above

Answer

#Ans. c. Both of the above




Question 5. Which statement will return one line from a file (file object is 'f')?

Option a.) f.readline( )
Option b.) f.readlines( )
Option c.) f.read( )
Option d.) f.line( )

Answer

#Ans. a. f.readline( )





Question 6. readlines() method return _________

Option a.) String
Option b.) List
Option c.) Dictionary
Option d.) Tuple

Answer

#Ans. b. List




Question 7. EOF stands for _________________

Option a.) End of File
Option b.) End off File
Option c.) End on File
Option d.) End or File

Answer

#Ans. a. End of File




Question 8. Which function is used to read data from Text File?

Option a.) read( )
Option b.) writelines( )
Option c.) pickle( )
Option d.) dump( )

Answer

#Ans. a. read( )




Question 9. Which of the following will read entire content of file(file object 'f')?

Option a.) f.reads( )
Option b.) f.read( )
Option c.) f.read(all)
Option d.) f.read( * )

Answer

#Ans. b. f.read( )




Question 10. Which symbol is used for append mode?

Option a.) ap
Option b.) a
Option c.) w
Option d.) app

Answer

#Ans. b. a




Question 11. Which of the following options can be used to read the first line of a text file data.txt?

Option a.) f = open('data.txt'); f.read()
Option b.) f = open('data.txt','r'); f.read(n)
Option c.) myfile = open('data.txt'); f.readline()
Option d.) f = open('data.txt'); f.readlines()

Answer

#Ans. c. myfile = open('data.txt'); f.readline()




Question 12. File in python is treated as sequence of ________________

Option a.) Bytes
Option b.) Bites
Option c.) bits
Option d.) None of the above

Answer

#Ans. a. Bytes




Question 13. Which function is used to write data in binary mode?

Option a.) write
Option b.) writelines
Option c.) pickle
Option d.) dump

Answer

#Ans. d. dump




Question 14. Which function is used to force transfer of data from buffer to file?

Option a.) flush( )
Option b.) save( )
Option c.) move( )
Option d.) None of the above

Answer

#Ans. a. flush( )




Question 15. Let the file pointer is at the end of 3rd line in a text file named "data.txt". Which of the following option can be used to read all the remaining lines?

Option a.) f.read( )
Option b.) f.read(all)
Option c.) f.readline( )
Option d.) f.readlines( )

Answer

#Ans. d. f.readlines( )




Question 16. ____________________ module is used for serializing and de-serializing any Python object structure.

Option a.) pickle
Option b.) unpickle
Option c.) pandas
Option d.) math

Answer

#Ans. a. pickle




Question 17. Which of the following error is returned when we try to open a file in write mode which does not exist?

Option a.) FileNotFoundError
Option b.) FileFoundError
Option c.) FileNotExistError
Option d.) None of the above

Answer

#Ans. d. None of the above r




Question 18. ______________ function returns the strings.

Option a.) read( )
Option b.) readline( )
Option c.) Both of the above
Option d.) None of the above

Answer

#Ans. c. Both of the above




Question 19. The syntax of seek() is: file_object.seek(offset [, reference_point])
What is reference_point indicate?

Option a.) reference_point indicates the starting position of the file object
Option b.) reference_point indicates the ending position of the file object
Option c.) reference_point indicates the current position of the file object
Option d.) None of the above.

Answer

#Ans. a. reference_point indicates the starting position of the file object




Question 20. Identify the invalid mode from the following.

Option a.) a
Option b.) r+
Option c.) ar+
Option d.) w

Answer

#Ans. c. ar+




Question 21. Which of the following is an invalid mode of file opening?

Option a.) read only mode
Option b.) write only mode
Option c.) read and write mode
Option d.) write and append mode

Answer

#Ans. d. write and append mode




Question 22. readlines( ) function returns all the words of the file in the form of List. (T/F)

Option a.) True
Option b.) False

Answer

#Ans. b. False




Question 23. What is 'f' in the following statement?
f=open("Data.txt" , "r")

Option a.) File Name
Option b.) File Handle
Option c.) Mode of file
Option d.) File Handling

Answer

#Ans. b. File Handle




Question 24. What is full form of CSV

Option a.) Comma Separation Value
Option b.) Comma Separated Value
Option c.) Common Syntax Value
Option d.) Comma Separated Variable

Answer

#Ans. b. Comma Separated Value




Question 25. Which statement will open file "data.txt" in append mode?

Option a.) f = open("data.txt" , "a")
Option b.) f = Open("data.txt" , "ab")
Option c.) f = new("data.txt" , "a")
Option d.) open("data.txt" , "a")

Answer

#Ans. a. f = open("data.txt" , "a")




Question 26. Fill in the blank


import pickle
f=open("data.dat",'rb')
d=_____________________.load(f)
f.close()

Option a.) unpickle
Option b.) pickling
Option c.) pickle
Option d.) pick

Answer

#Ans. c. pickle




Question 27. Which module to be imported to make the following line functional?
sys.stdout.write("ABC")

Option a.) system
Option b.) sys
Option c.) stdout
Option d.) stdin

Answer

#Ans. b. sys




Question 28. What error is returned by the following statement if the file does not exist?
f=open("A.txt")

Option a.) FileNotFoundError
Option b.) NotFoundError
Option c.) FileNotFound
Option d.) FoundError

Answer

#Ans. a. FileNotFoundError




Question 29. Which statement will return error?
import pickle
f=open("data.dat",'rb')
d=pickle.load(f)
f.end()

Option a.) Statement 1
Option b.) Statement 2
Option c.) Statement 3
Option d.) Statement 4

Answer

#Ans. d. Statement 4




Question 30. Which of the following function takes two arguments?

Option a.) load( )
Option b.) dump( )
Option c.) both of the above
Option d.) none of the above

Answer

#Ans. b. dump( )




Question 31. Almost all the files in our computer stored as _______ File.

Option a.) Text
Option b.) Binary
Option c.) CSV
Option d.) None of the above

Answer

#Ans. b. Binary




Question 32. Binary files are not in human readable format.(T/F)

Option a.) True
Option b.) False

Answer

#Ans. a. True




Question 33. .pdf and .doc are examples of __________ files.

Option a.) Text
Option b.) Binary
Option c.) CSV
Option d.) None of the above

Answer

#Ans. b. Binary




Question 34. The syntax of seek() is:file_object.seek(offset [, reference_point]) What all values can be given as a reference point?

Option a.) 1
Option b.) 2
Option c.) 0
Option d.) All of the above

Answer

#Ans. d. All of the above




Question 35. There is no delimiter to end a line in binary files.(T/F)

Option a.) True
Option b.) False

Answer

#Ans. a. True




Question 36. seek( ) method is used for random access to the file.(T/F)

Option a.) True
Option b.) False

Answer

#Ans. a. True




Question 37. Fill in the blanks in the following code of writing data in binary files. Choose the answer for statement 1
import ___________ # Statement 1
rec = [ ]
while True:
rn = int(input("Enter"))
nm = input("Enter")
temp = [rn, nm]
rec.append(temp)
ch = input("Enter choice (Y/N)")
if ch.upper == "N":
break
f = open("stud.dat", "____________") #statement 2
__________ .dump(rec, f) #statement 3
_______.close( ) # statement 4


Option a.) csv
Option b.) unpickle
Option c.) pickle
Option d.) load

Answer

#Ans. c. pickle




Question 38. Refer to the above code and choose the option for statement2.

Option a.) w
Option b.) w+
Option c.) wb
Option d.) write

Answer

#Ans. c. wb




Question 39. Refer to the above code (Q 38)and choose the option for statement 3

Option a.) unpickle
Option b.) write
Option c.) pickle
Option d.) None of the above

Answer

#Ans. c. pickle




Question 40. Refer to the above code (Q 38)and choose the option for statement 4.

Option a.) f
Option b.) rec
Option c.) file
Option d.) stud

Answer

#Ans. a. f





Question 41. The syntax of seek() is:file_object.seek(offset [, reference_point] What is the default value of reference_point

Option a.) 0
Option b.) 1
Option c.) 2
Option d.) 3

Answer

#Ans. a. 0




Question 42. _______ function returns the current position of file pointer.

Option a.) get( )
Option b.) tell( )
Option c.) cur( )
Option d.) seek( )

Answer

#Ans. b. tell( )




Question 43. f.seek(10,0) will move 10 bytes forward from beginning of file.(T/F)

Option a.) True
Option b.) False

Answer

#Ans. a. True




Question 44. Which statement will move file pointer 10 bytes backward from current position.

Option a.) f.seek(-10, 0)
Option b.) f.seek(10, 0)
Option c.) f.seek(-10, 1)
Option d.) None of the above

Answer

#Ans. c. f.seek(-10, 1)




Question 45. When we open file in append mode the file pointer is at the _________ of the file.

Option a.) end
Option b.) beginning
Option c.) anywhere in between the file
Option d.) second line of the file

Answer

#Ans. a. end




Question 46. When we open file in write mode the file pointer is at the _______ of the file.

Option a.) end
Option b.) beginning
Option c.) anywhere in between the file
Option d.) second line of the file

Answer

#Ans. b. beginning




Question 47. Write the output of the First Print statements :
f=open("data.txt",'w')
f.write("Hello")
f.write("Welcome to my Blog")
f.close()
f=open("data.txt",'r')
d=f.read(5)
print(d) # First Print Statement
f.seek(10)
d=f.read(3)
print(d) # Second Print Statement
f.seek(13)
d=f.read(5)
print(d) # Third Print Statement
d=f.tell()
print(d) # Fourth Print Statement

Option a.) Hello
Option b.) Hell
Option c.) ello
Option d.) None of the above

Answer

#Ans. a. Hello




Question 48. Refer to the above code (Q 47) : Write the output of Second Print Statement

Option a.) om
Option b.) me
Option c.) co
Option d.) None of the above

Answer

#Ans. b. me




Question 49. Refer to the above code (Q 47) : Write the output of Third Print Statement

Option a.) e to m
Option b.) e to my
Option c.) to my
Option d.) None of the above

Answer

#Ans. c. to my




Question 50. Refer to the above code (Q 47) : Write the output of Fourth Print Statement

Option a.) 17
Option b.) 16
Option c.) 19
Option d.) 18

Answer

#Ans. d. 18




Question 51. A _____________ is a named location on a secondary storage media where data are permanently stored for later access.

Option a.) file
Option b.) variable
Option c.) comment
Option d.) token

Answer

#Ans. a. file




Question 52. A _____ file consists of human readable characters.

Option a.) Binary
Option b.) Text
Option c.) Both of the above
Option d.) None of the above

Answer

#Ans. b. Text




Question 53. Which of the following file require specific programs to access its contents?

Option a.) Binary
Option b.) Text
Option c.) CSV
Option d.) None of the above

Answer

#Ans. a. Binary




Question 54. Which of the following file can be opened in any text editor?

Option a.) Binary
Option b.) Text
Option c.) Both of the above
Option d.) None of the above

Answer

#Ans. b. Text




Question 55. Each line of a text file is terminated by a special character, called the ________

Option a.) End of File
Option b.) End of Line
Option c.) End of Statement
Option d.) End of program

Answer

#Ans. b. End of Line




Question 56. Default EOL character in text file is ________

Option a.) \n
Option b.) \N
Option c.) \t
Option d.) \l

Answer

#Ans. a. \n




Question 57. Which of the following file can be created in python?

Option a.) Text File
Option b.) Binary File
Option c.) CSV File
Option d.) All of the above

Answer

#Ans. d. All of the above




Question 58. In which of the following data store permanently?

Option a.) File
Option b.) Variable
Option c.) Both of the above
Option d.) None of the above

Answer

#Ans. a. File




Question 59. open( ) function takes ____ as parameter.

Option a.) File name
Option b.) Access mode
Option c.) Both of the above
Option d.) None of the above

Answer

#Ans. c. Both of the above




Question 60. Identify the correct statement to open a file:

Option a.) f = open("D:\\myfolder\\naman.txt")
Option b.) f = open("D:\myfolder\naman.txt")
Option c.) f = open("D:myfolder#naman.txt")
Option d.) f = Open("D:\myfolder\naman.txt")

Answer

#Ans. a. f = open("D:\\myfolder\\naman.txt")




Question 61. Which of the following are the attributes of file handle?

Option a.) closed
Option b.) name
Option c.) mode
Option d.) All of the above

Answer

#Ans. d. All of the above




Question 62. Write the output of the following:
>>> f = open("test.txt","w")
>>> f.closed

Option a.) True
Option b.) False
Option c.) Yes
Option d.) No

Answer

#Ans. b. False




Question 63. Write the output of the following:
>>> f = open("test.txt","w")
>>> f.mode

Option a.) 'w'
Option b.) 'r'
Option c.) 'a'
Option d.) 'w+'

Answer

#Ans. a. 'w'




Question 64. Write the output of the following:
>>> f = open("test.txt","w")
>>> f.name

Option a.) 'test'
Option b.) 'test.txt'
Option c.) 'test.dat'
Option d.) file does not exist

Answer

#Ans. b. 'test.txt'




Question 65. Write the output of the following:
>>> f = open("test.txt","w")
>>> f.close()
>>> f.closed

Option a.) True
Option b.) False
Option c.) Yes
Option d.) No

Answer

#Ans. a. True





Question 66. Which of the following attribute of file handle returns Boolean value?

Option a.) name
Option b.) closed
Option c.) mode
Option d.) None of the above

Answer

#Ans. b. closed




Question 67. Ravi opened a file in python using open( ) function but forgot to specify the mode. In which mode the file will open?

Option a.) write
Option b.) append
Option c.) read
Option d.) read and write both

Answer

#Ans. c. read




Question 68. Which of the following is invalid mode of opening file?

Option a.) r
Option b.) rb
Option c.) +r
Option d.) None of the above

Answer

#Ans. d. None of the above




Question 69. Which of the following mode will create a new file, if the file does not exist?

Option a.) 'a'
Option b.) 'a+'
Option c.) '+a'
Option d.) All of the above

Answer

#Ans. d. All of the above




Question 70. Which of the following mode will open the file in binary and read-only mode.

Option a.) 'r'
Option b.) 'rb'
Option c.) 'r+'
Option d.) 'rb+'

Answer

#Ans. b. 'rb'




Question 71. Which of the following mode will opens the file in read, write and binary mode?

Option a.) 'wb+
Option b.) '+wb'
Option c.) Both of the above
Option d.) None of the above

Answer

#Ans. c. Both of the above




Question 72. In the given statement, the file myfile.txt will open in _______________ mode.
myObject=open("myfile.txt", "a+")

Option a.) append and read
Option b.) append and write
Option c.) append and read and binary
Option d.) All of the above

Answer

#Ans. a. append and read




Question 73. Ravi opened the file myfile.txt in append mode. In this file the file object/file handle will be at the __________

Option a.) beginning of the file
Option b.) end of the file
Option c.) second line of the file
Option d.) the end of the first line

Answer

#Ans. b. end of the file




Question 74. Ravi opened the file myfile.txt in write mode. In this file the file object/file handle will be at the ______________

Option a.) beginning of the file
Option b.) end of the file
Option c.) second line of the file
Option d.) the end of the first line

Answer

#Ans. a. beginning of the file




Question 75. Ravi opened the file myfile.txt in read mode. In this file the file object/file handle will be at the _______________

Option a.) beginning of the file
Option b.) end of the file
Option c.) second line of the file
Option d.) the end of the first line

Answer

#Ans. a. beginning of the file




Question 76. Ravi opened a file in a certain mode. After opening the file, he forgot the mode. One interesting fact about that mode is " If the file already exists, all the contents will be overwritten". Help him to identify the correct mode.

Option a.) read mode
Option b.) write mode
Option c.) append mode
Option d.) binary and read mode

Answer

#Ans. b. write mode




Question 77. Ram opened a file in a certain mode. After opening the file, he forgot the mode. The interesting facts about that mode are " If the file doesn't exist, then a new file will be created" and "After opening file in that mode the file handle will be at the end of the file" Help him to identify the correct mode.

Option a.) read mode
Option b.) write mode
Option c.) append mode
Option d.) binary and read mode

Answer

#Ans. c. append mode




Question 78. Which of the following function is used to close the file?

Option a.) close( )
Option b.) end( )
Option c.) quit( )
Option d.) exit( )

Answer

#Ans. a. close( )




Question 79. open( ) function returns a file object called ______________

Option a.) object handle
Option b.) file handle
Option c.) read handle
Option d.) write handle

Answer

#Ans. b. file handle




Question 80. Which of the following is the valid way to open the file?

Option a.) with open (file_name, access_mode) as fo:
Option b.) fo = open (file_name, access_mode)
Option c.) Both of the above
Option d.) None of the above

Answer

#Ans. c. Both of the above




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

Option a.) In case the user forgets to close the file explicitly the file will closed automatically.
Option b.) file handle will always be present in the beginning of the file even in append mode.
Option c.) File will be processed faster
Option d.) None of the above

Answer

#Ans. a. In case the user forgets to close the file explicitly the file will closed automatically.




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

Option a.) read mode
Option b.) append mode
Option c.) write mode
Option d.) All of the above

Answer

#Ans. b. append mode




Question 83. Aman jotted down few features of the "write mode". Help him to identify the valid features.

Option a.) If we open an already existing file in write mode, the previous data will be erased
Option b.) In write mode, the file object will be positioned at the beginning of the file.
Option c.) In write mode, if the file does not exist then the new file will be created.
Option d.) All of the above

Answer

#Ans. d. All of the above




Question 84. Ananya jotted down few features of the "append mode". Help her to identify the valid features.

Option a.) If we open an existing file in append mode, the previous data will remain there.
Option b.) In append mode the file object will be positioned at the end of the file.
Option c.) In append mode, if the file does not exist then the new file will be created.
Option d.) All of the above

Answer

#Ans. d. All of the above




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

Option a.) write( )
Option b.) writelines( )
Option c.) Both of the above
Option d.) None of the above

Answer

#Ans. c. Both of the above




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

Option a.) 17
Option b.) 16
Option c.) 14
Option d.) 15

Answer

#Ans. c. 14




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

Option a.) Syntax Error
Option b.) TypeError
Option c.) StringError
Option d.) Run Time Error

Answer

#Ans. b. TypeError




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

Option a.) clear( )
Option b.) buffer( )
Option c.) flush( )
Option d.) clean( )

Answer

#Ans. c. flush( )




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

Option a.) write( )
Option b.) writelines( )
Option c.) Both of the above
Option d.) None of the above

Answer

#Ans. b. writelines( )




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

Option a.) write( )
Option b.) writelines( )
Option c.) writeline( )
Option d.) None of the above

Answer

#Ans. b. writelines( )




Question 91. In order to read the content from the file, we can open file in ___________________

Option a.) "r" mode
Option b.) "r+" mode
Option c.) "w+" mode
Option d.) All of the above

Answer

#Ans. d. All of the above




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

Option a.) read( )
Option b.) read(n)
Option c.) readlines( )
Option d.) reading(n)

Answer

#Ans. b. read(n)




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

Option a.) File-
Option b.) File
Option c.) File-H
Option d.) No Output

Answer

#Ans. d. No Output




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

Option a.) File-
Option b.) File
Option c.) File-H
Option d.) No Output

Answer

#Ans. a. File-




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

Option a.) andli
Option b.) Handl
Option c.) eHand
Option d.) No Output

Answer

#Ans. a. andli




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

Option a.) File
Option b.) Handling
Option c.) FileHandling
Option d.) No Output

Answer

#Ans. c. FileHandling




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

Option a.) File
Option b.) Handling
Option c.) FileHandling
Option d.) No Output

Answer

#Ans. c. FileHandling




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

Option a.) read( )
Option b.) read(n)
Option c.) readline( )
Option d.) readlines( )

Answer

#Ans. c. readline( )




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

Option a.) File Handling
Option b.) FileHandling
Option c.) File
Option d.) No Output

Answer

#Ans. c. File




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

Option a.) File\nHandling
Option b.) FileHandling
Option c.) Fi
Option d.) No Output

Answer

#Ans. c. Fi




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

Option a.) This code will read one line from the file.
Option b.) This code will read all the lines from the file.
Option c.) This code will read only last line from the file.
Option d.) None of the above

Answer

#Ans. b. This code will read all the lines from the file.




Question 102. Write the output of the following:
f=open("test.txt","w+")
L = ["My name\n", "is\n", "Amit"]
f.writelines(L)
f.seek(0)
a=f.readlines()
print(type(a))

Option a.)
Option b.)
Option c.)
Option d.) None of the above

Answer

#Ans. b.




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

Option a.) read( )
Option b.) read(n)
Option c.) readline( )
Option d.) readlines( )

Answer

#Ans. d. readlines( )




Question 104. 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\n", "is\n", "Amit"] #Statement2
f.writeline(L) #Statement3
f.close() #Statement4

Option a.) Statement1
Option b.) Statement2
Option c.) Statement3
Option d.) Statement4

Answer

#Ans. c. Statement3




Question 105. ____________________ function returns an integer that specifies the current position of the file object in the file.

Option a.) seek( )
Option b.) tell( )
Option c.) disp( )
Option d.) None of the above

Answer

#Ans. b. tell( )





Question 106. _______ method is used to position the file object at a particular position in a file.

Option a.) tell( )
Option b.) seek( )
Option c.) put( )
Option d.) None of the above

Answer

#Ans. b. seek( )




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

Option a.) 0
Option b.) 10
Option c.) 5
Option d.) 4

Answer

#Ans. b. 10




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

Option a.) file_object.seek(5, 1)
Option b.) file_object.seek(1, 5)
Option c.) file_object.seek(5, 0)
Option d.) file_object.seek(5, 2)

Answer

#Ans. a. file_object.seek(5, 1)




Question 109. Write the output of the following code:
f=open("test.txt","w+")
f.write("My name is Amit\n")
f.seek(5,0)
a=f.read(5)
print(a)

Option a.) me i
Option b.) ame is
Option c.) me is
Option d.) Error

Answer

#Ans. c. me is




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

Option a.) 1
Option b.) 2
Option c.) -1
Option d.) 0

Answer

#Ans. d. 0




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

Option a.) 165
Option b.) 650
Option c.) 065
Option d.) 506

Answer

#Ans. c. 065




Question 113. 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()

Option a.) 3
Option b.) 4
Option c.) 5
Option d.) 6

Answer

#Ans. c. 5




Question 114. _________ method is used to write the objects in a binary file.

Option a.) write( )
Option b.) dump( )
Option c.) load( )
Option d.) writer( )

Answer

#Ans. b. dump( )




Question 115. _________ method is used to read data from a binary file.

Option a.) write( )
Option b.) dump( )
Option c.) load( )
Option d.) writer( )

Answer

#Ans. c. load( )




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

Option a.) unpickle
Option b.) pickle
Option c.) pickling
Option d.) unpickling

Answer

#Ans. b. pickle




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

Option a.) f = open("marker.txt", "r")
Option b.) f = Open("marker.txt", "r")
Option c.) f = open("marker.txt", "w")
Option d.) None of the above

Answer

#Ans. a. f = open("marker.txt", "r")




Question 118. Which of the following statement open the file "marker.txt" as a blank file?

Option a.) f = open("marker.txt", "r")
Option b.) f = open("marker.txt", "rb")
Option c.) f = open("marker.txt", "w")
Option d.) None of the above

Answer

#Ans. c. f = open("marker.txt", "w")




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

Option a.) Text File
Option b.) CSV File
Option c.) Binary File
Option d.) None of the above

Answer

#Ans. c. Binary File




Question 120. Which of the following option is correct?

Option a.) if we try to write in a text file that does not exist, an error occurs
Option b.) if we try to read a text file that does not exist, the file gets created.
Option c.) if we try to write on a text file that does not exist, the file gets Created.
Option d.) None of the value

Answer

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




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

Option a.) f.tell( )
Option b.) tell(f)
Option c.) f_tell( )
Option d.) None of the above

Answer

#Ans. a. f.tell( )




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

Option a.) 0
Option b.) 1
Option c.) 2
Option d.) 3

Answer

#Ans. c. 2




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

Option a.) A text file is usually considered as a sequence of characters consisting of alphabets, numbers and other special symbols.
Option b.) Each line of a text file is terminated by a special character.
Option c.) Text files are not in Human readable form.
Option d.) Text files can be opened in Text editor.

Answer

#Ans. c. Text files are not in Human readable form.




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

Option a.) test.dat is a binary file
Option b.) reading operation can be done on test.dat
Option c.) appending operation can be done on test.dat
Option d.) test.dat contains records about books

Answer

#Ans. d. test.dat contains records about books




Question 125. Which module is to be imported for CSV file?

Option a.) pickle
Option b.) unpickle
Option c.) csv
Option d.) pandas

Answer

#Ans. c. csv





Question 126. Write the output of the following code :
import csv
f = open("data.csv", 'r')
row = csv.reader(f)
print(row)

Option a.) It prints the memory address where csv.reader object is stored
Option b.) It prints the first record of data.csv
Option c.) It prints all the records of data.csv
Option d.) None of the above

Answer

#Ans. a. It prints the memory address where csv.reader object is stored




Question 127. _________ function help us to read the csv file.

Option a.) reader( )
Option b.) read( )
Option c.) read(n)
Option d.) readline( )

Answer

#Ans. a. reader( )




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

Option a.) isAmit
Option b.) sAmit
Option c.) Amit
Option d.) eisAmit

Answer

#Ans. b. sAmit




Question 129. Which of the following method returns an integer value?

Option a.) seek( )
Option b.) read( )
Option c.) tell( )
Option d.) readline( )

Answer

#Ans. c. tell( )




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

Option a.) pickling
Option b.) unpickling
Option c.) reading
Option d.) writing

Answer

#Ans. a. pickling




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

Option a.) dump
Option b.) load
Option c.) write
Option d.) list

Answer

#Ans. b. load




Question 132. Write the output of the following code:
f = open("data.txt","w")
L=["My\n","name\n","is\n","amit"]
f.writelines(L)
f.close()
f = open("data.txt","r")
print(len(f.read()))

Option a.) 13
Option b.) 14
Option c.) 15
Option d.) 16

Answer

#Ans. c. 15




Question 133. Write the output of the following code:
f = open("data.txt","w")
L=["My\n","name\n","is\n","amit"]
f.writelines(L)
f.close()
f = open("data.txt","r")
print(len(f.readlines()))

Option a.) 4
Option b.) 5
Option c.) 6
Option d.) 7

Answer

#Ans. a. 4




Question 134. Write the output of the following code:
f = open("data.txt","w")
L=["My\n","name\n","is\n","amit"]
f.writelines(L)
f.close()
f = open("data.txt","r")
print(len(f.readline()))

Option a.) 2
Option b.) 3
Option c.) 4
Option d.) 5

Answer

#Ans. b. 3




Question 135. Fill in the blank in the given code :
import pickle
f = open("data.dat", "wb")
L = [1, 2, 3]
pickle._______
f.close()

Option a.) dump(f, L)
Option b.) load(f, L)
Option c.) dump(L, f)
Option d.) load(L, f)

Answer

#Ans. c. dump(L, f)





Question 136. Which of the following statement will return attribute error?

Option a.) print(len(f.readlines( ).split( ))) #f is file handle
Option b.) print(len(f.readline( ).split( ))) #f is file handle
Option c.) print(len(f.read( ).split( ))) #f is file handle
Option d.) None of the above

Answer

#Ans. a. print(len(f.readlines( ).split( )))




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

Option a.) "data.txt"
Option b.) data.txt
Option c.) "data"
Option d.) data

Answer

#Ans. b. data.txt




Question 138. Identify the suitable code for blank space in the line marked as Statement 2(Refer Q. 137)

Option a.) readlines( )
Option b.) readline( )
Option c.) read( )
Option d.) None of the above

Answer

#Ans. c. read( )




Question 139. Identify the suitable code for blank space in the line marked as Statement 3(Refer Q. 137)

Option a.) split( )
Option b.) break( )
Option c.) words( )
Option d.) jump( )

Answer

#Ans. a. split( )




Question 140. Identify the suitable code for blank space in the line marked as Statement 4(Refer Q. 137)

Option a.) length
Option b.) len
Option c.) Len
Option d.) Length

Answer

#Ans. b. len




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

Option a.) import
Option b.) create
Option c.) data
Option d.) Import

Answer

#Ans. a. import




Question 142. Identify the correct option for Statement2 (Refer Q 141)

Option a.) w
Option b.) a
Option c.) rb
Option d.) r

Answer

#Ans. d. r




Question 143. Identify the correct option for Statement3 (Refer Q 141)

Option a.) readlines
Option b.) readline
Option c.) read
Option d.) reader

Answer

#Ans. d. reader




Question 144. Identify the correct option for Statement4 (Refer Q 141)

Option a.) row
Option b.) d
Option c.) csv
Option d.) i

Answer

#Ans. a. row




Question 145. Parth is writing a program to add/insert records in file "data.csv". He has written the following code. As a programmer, help him to execute it successfully.
import csv
field = ["Roll no" , "Name" , "Class"]
f = open("_________" , 'w')Â Â Â Â Â Â #Statement1
d=__________.writer(f)Â Â Â Â Â Â Â Â #Statement2
Option d.)writerow(field)
ch='y'
while ch=='y' or ch=='Y':
    rn=int(input("Enter Roll number: "))
    nm = input("Enter name: ")
    cls = input("Enter Class: ")
    rec=[_________]     #Statement3
    d.writerow(rec)
    ch=input("Enter more record??(Y/N)")
f.close()
Identify the correct statement for Statement 1

Option a.) data
Option b.) data.csv
Option c.) file_data.csv
Option d.) None of the above

Answer

#Ans. b. data.csv




Question 146. Identify the correct statement for Statement 2. (Refer Q. 145)

Option a.) CSV
Option b.) csv
Option c.) file
Option d.) csv_

Answer

#Ans. b. csv




Question 147. Identify the correct statement for Statement 3. (Refer Q. 145)

Option a.) rollno, name, class
Option b.) Roll no, Name, Class
Option c.) rn, nm, cls
Option d.) field

Answer

#Ans. c. rn, nm, cls




Question 148. 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( )
Identify the correct statement for Statement 1.

Option a.) r
Option b.) rb+
Option c.) wa
Option d.) w

Answer

#Ans. d. w




Question 149. Identify the correct statement for Statement 2.(Refer Q148.)

Option a.) read
Option b.) reader( )
Option c.) reader
Option d.) read(r)

Answer

#Ans. c. reader




Question 150. Identify the correct statement for Statement 2.(Refer Q148.)

Option a.) d
Option b.) d1
Option c.) f
Option d.) f1

Answer

#Ans. b. d1




Question 151. Simran is writing a function by name "bookshop" in which she is asking about book name and price from the user and storing the details in a file "book.txt". Function is showing some error due to some missing words/commands. As a programmer help her to write the correct program.
_________________ bookshop: #Statement 1
b_name = input("Enter book name:")
b_price=int(input("Enter book price:"))
data = _________________([b_name, b_price]) #Statement 2
f = open("book.txt","w")
f.write(_____________) #Statement 3
f.close()
Identify the correct option for Statement 1.

Option a.) def
Option b.) Def
Option c.) define
Option d.) Define

Answer

#Ans. a. def




Question 152. Identify the correct option for Statement 2.

Option a.) int
Option b.) str
Option c.) char
Option d.) float

Answer

#Ans. b. str




Question 153. Identify the correct option for Statement 3.

Option a.) data
Option b.) b_name
Option c.) b_price
Option d.) f

Click the button to see more MCQ

error: Content is protected !!