Previous Index Next

Review Questions - Binary File Handling

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


2. Write Python statements to open a binary file "student.dat" in both read & write mode.


3. Write Python statements to open a binary file "book.dat" in append and read mode.


4. Write Python statements to open a binary file "employee.dat" in write mode.


5. Write Python statements to open a binary file "teacher.dat" in read only mode.



6. Name the module used to serialize an object into a stream of bytes.


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


8. Write two methods of pickle module.


9. ________ method is used to convert (pickling) Python objects for writing data in a binary file.


10. ________ method is used to unpickle data to read from a binary file.


11. If the end of the file is already reached, the load function will raise an ________ exception.


12. Which of the following statement opens a binary file record.bin in write mode and writes data from a list L = [1,2,3,4] on the binary file?

a.  with open('record.bin','wb') as myfile:
    pickle.dump(L,myfile)

b.  with open('record.bin','wb') as myfile:
    pickle.dump(myfile,L)

c.  with open('record.bin','wb+') as myfile:
    pickle.dump(myfile,L)

d.  with open('record.bin','ab') as myfile:
    pickle.dump(myfile,L)


13. Read the following Python code carefully and answers the question given after the code.

import pickle
infile = open('phonebook.dat', '_____') #Line 1
phonebook = __________(infile) #Line 2
print(phonebook)
__________________ #Line 3

a. Fill in the blank in line 1 to open file in binary mode for reading.


b. Fill in the blank in line 2 to read object from file.


c. Fill in the blank in line 3 to close the file.


14. Read the following Python code carefully and answers the question given 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


15. Raghav is trying to write a tuple t = (1,2,3,4,5) on a binary file test.bin. Consider the following code written by him.

import pickle
t = (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,t)
b. dump(t, myfile)
c. write(t,myfile)
d. load(myfile,t)



Previous Index Next