0% found this document useful (0 votes)
70 views11 pages

Solved QBank_Binary Files

Uploaded by

Harshit Saxena
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
70 views11 pages

Solved QBank_Binary Files

Uploaded by

Harshit Saxena
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 11

AMITY INTERNATIONAL SCHOOL, MAYUR VIHAR

CLASS XII
CBSE SOLVED QUESTION BANK
BINARY FILES
1 mark

1) Which of the following is a function/method of the pickle module ?


a) reader ( ) b) writer ( ) c) load ( ) d) read ( )
Ans: c) load()

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’.

4) Consider the following directory structure

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’.

5) How are text files different from binary files?


Ans: Text Files:
• Store human-readable characters (e.g., ASCII, UTF-8).
• Easily opened with a text editor.
• Typically smaller in size.
Binary Files:
• Store data in binary format (sequences of 0s and 1s).
• Cannot be read directly by a text editor.
• Often larger in size.

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

8) Mention any two differences between seek() and tell().


Ans:

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

11) Which of the following statement is true?


a. pickling creates an object from a sequence of bytes
b. pickling is used for object serialization
c. pickling is used for object deserialization
d. pickling is used to manage all types of files in Python
Ans: b. pickling is used for object serialization
12) Which of the following statement opens a binary file record.bin in write mode and writes
data from a list lst1 = [1,2,3,4] on the binary file?
a. with open('record.bin','wb') as myfile:
pickle.dump(lst1,myfile)
b. with open('record.bin','wb') as myfile:
pickle.dump(myfile,lst1)
c. with open('record.bin','wb+') as myfile:
pickle.dump(myfile,lst1)
d. with open('record.bin','ab') as myfile:
pickle.dump(myfile,lst1)
Ans: a. with open('record.bin','wb') as myfile:
pickle.dump(lst1,myfile)

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)

14) A binary file employee.dat has following data

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:

(i) Which module should be imported in the program? (Statement 1)


Ans: pickle

(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)

16) A Binary file, CINEMA.DAT has the following structure:


{MNO:[MNAME, MTYPE]}
Where
MNO – Movie Number
MNAME – Movie Name
MTYPE is Movie Type
Write a user defined function, findType(mtype), that accepts mtype as parameter and
displays all the records from the binary file CINEMA.DAT, that have the value of Movie
Type as mtype.
Ans:

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

if ans.lower() == 'n': # Statement 4


break
(iv) What should Shreyas fill in Statement 5 to close the binary file named
Cust_file.dat and in Statement 6 to call a function to write data in binary file?
Ans: In Statement 5 Shreyas has to close the binary file named ‘Cust_file.dat’
bin_file.close() # Statement 5
And in Statement 6 he has to call a function to write in the binary file
write_bin() # Statement 6
18) Surya is a manager working in a recruitment agency. He needs to manage the
records of various candidates. For this, he wants the following information of each
candidate to be stored:
- Candidate_ID – integer
- Candidate_Name – string
- Designation – string
- Experience – float
You, as a programmer of the company, have been assigned to do this job for Surya.
(I) Write a function to input the data of a candidate and append it in a binary file.
ii) Write a function to update the data of candidates whose experience is more
than 10 years and change their designation to "Senior Manager".
(I) Write a function to read the data from the binary file and display the data of all
those candidates who are not "Senior Manager".
Ans:
19) A binary file "PLANTS.dat" has structure (ID, NAME, PRICE).
• Write the definition of a function WRITEREC() in Python, to input data for records
from the user and write them to the file PLANTS.dat.
• Write the definition of a function SHOWHIGH() in Python, which reads the
records of PLANTS.dat and displays those records for which the PRICE is more
than 500.
Ans:

20) A binary file "PATIENTS.dat" has structure (PID, NAME, DISEASE).


Write the definition of a function countrec()in Python that would read contents
of the file "PATIENTS.dat" and display the details of those patients who have
the DISEASE as 'COVID-19'. The function should also display the total number
of such patients whose DISEASE is 'COVID-19'.
Ans:
21) Consider a file, SPORT.DAT, containing records of the following structure:
[SportName, TeamName, No_Players]
Write a function, copyData(), that reads contents from the file SPORT.DAT and copies
the records with Sport name as “Basket Ball” to the file named BASKET.DAT. The
function should return the total number of records copied to the file BASKET.DAT.
Ans:

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:

You might also like