Class 12 Computer Science File Handling in Python MCQs Set 8

Python File Handling



File Handling in Python MCQ’s Set – 8

Multiple Choice Questions


Directions (Question Number 141 to 143) :

Sumit want to write a list in file “test.dat” file. He has written some codes, but it is not complete. Complete these statements. Code is given below.

import pickle as _____                  #statement 1
letter = ['a','b','c','d']
with open('test.dat','____') as file:   #statement 2
    pkl.dump(letter,____)               #statement 3

141. Write the valid alias name of pickle module in #statement 1 ?

a) pk

b) p

c) pkl

d) pickl

Answer : c) pkl

142. Fill in the blank in #statement 2 to write the proper file opening mode?

a) wb

b) a

c) r

d) w

Answer : a) wb

143. Fill in the blank in #statement 3 to write the content of list to the binary file ‘test.dat’?

a) pkl.dump(letter)

b) pkl.dump(letter, file)

c) pkl.dump(letter, file_object)

d) dump.pkl(letter, file)

Answer : b) pkl.dump(letter,file)

144. Which one is the valid difference between open() and with open() statement?

a) open() close the file automatically but with open() does not close the file automatically.

b) open() does not close the file automatically, but with open() close the file automatically.

c) open() and with open() both will close the file automatically.

d) None of these

Answer : b) open() does not close the file automatically, but with open() close the file automatically.

Directions (Question Number 145 to 147) :

Riya want to write content of a dictionary in a binary file “result.dat” file. She has written some codes, but it is not working. Complete these statements. Code is given below.

import ______                            #statement 1
fa_result {'amrit':50, 'sumit':45, 'tanmay':50}
with open('______','____') as file:      #statement 2
    _____________________                #statement 3

145. Write the valid import statement in #statement 1 ?

a) import pkl

b) import module pickle

c) import pickle

d) None of these

Answer : c) import pickle

146. Fill in the blank in #statement 2, to open a binary file ‘result.dat’ to write dictionary.

a) with open(result.dat, wb) as file:

b) with open(‘result.dat’ , ‘wb’) as file:

c) with open(‘result.dat’, wb) as file:

d) None of these

Answer : b) with open(‘result.dat’, ‘wb’) as file:

147. Fill in the blank in #statement 3, to write the content of dictionary fa_result to the binary file ‘result.dat’?

a) pickle.dump(fa_result, file)

b) pkl.dump(fa_result, file)

c) pickle.dump(fa_result)

d) pickle.dump(file)

Answer : a) pickle.dump(fa_result, file)

Directions (Question Number 148 to 151) :

Sumit want to read the content from a binary file “test.dat” as a list and print the result as list. He has written some codes, but it is not complete. Complete these statements. Code is given below.

import _______________                  #statement 1
letter = []
with open('________','____') as file:   #statement 2
    _____________________               #statement 3
print(________)                         #statement 4

148. Write the statement to import the proper module in #statement 1 ?

a) import pi

b) import binaryfile

c) import pkl

d) import pickle

Answer : d) import pickle

149. Fill in the blank in #statement 2, to open the binary file for reading

a) with (‘test.dat’, ‘wb’)

b) with open(‘test.dat’, ‘r’) as file:

c) with open(‘test.dat’, ‘rb’) as file:

d) with open(‘test.dat’, ‘wb’) as file:

Answer : c) with open(‘test.dat’, ‘rb’) as file:

150. Fill in the blank in #statement 3, to read a list from a binary file.

a) pickle.load(letter, file)

b) file = pickle.load(letter)

c) letter = load(file)

d) letter = pickle.load(file)

Answer : d) letter = pickle.load(file)

151. Fill in the blank in #statement 4, to display the value of list letter.

a) print(letter[0])

b) print(letter)

c) print letter

d) None of these

Answer : b) print(letter)

Directions (Question Number 152 to 155) :

Riya want to read a binary file named “result.dat” and want to read data and store in a diction. She has written some codes, but it is not working. Complete these statements. Code is given below.

import _______________                  #statement 1
info = {}
with open('________','____') as file:   #statement 2
    _____________________               #statement 3
print(________)                         #statement 4

152. Write the statement to import the proper module in #statement 1 ?

a) import pi

b) import binaryfile

c) import pkl

d) import pickle

Answer : d) import pickle

153. Fill in the blank in #statement 2, to open the binary file for reading

a) with (‘result.dat’, ‘wb’)

b) with open(‘result.dat’, ‘r’) as file:

c) with open(‘result.dat’, ‘rb’) as file:

d) with open(‘result.dat’, ‘wb’) as file:

Answer : c) with open(‘result.dat’, ‘rb’) as file:

154. Fill in the blank in #statement 3, to read a list from a binary file.

a) pickle.load(info, file)

b) file = pickle.load(info)

c) info = load(file)

d) info = pickle.load(file)

Answer : d) info = pickle.load(file)

155. Fill in the blank in #statement 4, to display the value of list letter.

a) print(info[‘amit’])

b) print(info)

c) print info

d) None of these

Answer : b) print(info)

156. “wb” mode in file handling also can be written as _____

a) w+b

b) w*b

c) w/b

d) None of these

Answer : a) w+b

157. Mode used for reading from pickle file is ________.

a) br

b) r*b

c) rb

d) None of these

Answer : c) rb

158. When file is opened as “with open” then file is ___________ automatically..

a) open

b) read

c) write

d) close

Answer : d) close

Directions (Question Number 159 to 160) :

Define the function store_data(record) in python to write the content in a pickled file member.dat

import pickle

def store_data(record):
    fileObject = open('________','____')#statement 1
    pickle.______(_______, ________)  #statement 2    
    piFile.close()

159. Write the statement to open the file to write data in #statement 1 ?

a) fileObject = open(‘member.dat’)

b) fileObject = open(‘member.dat’, ‘w’)

c) fileObject = open(‘member.dat’, ‘wb’)

d) None of these

Answer : c) fileObject = open(‘member.dat’, ‘wb’)

160. Fill in the blank in #statement 2, to write record in the binary file.

a) pickle.dump(record, fileObject)

b) pickle.dump(‘record’, ‘fileObject’)

c) pickle.dump(fileObject, record)

d) None of these

Answer : a) pickle.dump(record, fileObject)


More MCQ’s are on the way, Keep visiting…

Python File Handling (MCQ’s)


Python Revision Tour – Multiple Choice Questions (MCQ’s)


Python Functions – Working with Functions (MCQ’s)


Thanks for visiting. Online Classes are available.

You cannot copy content of this page

Scroll to Top