0% found this document useful (0 votes)
16 views

150+ Best MCQ File Handling in Python Class 12

Uploaded by

MANSIMAR SINGH
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

150+ Best MCQ File Handling in Python Class 12

Uploaded by

MANSIMAR SINGH
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 42

150+ Best MCQ File Handling

in Python Class 12
File Handling in Python Class 12

Q1. Which statement will read 5


characters from a file(file object ‘f’)?
a. f.read()
b. f.read(5)
c. f.reads(5)
d. None of the above
Ans. b. f.read(5)
Q2. Which function open file in python?
a. open( )
b. new( )
c. Open( )
d. None of the above
Ans. a. open( )
Q3. Processing of Text file is faster than
binary files.(T/F)
a. True
b. False
Ans. b. False
Q4. Which mode create new file if the
file does not exist?
a. write mode
b. append mode
c. Both of the above
d. None of the above
ANS
Ans. c. Both of the above
Q5. Which statement will return one
line from a file (file object is ‘f’)?
a. f.readline( )
b. f.readlines( )
c. f.read( )
d. f.line( )
ANS
Ans. a. f.readline( )
File Handling in Python Class 12
MCQ
Q6. readlines() method return _________
a. String
b. List
c. Dictionary
d. Tuple
ANS
Ans. b. List
Q7. EOF stands for _________________
a. End of File
b. End off File
c. End on File
d. End or File
ANS
Ans. a. End of File
Q8. Which function is used to read data
from Text File?
a. read( )
b. writelines( )
c. pickle( )
d. dump( )
ANS
Ans. a. read( )
Q9. Which of the following will read
entire content of file(file object ‘f’)?
a. f.reads( )
b. f.read( )
c. f.read(all)
d. f.read( * )
ANS
Ans. b. f.read( )
Q10. Which symbol is used for append
mode?
a. ap
b. a
c. w
d. app
ANS
Ans. b. a
Q11. Which of the following options can
be used to read the first line of a text
file data.txt?
a. f = open(‘data.txt’); f.read()
b. f = open(‘data.txt’,’r’); f.read(n)
c. myfile = open(‘data.txt’); f.readline()
d. f = open(‘data.txt’); f.readlines()
ANS
Ans. c. myfile = open(‘data.txt’); f.readline()
Q12. File in python is treated as
sequence of ________________
a. Bytes
b. Bites
c. bits
d. None of the above
ANS
Ans. a. Bytes
Q13. Which function is used to write
data in binary mode?
a. write
b. writelines
c. pickle
d. dump
ANS
Ans. d. dump
Q14. Which function is used to force
transfer of data from buffer to file?
a. flush( )
b. save( )
c. move( )
d. None of the above
ANS
Ans. a. flush( )
Q15. 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?
a. f.read( )
b. f.read(all)
c. f.readline( )
d. f.readlines( )
ANS
Ans. d. f.readlines( )
Q16. ____________________ module is
used for serializing and de-serializing
any Python object structure.
a. pickle
b. unpickle
c. pandas
d. math
ANS
Ans. a. pickle
Q17. Which of the following error is
returned when we try to open a file in
write mode which does not exist?
a. FileNotFoundError
b. FileFoundError
c. FileNotExistError
d. None of the above
ANS
Ans. d. None of the above r
Q18. ______________ function returns the
strings.
a. read( )
b. readline( )
c. Both of the above
d. None of the above
ANS
Ans. c. Both of the above
Q19. The syntax of seek() is:
file_object.seek(offset [,
reference_point])
What is reference_point indicate?
a. reference_point indicates the starting position of the file
object
b. reference_point indicates the ending position of the file
object
c. reference_point indicates the current position of the file
object
d. None of the above.
ANS
Ans. a. reference_point indicates the starting position of the
file object
Q20. Identify the invalid mode from the
following.
a. a
b. r+
c. ar+
d. w
ANS
Ans. c. ar+
File Handling in Python Class 12
Q21. Which of the following is an
invalid mode of file opening?
a. read only mode
b. write only mode
c. read and write mode
d. write and append mode
ANS
Ans. d. write and append mode
Q22. readlines( ) function returns all
the words of the file in the form of List.
(T/F)
a. True
b. False
ANS
Ans. b. False
Q23. What is ‘f’ in the following
statement?
f=open("Data.txt" , "r")
a. File Name
b. File Handle
c. Mode of file
d. File Handling
ANS
Ans. b. File Handle
Q24. What is full form of CSV
a. Comma Separation Value
b. Comma Separated Value
c. Common Syntax Value
d. Comma Separated Variable
ANS
Ans. b. Comma Separated Value
Q25. Which statement will open file
“data.txt” in append mode?
a. f = open(“data.txt” , “a”)
b. f = Open(“data.txt” , “ab”)
c. f = new(“data.txt” , “a”)
d. open(“data.txt” , “a”)
ANS
Ans. a. f = open(“data.txt” , “a”)
Q26. Fill in the blank

