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

Binary File Worksheet

Uploaded by

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

Binary File Worksheet

Uploaded by

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

QUESTION ANSWERS: SET 1

1 Which type of file does not have delimiters? 1


2 The process of converting the structure to a byte stream before writing to the file is 1
known as .

3 The process of converting byte stream back to the original structure is known as 1

4 Raman open a file in readmode, but the file doesn’t exist in the folder. 1
Python raised an error for the code. What type of error will be shown?
5 The prefix in front of a string makes it raw string that is no special meaning 1
attached to any character

6 Pickling is otherwise known as 1

7 CSV files are opened with argument to supress EOL translation 1


8 Which of the following statement is incorrect in the context of binary files? 1
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

9 What is EOFError? How can we handle EOFError in python 2

10 How text files and binary files are stored inside computer memory? 2

11 Name any two exceptions that occur while working with pickle module. 2

12 What is the difference between writer object’s writerow() and writerows() function? 2

13 Binary files are the best way to store program information. Discuss 3
QUESTION ANSWERS: SET 2
1 Write Python statements to open a binary file "student.dat" in both read & write 1
mode.

2 Which of the following statement is true? 1


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
3 Read the following Python code carefully and answers the question given afterthe 1
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
Fill in the blank in line 1 to open file in binary mode for append data to the file. Fill in
the blank in line 2 to pickle the list and write to file
4 Raghav is trying to write a tuple t = (1,2,3,4,5) on a binary file test.bin. 1
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)
5 Computers store every file as a collection of ……… 1
a)strings
b)Bytes
c)characters
d)object
e)none of the above
6 The default file-open mode is ……….. 1
a)r
b) a
c) w
d)w+
e)none of the above
7 Which of the following file mode open a file for reading and writing both in the binary 1
file?
a) r
b) rb
c) rb+
d) rwb
8 The file mode is used to handle binary file for reading. 1
a)rb
b)wb
c)r+
d)wb+
e)None of the above
9 Which of the following file mode opens a file for reading and writing both as well as 1
overwrite the existing file if the file exists otherwise creates a new file?
a) w
b) wb+
c) wb
d) rwb
10 module is used to store data into an python objects with their structure. 1
a)csv
b)pickle
c)os
d)numpy
11 What are the two types of data files? Give examples for each. 2
12 What is pickling and unpickling of data? 2
13 What is aBinary File? Give examples 2
14 How can you delete a file? Write a pseudocode to delete a file 2
15 Write a function to read name and roll no from a binary file. 2
16 Amritya Seth is a programmer, who has recently been given a task to write a python 5
code to perform the following binary file operations with the help of two user defined
functions/modules:

a. AddStudents() to create a binary file called STUDENT.DAT containing student


information – roll number, name and marks (out of 100) of each student.

b. GetStudents() to display the name and percentage of those students who have a
percentage greater than 75. In case there is no student having percentage > 75 the
function displays an appropriate message. The function should also display the average
percent. He has succeeded in writing partial code and has missed out certain
statements, so he has left certain queries in comment lines. You as an expert of Python
have to provide the missing statements and other related queries based on the
following code of Amritya Answer any four questions (out of five) from the below
mentioned questions.

import pickle
def AddStudents():
#1 statement to open the binary file to write data
while True:
Rno = int(input("Rno :"))
Name = input("Name : ")
Percent = float(input("Percent :"))
L = [Rno, Name, Percent]
#2 statement to write the list Linto the file
Choice = input("enter more (y/n): ")
if Choice in "nN":
break
F.close()
def GetStudents():
Total=0
Countrec=0
Countabove75=0
with open("STUDENT.DAT","rb") as F:
while True:
try:
#3 statement to readfrom the file
Countrec+=1
Total+=R[2]
if R[2] > 75:
print(R[1], " has percent =",R[2])
Countabove75+=1
except:
break
if Countabove75==0:
print("There is no student who has percentage more than 75")
average=Total/Countrec print("average percent of class = ",average)
AddStudents()
GetStudents()
1. Which of the following commands is used to open the file “STUDENT.DAT” for
writing only in binary format? (marked as #1 in the Python code)
a. F= open("STUDENT.DAT",'wb')
b. F= open("STUDENT.DAT",'w')
c. F= open("STUDENT.DAT",'wb+')
d. F= open("STUDENT.DAT",'w+')
2. Which of the following commands is used to write the list L into the binary file,
STUDENT.DAT? (marked as #2 in the Python code)
a. pickle.write(L,f)
b. pickle.write(f, L)
c. pickle.dump(L,F) d.
f=pickle.dump(L)
3. Which of the following commands is used to read each record from the binary file
STUDENT.DAT? (marked as #3 in the Python code)
a. R = pickle.load(F)
b. pickle.read(r,f)
c. r= pickle.read(f)
d. pickle.load(r,f)
4. Which of the following statement(s) are correct regarding the file access modes?
a. ‘r+’ opens a file for both reading and writing. File object points to its beginning.
b. ‘w+’ opens a file for both writing and reading. Adds at the end of the existing file if it
exists and creates a new one if it does not exist.
c. ‘wb’ opens a file for reading and writing in binary format. Overwrites the file if it
exists and creates a new one if it does not exist.
d. ‘a’ opens a file for appending. The file pointer is at the start of the file if the file
exists
5. Which of the following statements correctly explain the function of
seek() method?
a. tells the current position within the file.
b. determines if you can move the file position or not.
c. indicates that the next read or write occurs from that position in a file.
d. moves the current file position to a given specified position
17 Compare Text files, Binary Files and CSV files. 5

You might also like