150+ Best MCQ File Handling in Python Class 12
150+ Best MCQ File Handling in Python Class 12
in Python Class 12
File Handling in Python Class 12
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
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()