Solved QBank_Binary Files
Solved QBank_Binary Files
CLASS XII
CBSE SOLVED QUESTION BANK
BINARY FILES
1 mark
2) Which of the following statement is incorrect in the context of pickled binary files ?
a. csv module is used for reading and writing object in binary files.
b. pickle module is used for reading and writing objects in binary files.
c. load ( ) of the pickle module is used to read objects.
d. dump ( ) of the pickle module is used to write objects.
Ans: In pickled binary files only pickle, load ( ) & dump ( ) exist. csv module is not in the
context of pickled binary files, Hence, the correct option is ‘a’.
3) Which of the following Python modules is imported to store and retrieve objects using
the process of serialization and deserialization ?
a. csv b. binary c. math d. pickle
Ans: Pickle module is imported to store and retrieve objects using the process of
serialization and deserialization. Hence, the correct option is ‘d’.
Suppose the present working directory is MyCompany. What will be the relative path of
the file Transactions.Dat ?
a. MyCompany/Transactions.Dat
b. MyCompany/Accounts/Transaction.Dat
c. Accounts/Transactions.Dat
d. ../transaction.Dat
Ans: The Correct output of the following Python statement will be
Accounts/Transactions.Dat. Hence, the correct option is ‘c’.
6) Which pickle module method is used to write a Python object to a binary file?
a. save() b. serialize() c. store() d. dump()
Ans: d. dump()
7) Assertion (A): A binary file in python is used to store collection objects like lists and
dictionaries that can be later retrieved in their original form using pickle module.
Reasoning (A): Binary files are just like normal text files and can be read using a text
editor like Notepad.
Ans: c. A is True but R is False
9) Mention any two differences between binary files and csv files?
Ans:
10) Which of the following statement is incorrect in the context of binary files?
a. Information is stored in the same format in which the information is held in memory.
b. No character translation takes place
c. Every line ends with a new line character
d. pickle module is used for reading and writing
Ans: c. Every line ends with a new line character
13) Raghav is trying to write a tuple tup1 = (1,2,3,4,5) on a binary file test.bin. Consider the
following code written by him.
import pickle
tup1 = (1,2,3,4,5)
myfile = open("test.bin",'wb')
pickle._______ #Statement 1
myfile.close()
Identify the missing code in Statement 1.
a. dump(myfile,tup1) b. dump(tup1, myfile)
c. write(tup1,myfile) d. load(myfile,tup1)
Ans: b. dump(tup1, myfile)
When the above mentioned function, display (103) is executed, the output displayed is
190000. Write appropriate jump statement from the following to obtain the above
output.
a. jump b. break c. continue d. return
Ans: c. continue
3 marks
15) Aman is a Python programmer. He has written a code and created a binary file
record.dat with employeeid, ename and salary. The file contains 10 records.
He now has to update a record based on the employee id entered by the user and
update the salary. The updated record is then to be written in the file temp.dat. The
records which are not to be updated also have to be written to the file temp.dat. If the
employee id is not found, an appropriate message should to be displayed.
As a Python expert, help him to complete the following code based on the requirement
given above:
(ii) Write the correct statement required to open a temporary file named temp.dat
for writing the updated data. (Statement 2)
Ans: fout=open(‘temp.dat’, ‘wb’)
(iii) Which statement should Aman fill in Statement 3 to read the data from the binary
file, record.dat and in Statement 4 to write the updated data in the file, temp.dat?
Ans: Statement 3: pickle.load(fin)
Statement 4: pickle.dump(rec,fout)
4 / 5 marks
17) Shreyas is a programmer, who has recently been given a task to write a user defined
function named write_bin ( ) to create a binary file called Cust_file.dat containing
customer information – customer number(c_no), name (c_name), quantity (qty),
price (price) and amount (amt) of each customer.
The function accepts customer number, name, quantity and price. Thereafter, it
displays the message ‘Quantity less than 10….. Cannot SAVE’, if quantity entered is
less than 10. Otherwise the function calculated amount as price * quantity and
then writes the record in the form of a list into the binary file.
import pickle
def write_bin ():
bin_file=______ #Statement 1
while True:
c_no=int(input(“enter customer number”))
c_name= input(“enter customer name”)
qty=int(input(“enter qty”))
price=int(input(“enter price”))
if ________ #Statement 2
print(“Quantity less than 10.. Cannot SAVE”)
else:
amt=price*qty
c_detail=[ c_no, c_name, qty, price,amt ]
______ #Statement 3
ans = input(“Do you wish to enter more records y/n”)
if ans.lower()==’n’;
_________ #Statement 4
_______________ #Statement 5
_____________________ #Statement 6
(i) Write the correct statement to open a file ‘Cust_file.dat’ for writing the data of
the customer.
Ans: This is done using the open function with the mode 'wb' for writing in binary
mode.
bin_file = open('Cust_file.dat', 'wb') # Statement 1
(ii) Which statement should Shreyas fill in Statement 2 to check whether quantity is
less than 10.
Ans: if qty < 10: # Statement 2
(iii) Which statement should Shreyas fill in Statement 3 to write data to the binary
file and in Statement 4 to stop further processing if the user does not wish to
enter more records.
Ans: To write data to the binary file, use the pickle.dump function. To close the file if
the user does not wish to enter more records, use the close method on the file
object.
pickle.dump(c_detail, bin_file) # Statement 3
22) Consider a binary file 'INVENTORY.DAT' that stores information about products using
tuple with the structure (ProductID, ProductName, Quantity, Price). Write a Python
function expensiveProducts() to read the contents of 'INVENTORY.DAT' and display
details of products with a price higher than Rs. 1000. Additionally, calculate and display
the total count of such expensive products.
For example: If the file stores the following data in binary format
(1, 'ABC', 100, 5000)
(2, 'DEF', 250, 1000)
(3, 'GHI', 300, 2000)
then the function should display
Product ID: 1
Product ID: 3
Total expensive products: 2
Ans:
23) Consider a file FLIGHT.DAT containing multiple records. The structure of each record is
as shown below:
[Fno, FName, Fare, Source, Destination]
Write a function COPY_REC() in Python that copies all those records from FLIGHT.DAT
where the source is DELHI and the destination is MUMBAI, into a new file RECORD.DAT
Ans:
24) Consider a Binary file BOOK.DAT containing a dictionary having multiple elements.
Each element is in the form BNO:[BNAME,BTYPE,PRICE] as key:value pair
where
BNO – Book Number
BNAME – Book Name
BTYPE - Book Type
PRICE – Book price
Write a user-defined function, findBook(price), that accepts price as parameter and
displays all those records from the binary file BOOK.DAT which has a book price more
than or equal to the price value passed as a parameter.
Ans: