0% found this document useful (0 votes)
95 views

Binary File Handling Question Bank

Uploaded by

mdharsit765
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
95 views

Binary File Handling Question Bank

Uploaded by

mdharsit765
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Q.No.

1 mark MCQs Marks


1. _____________ module is used for serializing and de- serializing any 1
python object structure.
a) unpickle
b) pickle
c) random
d) math
2. Which statement will return error? 1
import pickle statement-1
f=open(“data.dat”,’rb’) statement-2
d=pickle.load(f) statement-3
f.end() statement-4
a) statement-1
b) statement-2
c) statement-3
d) statement-4
3 Ananya jotted down a few features of the "append mode”. Help her to 1
identify the valid features
a) If we open an existing file in append mode, the previous data will
remain there
b) In append mode the file object will be positioned at the end of the
file
c) In append mode if file does not Exist then the new file will be
created
d) All of the above
4. Raghu is trying to write an object o1 = (1,2,3,4,5) on a binary file 1
"test.bin". Consider the following code written by him.
import pickle
o1= (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.o1)
(c) write(o1, myfile)
(b) dump(ol, myfile)
(d) load(myfile,o1 )
5. Which of the following is not a correct statement for binary files? 1
a. Easy for carrying data into buffer
b. Much faster than other file systems
c. Characters translation is not required
d. Every line ends with new line character ‘\n’
Assertion Reasoning Questions
Question based on ASSERTION AND REASONING. Mark the correct
choice as
(a) Both A and R are true and R is the correct explanation for A
(b)Both A and R are true and R is not the correct explanation for A
(c) A is True but R is False
(d)A is false but R is True
6. Assertion (A): A binary file stores the data in the same way as stored in 1
the memory.
Reasoning(R): Binary file in python does not have line delimiter
7. Assertion (A): Python pickle is used to serialize and de-serialize a python 1
object structure. Any object on python can be pickled so that it can be
saved on disk.
Reasoning(R): Pickling is the process whereby a Python object hierarchy
is converted into a byte stream.
8. Assertion (A): an open file can be close using close () function. 1
Reason(R): sometimes the data written onto files is held in memory until
the file is closed.
VSA 2 Mark Question
9. Read the following Python code carefully and answers the question given 2
after the code
import pickle
#open file in binary mode for writing.
with open('emp.dat', '____') as outfile: #Line 1
#Store data in list
employee = [101,'Simran',20000]
_________________ #Line 2
a. Fill in the blank in line 1 to open file in binary mode for append
data to the file.
b. Fill in the blank in line 2 to pickle the list and write to file

10. Write difference between serialization and de-serialization in binary file 2


handling?
LA- 5 Marks Questions
11. i. How are text files different from binary files? (2+3)
ii. 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.
12. (i) Differentiate between r+ and w+ file modes in Python. (2+3)
(ii) 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.

13. a. i) Explain the process of reading and writing data to a binary file in 5
Python.
ii) Write the difference between a and a+ mode?

b. Ramesh wants to add a record of employees working in his company


with a python program, so Define a function add_employee() that allows
the user to enter details for an employee and writes them to a binary file
employees.dat and to search record define a function
search_employee(emp_id) that takes an Employee ID as input and
displays the details of the employee with that ID. If the employee is not
found, display an appropriate message

14. a. What do you mean by seek () and tell ()? (1+4)


b. A training institute wanted to make record of students Write a
Python program to perform the following tasks:
1. **Write Data**: Define a function `write_data()` that prompts
the user to enter details for 5 students and write them to the
`students.dat` file.
2. **Read Data**: Define a function `read_data()` that reads the
content of the `students.dat` file and displays it on the screen.

15. a. When do you think text files should be preferred over binary files? (2+3)
b. A binary file “student.dat” has structure [rollno, name, marks].
Write a user defined function insertRec() to input data for a
student and add to student.dat.
16. Consider a binary file stock.dat that has the following data: OrderId, (2+3)
MedicineName, Qty and Price of all the medicines of wellness medicos,
write the following functions:
a) AddOrder() that can input all the medicine orders.
b) DisplayPrice() to display the details of all the medicine that have Price
more than 10.
17. a. Binary file is unreadable and open and close through a function only so (2+3)
what are the advantages of using binary file.
b. Write a user-defined function named SumSalary(Post) that will accept
an argument the post of employees & read the contents of emp.dat and
calculate the sum of salary of all employees of that Post.

You might also like