import pickle
f=open("data.dat",'rb')
d=_____________________.load(f)
f.close()
a. unpickle
b. pickling
c. pickle
d. pick
ANS
Ans. c. pickle
Q27. Which module to be imported to
make the following line functional?
sys.stdout.write("ABC")
a. system
b. sys
c. stdout
d. stdin
ANS
Ans. b. sys
Q28. What error is returned by the
following statement if the file does not
exist?
f=open("A.txt")
a. FileNotFoundError
b. NotFoundError
c. FileNotFound
d. FoundError
ANS
Ans. a. FileNotFoundError
Q29. Which statement will return error?
import pickle
f=open("data.dat",'rb')
d=pickle.load(f)
f.end()
a. Statement 1
b. Statement 2
c. Statement 3
d. Statement 4
ANS
Ans. d. Statement 4
Q30. Which of the following function
takes two arguments?
a. load( )
b. dump( )
c. both of the above
d. none of the above
ANS
Ans. b. dump( )
Q31. Almost all the files in our
computer stored as _______ File.
a. Text
b. Binary
c. CSV
d. None of the above
ANS
Ans. b. Binary
Q32. Binary files are not in human
readable format.(T/F)
a. True
b. False
ANS
Ans. a. True
Q33. .pdf and .doc are examples
of __________ files.
a. Text
b. Binary
c. CSV
d. None of the above
ANS
Ans. b. Binary
Q34. The syntax of seek()
is:file_object.seek(offset [,
reference_point]) What all values can
be given as a reference point?
a. 1
b. 2
c. 0
d. All of the above
ANS
Ans. d. All of the above
Q35. There is no delimiter to end a line
in binary files.(T/F)
a. True
b. False
ANS
Ans. a. True
Q36. seek( ) method is used for random
access to the file.(T/F)
a. True
b. False
ANS
Ans. a. True
Q37. 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

