0% found this document useful (0 votes)
45 views5 pages

BINARY FILE HANDLING ASSIGNMENT(2024-25)

The document is an assignment for Class XII Computer Science at Bhatnagar International School, focusing on file handling with binary files in Python. It includes multiple-choice questions, programming tasks, and code completion exercises related to file operations, data structures, and functions. The assignment aims to assess students' understanding of file handling concepts and their ability to write and complete Python code for various scenarios.

Uploaded by

clardie00
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views5 pages

BINARY FILE HANDLING ASSIGNMENT(2024-25)

The document is an assignment for Class XII Computer Science at Bhatnagar International School, focusing on file handling with binary files in Python. It includes multiple-choice questions, programming tasks, and code completion exercises related to file operations, data structures, and functions. The assignment aims to assess students' understanding of file handling concepts and their ability to write and complete Python code for various scenarios.

Uploaded by

clardie00
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

BHATNAGAR INTERNATIONAL SCHOOL, PASCHIM VIHAR

CLASS XII COMPUTER SCIENCE


ASSIGNMENT
FILE HANDLING (BINARY FILES)

1. _______________ will return the current position of file pointer in the file
a. seek() b) search() c) tell() d) print()
2. ________ places the file pointer at the specified position in the open file.
a. seek() b) search() c) tell() d) print()
3. F.seek(20,0) will move the file pointer 20 bytes in forward direction from beginning of
file. State True or False
4. F1.seek(-5,1) will move the file pointer 5 bytes backwards from end of file. State True
or False
5. Syntax of seek function in Python is myfile.seek(offset, reference_point) where myfile
is the file object. What is the default value of reference_point?
a. 0 b. 1 c. 2 d. 3
6. A binary file “Book.dat” has structure
[BookNo, Book_Name, Author, Price]
Write the Python program to input data for a record and add to Book.dat.
7. A binary file “Employee.dat” has structure
[empl_no, emp_name, age, salary]
Write the Python program to print the records of those employee whose salary is greater t
than 20000.
8. Consider a Binary file ‘Book.dat’ which stores data of books, where each record of the
book is as
[Bookid, Bookname, Price]

[1, LET US C , 500]


