Binary file operations with pickle PYQ part 1
PYQ PRACTICE SCOPE: Binary file operations with pickle.
Concept ID: U1_BINARY_PICKLE_OPS.
Use only previous-year questions whose concept_ids include U1_BINARY_PICKLE_OPS.
Key things we'll cover
Binary file operations with pickle
PYQ references used by both prompts8 included0 excluded
Flagged questions are excluded from both Study Notes and PPT references until corrected in Paper Analyzer.
Included
ID 6482 · 2021 · 1 mark(s)
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.
File Handling → Binary file operations with pickle
Included
ID 6484 · 2021 · 1 mark(s)
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
File Handling → Binary file operations with pickle
Included
ID 6503 · 2021 · 1 mark(s)
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)
File Handling → Binary file operations with pickle
Included
ID 6504 · 2021 · 1 mark(s)
A binary file employee.dat has the following data:
| Empno | Empname | Salary |
|-------|---------------|--------|
| 101 | Anuj | 50000 |
| 102 | Arijita | 40000 |
| 103 | Hanika | 30000 |
| 104 | Firoz | 60000 |
| 105 | Vijaylakshmi | 40000 |
def display(eno):
f=open("employee.dat","rb")
totSum=0
try:
while True:
R=pickle.load(f)
if R[0]==eno:
__________ #Line1
totSum=totSum+R[2]
except:
f.close()
print(totSum)
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
File Handling → Binary file operations with pickle
Included
ID 6568 · 2022 · 4 mark(s)
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 be displayed.
import _______ #Statement 1
def update_data():
rec={}
fin=open("record.dat","rb")
fout=open("_____________") #Statement 2
found=False
eid=int(input("Enter employee id to update their salary :: "))
while True:
try:
rec=______________ #Statement 3
if rec["Employee id"]==eid:
found=True
rec["Salary"]=int(input("Enter new salary :: "))
pickle.____________ #Statement 4
else:
pickle.dump(rec,fout)
except:
break
if found==True:
print("The salary of employee id ",eid," has been updated.")
else:
print("No employee with such id is not found")
fin.close()
fout.close()
(i) Which module should be imported in the program? (Statement 1)
(ii) Write the correct statement required to open a temporary file named temp.dat. (Statement 2)
(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?
File Handling → Binary file operations with pickle
Included
ID 6129 · 2023 · 4 mark(s)
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 calculates 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.
(ii) Which statement should Shreyas fill in Statement 2 to check whether quantity is less than 10.
(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.
File Handling → Binary file operations with pickle
Included
ID 6130 · 2023 · 4 mark(s)
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 calculates 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.
(ii) Which statement should Shreyas fill in Statement 2 to check whether quantity is less than 10.
(Option for part (iii) only)
(iii) 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 ?
File Handling → Binary file operations with pickle
Included
ID 6325 · 2023 · 4 mark(s)
Atharva is a programmer, who has recently been given a task to write a Python code to perform the following binary file operation with the help of a user defined function/module :
- Copy_new() : to create a binary file new_items.dat and write all the item details stored in the binary file, items.dat, except for the item whose item_id is 101. The data is stored in the following format :
{item_id:[item_name,amount]}
import ______ # Statement 1
def Copy_new():
f1= ______ # Statement 2
f2= ______ # Statement 3
item_id=int(input("Enter the item id"))
item_detail= ______ # Statement 4
for key in item_detail:
if ______: # Statement 5
pickle. ______ # Statement 6
f1.close()
f2.close()
He has succeeded in writing partial code and has missed out certain statements. Therefore, as a Python expert, help him to complete the code based on the given requirements :
(i) Which module should be imported in the program ? (Statement 1)
(ii) Write the correct statement required to open the binary file "items.dat". (Statement 2)
(iii) Which statement should Atharva fill in Statement 3 to open the binary file "new_items.dat" and in Statement 4 to read all the details from the binary file "items.dat".
File Handling → Binary file operations with pickle