a. csv
b. unpickle
c. pickle
d. load
ANS
Ans. c. pickle
Q38. Refer to the above code and
choose the option for statement2.
a. w
b. w+
c. wb
d. write
ANS
Ans. c. wb
Q39. Refer to the above code (Q
38)and choose the option for statement
3
a. unpickle
b. write
c. pickle
d. None of the above
ANS
Ans. c. pickle
Q40. Refer to the above code (Q
38)and choose the option for statement
4.
a. f
b. rec
c. file
d. stud
ANS
Ans. a. f
File Handling in Python Class 12
Q41. The syntax of seek()
is:file_object.seek(offset [,
reference_point] What is the default
value of reference_point
a. 0
b. 1
c. 2
d. 3
ANS
Ans. a. 0
Q42. _______ function returns the
current position of file pointer.
a. get( )
b. tell( )
c. cur( )
d. seek( )
ANS
Ans. b. tell( )
Q43. f.seek(10,0) will move 10 bytes
forward from beginning of file.(T/F)
a. True
b. False
ANS
Ans. a. True
Q44. Which statement will move file
pointer 10 bytes backward from current
position.
a. f.seek(-10, 0)
b. f.seek(10, 0)
c. f.seek(-10, 1)
d. None of the above
ANS
Ans. c. f.seek(-10, 1)
Q45. When we open file in append
mode the file pointer is at
the _________ of the file.
a. end
b. beginning
c. anywhere in between the file
d. second line of the file
ANS
Ans. a. end
Q46. When we open file in write mode
the file pointer is at the _______ of the
file.
a. end
b. beginning
c. anywhere in between the file
d. second line of the file
ANS
Ans. b. beginning
Q47. 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
a. Hello
b. Hell
c. ello
d. None of the above
ANS
Ans. a. Hello
Q48. Refer to the above code (Q 47) :
Write the output of Second Print
Statement
a. om
b. me
c. co
d. None of the above
ANS
Ans. b. me
Q49. Refer to the above code (Q 47) :
Write the output of Third Print
Statement
a. e to m
b. e to my
c. to my
d. None of the above
ANS
Ans. c. to my
Q50. Refer to the above code (Q 47) :
Write the output of Fourth Print
Statement
a. 17
b. 16
c. 19
d. 18
ANS
Ans. d. 18
Q51. A _____________ is a named
location on a secondary storage media
where data are permanently stored for
later access.
a. file
b. variable
c. comment
d. token
ANS
Ans. a. file
Q52. A _____ file consists of human
readable characters.
a. Binary
b. Text
c. Both of the above
d. None of the above
ANS
Ans. b. Text
Q53. Which of the following file require
specific programs to access its
contents?
a. Binary
b. Text
c. CSV
d. None of the above
ANS
Ans. a. Binary
Q54. Which of the following file can be
opened in any text editor?
a. Binary
b. Text
c. Both of the above
d. None of the above
ANS
Ans. b. Text
Q55. Each line of a text file is
terminated by a special character,
called the ________
a. End of File
b. End of Line
c. End of Statement
d. End of program
ANS
Ans. b. End of Line
Q56. Default EOL character in text file
is ________
a. \n
b. \N
c. \t
d. \l
ANS
Ans. a. \n
Q57. Which of the following file can be
created in python?
a. Text File
b. Binary File
c. CSV File
d. All of the above
ANS
Ans. d. All of the above
Q58. In which of the following data
store permanently?
a. File
b. Variable
c. Both of the above
d. None of the above
ANS
Ans. a. File
Q59. open( ) function takes ____ as
parameter.
a. File name
b. Access mode
c. Both of the above
d. None of the above
ANS
Ans. c. Both of the above
Q60. Identify the correct statement to
open a file:
a. f = open(“D:\\myfolder\\naman.txt”)
b. f = open(“D:\myfolder\naman.txt”)
c. f = open(“D:myfolder#naman.txt”)
d. f = Open(“D:\myfolder\naman.txt”)
ANS
Ans. a. f = open(“D:\\myfolder\\naman.txt”)
File Handling in Python Class 12

Q61. Which of the following are the


attributes of file handle?
a. closed
b. name
c. mode
d. All of the above
ANS
Ans. d. All of the above
Q62. Write the output of the following:
>>> f = open("test.txt","w")
>>> f.closed
a. True
b. False
c. Yes
d. No
ANS
Ans. b. False
Q63. Write the output of the following:
>>> f = open("test.txt","w")
>>> f.mode
a. ‘w’
b. ‘r’
c. ‘a’
d. ‘w+’
ANS
Ans. a. ‘w’
Q64. Write the output of the following:
>>> f = open("test.txt","w")
>>> f.name
a. ‘test’
b. ‘test.txt’
c. ‘test.dat’
d. file does not exist
ANS
Ans. b. ‘test.txt’
Q65. Write the output of the following:
>>> f = open("test.txt","w")
>>> f.close()
>>> f.closed
a. True
b. False
c. Yes
d. No
ANS
Ans. a. True
Q66. Which of the following attribute of
file handle returns Boolean value?
a. name
b. closed
c. mode
d. None of the above
ANS
Ans. b. closed
Q67. Ravi opened a file in python using
open( ) function but forgot to specify
the mode. In which mode the file will
open?
a. write
b. append
c. read
d. read and write both
ANS
Ans. c. read
Q68. Which of the following is invalid
mode of opening file?
a. r
b. rb
c. +r
d. None of the above
ANS
Ans. d. None of the above
Q69. Which of the following mode will
create a new file, if the file does not
exist?
a. ‘a’
b. ‘a+’
c. ‘+a’
d. All of the above
ANS
Ans. d. All of the above
Q70. Which of the following mode will
open the file in binary and read-only
mode.
a. ‘r’
b. ‘rb’
c. ‘r+’
d. ‘rb+’
ANS
Ans. b. ‘rb’
Q71. Which of the following mode will
opens the file in read, write and binary
mode?
a. ‘wb+
b. ‘+wb’
c. Both of the above
d. None of the above
ANS
Ans. c. Both of the above
Q72. In the given statement, the file
myfile.txt will open
in _______________ mode.
myObject=open(“myfile.txt”, “a+”)
a. append and read
b. append and write
c. append and read and binary
d. All of the above
ANS
Ans. a. append and read
Q73. Ravi opened the file myfile.txt in
append mode. In this file the file
object/file handle will be at
the __________
a. beginning of the file
b. end of the file
c. second line of the file
d. the end of the first line
ANS
Ans. b. end of the file
Q74. Ravi opened the file myfile.txt in
write mode. In this file the file
object/file handle will be at
the ______________
a. beginning of the file
b. end of the file
c. second line of the file
d. the end of the first line
ANS
Ans. a. beginning of the file
Q75. Ravi opened the file myfile.txt in
read mode. In this file the file object/file
handle will be at the _______________
a. beginning of the file
b. end of the file
c. second line of the file
d. the end of the first line
ANS
Ans. a. beginning of the file
Q76. 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.
a. read mode
b. write mode
c. append mode
d. binary and read mode
ANS
Ans. b. write mode
Q77. 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.
a. read mode
b. write mode
c. append mode
d. binary and read mode
ANS
Ans. c. append mode
Q78. Which of the following function is
used to close the file?
a. close( )
b. end( )
c. quit( )
d. exit( )
ANS
Ans. a. close( )
Q79. open( ) function returns a file
object called ______________
a. object handle
b. file handle
c. read handle
d. write handle
ANS
Ans. b. file handle
Q80. Which of the following is the valid
way to open the file?
a. with open (file_name, access_mode) as fo:
b. fo = open (file_name, access_mode)
c. Both of the above
d. None of the above
ANS
Ans. c. Both of the above
Q81. 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
ANS
Ans. a. In case the user forgets to close the file explicitly the
file will closed automatically.
Q82. 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 of the above
ANS
Ans. b. append mode
Q83. 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 of the above
ANS
Ans. d. All of the above
Q84. 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 of the above
ANS
Ans. d. All of the above
Q85. 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
ANS
Ans. c. Both of the above
Q86. Write the output of the following:
>>> f = open("test.txt","w")
>>> f.write("File\n#Handling")
a. 17
b. 16
c. 14
d. 15
ANS
Ans. c. 14
Q87. 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
ANS
Ans. b. TypeError
Q88. Which of the following method is
used to clear the buffer?
a. clear( )
b. buffer( )
c. flush( )
d. clean( )
ANS
Ans. c. flush( )
Q89. 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
ANS
Ans. b. writelines( )
Q90. 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()
a. write( )
b. writelines( )
c. writeline( )
d. None of the above
ANS
Ans. b. writelines( )
Q91. 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 of the above
ANS
Ans. d. All of the above
Q92. 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)
ANS
Ans. b. read(n)
Q93. 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
ANS
Ans. d. No Output
Q94. Write the output of the following:
f=open("test.txt","w+")
f.write("File-Handling")
f.seek(0)
a=f.read(5)
print(a)
a. File-
b. File
c. File-H
d. No Output
ANS
Ans. a. File-
Q95. 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
ANS
Ans. a. andli
Q96. 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
ANS
Ans. c. FileHandling
Q97. 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
ANS
Ans. c. FileHandling
Q98. Which of the following method
reads one complete line from a file?
a. read( )
b. read(n)
c. readline( )
d. readlines( )
ANS
Ans. c. readline( )
Q99. Write the output of the following:
f=open("test.txt","w+")
f.write("File\nHandling")
f.seek(0)
a=f.readline(-1)
print(a)
a. File Handling
b. FileHandling
c. File
d. No Output
ANS
Ans. c. File
Q100. Write the output of the following:
f=open("test.txt","w+")
f.write("File\nHandling")
f.seek(0)
a=f.readline(2)
print(a)
a. File\nHandling
b. FileHandling
c. Fi
d. No Output
ANS
Ans. c. Fi
Q101. 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
ANS
Ans. b. This code will read all the lines from the file.
Q102. 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))
a. <class ‘tuple’>
b. <class ‘list’>
c. <class ‘string’>
d. None of the above
ANS
Ans. b. <class ‘list’>
Q103. 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( )
ANS
Ans. d. readlines( )
Q104. 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
a. Statement1
b. Statement2
c. Statement3
d. Statement4
ANS
Ans. c. Statement3
Q105. ____________________ 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
ANS
Ans. b. tell( )
Q106. _______ 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
ANS
Ans. b. seek( )
Q107. 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
ANS
Ans. b. 10
Q108. 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)
ANS
Ans. a. file_object.seek(5, 1)
Q109. 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)
a. me i
b. ame is
c. me is
d. Error
ANS
Ans. c. me is
Q111. Write the output of the following:
f=open("test.txt","r")
print(f.tell())
a. 1
b. 2
c. -1
d. 0
ANS
Ans. d. 0
Q112. 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
ANS
Ans. c. 065
Q113. 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
ANS
Ans. c. 5
Q114. _________ method is used to write
the objects in a binary file.
a. write( )
b. dump( )
c. load( )
d. writer( )
ANS
Ans. b. dump( )
Q115. _________ method is used to read
data from a binary file.
a. write( )
b. dump( )
c. load( )
d. writer( )
ANS
Ans. c. load( )
Q116. Which module is to be imported
for working in binary file?
a. unpickle
b. pickle
c. pickling
d. unpickling
ANS
Ans. b. pickle
Q117. 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
ANS
Ans. a. f = open(“marker.txt”, “r”)
Q118. 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
ANS
Ans. c. f = open(“marker.txt”, “w”)
Q119. 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
ANS
Ans. c. Binary File
Q120. 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 value
ANS
Ans. c. if we try to write on a text file that does not exist,
the file gets Created.
Q121. Correct syntax of tell( ) function
is _________. #f is file object
a. f.tell( )
b. tell(f)
c. f_tell( )
d. None of the above
ANS
Ans. a. f.tell( )
Q122. 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
ANS
Ans. c. 2
Q123. 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.
ANS
Ans. c. Text files are not in Human readable form.
Q124. 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
ANS
Ans. d. test.dat contains records about books
Q125. Which module is to be imported
for CSV file?
a. pickle
b. unpickle
c. csv
d. pandas
ANS
Ans. c. csv
Q126. 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
ANS
Ans. a. It prints the memory address where csv.reader object
is stored
Q127. _________ function help us to
read the csv file.
a. reader( )
b. read( )
c. read(n)
d. readline( )
ANS
Ans. a. reader( )
Q128. 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
ANS
Ans. b. sAmit
Q129. Which of the following method
returns an integer value?
a. seek( )
b. read( )
c. tell( )
d. readline( )
ANS
Ans. c. tell( )
Q130. ___________ 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
ANS
Ans. a. pickling
Q131. 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
ANS
Ans. b. load
Q132. 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()))
a. 13
b. 14
c. 15
d. 16
ANS
Ans. c. 15
Q133. 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()))
a. 4
b. 5
c. 6
d. 7
ANS
Ans. a. 4
Q134. 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()))
a. 2
b. 3
c. 4
d. 5
ANS
Ans. b. 3
Q135. Fill in the blank in the given code
:
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)
ANS
Ans. c. dump(L, f)
Q136. 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
ANS
Ans. a. print(len(f.readlines( ).split( )))
Q137. 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
ANS
Ans. b. data.txt
Q138. Identify the suitable code for
blank space in the line marked as
Statement 2(Refer Q. 137)
a. readlines( )
b. readline( )
c. read( )
d. None of the above
ANS
Ans. c. read( )
Q139. Identify the suitable code for
blank space in the line marked as
Statement 3(Refer Q. 137)
a. split( )
b. break( )
c. words( )
d. jump( )
ANS
Ans. a. split( )
Q140. Identify the suitable code for
blank space in the line marked as
Statement 4(Refer Q. 137)
a. length
b. len
c. Len
d. Length
ANS
Ans. b. len
Q141. 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
ANS
Ans. a. import
Q142. Identify the correct option for
Statement2 (Refer Q 141)
a. w
b. a
c. rb
d. r
ANS
Ans. d. r
Q143. Identify the correct option for
Statement3 (Refer Q 141)
a. readlines
b. readline
c. read
d. reader
ANS
Ans. d. reader
Q144. Identify the correct option for
Statement4 (Refer Q 141)
a. row
b. d
c. csv
d. i
ANS
Ans. a. row
Q145. 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
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
a. data
b. data.csv
c. file_data.csv
d. None of the above

ANS
Ans. b. data.csv
Q146. Identify the correct statement
for Statement 2. (Refer Q. 145)
a. CSV
b. csv
c. file
d. csv_
ANS
Ans. b. csv
Q147. Identify the correct statement
for Statement 3. (Refer Q. 145)
a. rollno, name, class
b. Roll no, Name, Class
c. rn, nm, cls
d. field
ANS
Ans. c. rn, nm, cls
Q148. 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.
a. r
b. rb+
c. wa
d. w
ANS
Ans. d. w
Q149. Identify the correct statement
for Statement 2.(Refer Q148.)
a. read
b. reader( )
c. reader
d. read(r)
ANS
Ans. c. reader
Q150. Identify the correct statement
for Statement 2.(Refer Q148.)
a. d
b. d1
c. f
d. f1
Ans. b. d1
Q151. 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.
a. def
b. Def
c. define
d. Define
ANS
Ans. a. def
Q152. Identify the correct option for
Statement 2.
a. int
b. str
c. char
d. float
ANS
Ans. b. str
Q153. Identify the correct option for
Statement 3.
a. data
b. b_name
c. b_price
d. f
Show Answer
Ans. a. data

You might also like