[2, PYTHON DUMMIES , 650]
[3, JAVA BLACK BOOK , 800]
[4, PERL, 425]
[5, PHP , 780]
[6, PROGRAMMING C# , 710]
Identify the output of following python function:def display():
f = open(“Book.dat”,”rb”)
total = 0
while true:
try:
book = pickle.load(f)
if book[1][0]!=’P’:
continue
total+=book[2]
except EOFError:
file.close( )
print(total)

a. 1300
b. 2565
c. 3865
d. None of the above

9. Differentiate between r+ and w+ file modes in Python. (V. Imp)


10. Mention any two differences between seek() and tell(). (V.imp)
11. Mention any two differences between binary files and csv files? (V.imp)

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

13. 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 pric
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

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

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

16. A binary file “Employee.dat” has structure


[empl_no, emp_name, salary]
Write the Python program to print the records of those employee whose salary is greater than 20000.

17. A binary file sports.dat contains information in the following structure:


[Event, Participant ]
Write a function, copyData(), that reads contents from the file “sports.dat” and creates a file named
“Athletic.dat” copying only those records from sports.dat where the event name is “Athletics”.
18. Saritha is trying to add data onto a existing binary file and is facing difficulty in completing the code.
Help her to fill the gaps in the code.
Incomplete Code:
import pickle
print("WORKING WITH BINARY FILES")
_____________________________ # Statement 1
recno=1
print ("Enter Records of Employees")
print()
#taking data from user and dumping in the file as list object
while True:
print("RECORD No.", recno)
eno=int(input("\tEmployee number : "))
ename=input_____________________ # Statement 2
ebasic=int(input("\tBasic Salary : "))
allow=int(input("\tAllowances : "))
totsal=ebasic+allow
print("\tTOTAL SALARY : ", totsal)
edata=[eno,ename,ebasic,allow,totsal]
pickle.dump(_____________) # Statement 3
ans=input("Do you wish to enter more records (y/n)? ")
recno=recno+1
if ans.lower()=='n':
print("Record entry OVER ")
print()
break # retrieving the size of file
print("Size of binary file (in bytes):",
bfile.tell())
______() # Statement 4

19. A Binary file Stock.dat has a structure [pno,pname,qty,price].A user defined function
Createfile() to input data for 3 records and add to stock.dat .There are some blanks
help in filling the gaps in the code:

Incomplete Code :
Import ___________ # Statement 1
def createfile():
File=open(“d:\\Stock.dat”,‟____‟) #Statement 2
pno=input(“Enter product no:”)
pname= input(“Enter product name:”)
qty= input(“Enter product quantity:”)
price= input(“Enter product price:”)
record=[pno,pname,qty,price
_______________ # Statement 3
Print(“Record inserted”)
File.close()

Createfile()
20. A binary file “STUDENT.DAT” has structure [admission_number, Name, Percentage]. Write a function
countrec() in Python that would read contents of the file “STUDENT.DAT” and display the details of
those students whose percentage is above 75. Also display number of students scoring above 75%.

________ pickle # line1


def countrec():
fobj=open("_____________","rb”) # line2
num = 0
try:
while ______: # line3
rec=pickle.load(fobj)
if rec[2]>75:
num = num + 1
print(rec[0],rec[1],rec[2])
except:
fobj.close()
return num

21. A binary file “salary.DAT” has structure [teacherid, teacher name, salary].
Complete the code in the blanks so that it would read contents of the file
“salary.DAT” and display the details of those teachers whose salary is above
20000.
import pickle
____________________________ # line1
try:
print("tr id\t tr Name\t tr Sal")
while True:
rec=___________.load(fobj) #line2
if rec[2]>_______: #line3
print(rec[0],"\t\t",rec[1],"\t\t",rec[2])
except:
____.close() #line 4

22. Mr.Rohan wants to modify salary of employee having a structure[eid,ename ,salary],but unable to fill
the gaps in the code. Help him to complete the code
import pickle
f = open('d:/student.dat','rb')
reclst = []
r=___________________________ # line 1 code to ask employee id
m=int(input(“enter correct salary”))
while True:
try:
rec = pickle.load(f)
reclst.append(rec) #line2 statement to add items in list at the end one by one
except EOFError:
break
f.close()
for i in range (len(reclst)):
if reclst[i]['eid']==r:
reclst[i]['salary'] = m
f = open('d:/student.dat','___') #line 3 mode to be used to copy the data
for x in reclst:
pickle.dump(x,f)
f.close()

23. A binary file sports.dat contains information in the following structure:( Event, Participant )
A code is shown below which is incomplete that would read contents from the sports.dat and creates
a file named Athletic.dat copying only those records from
sports.dat where the event name is “Athletics”.
import pickle
ath ( f1 , f2 ) :
l = pickle.load ( f1)
for t in l :
if ( t [ 0 ] == “________________” ) : #line 1
pickle.__________ ( t , f2 ) #line 2
f1 = open ( “ sports.dat “ , “ rb ” )
f2 = open ( “ athletics.dat “ , “ wb “ )
f.close()
f1.close()

24. A function searchprod( pc) in python is created to display the record of a particular
product from a file product.dat whose code is passed as an argument. Structure of
product contains the following elements [product code , product price].There is
some problem in completing the code,help to finish the code:
f = ________('d:/product.dat','rb') #line1
flag = False
pc=input(“Enter product code to be searched”)
while True:
try:
rec = pickle.load(f)
if rec['pcode'] ==_____: #line2
print('Product code:',rec['pcode'])
print('Price:',rec['price'])
flag = True
except EOFError:
break
if flag == False:
print('No Records found')
f.close()

You might also like