Binary File MCQ Question Bank For Class 12

 

1. Out of the followings which mode is used for both reading and writing in binary format in file?

a) wb

b) wb+

c) w

d) w+

 

b) wb+

 

 

2. Which of the following is not true about binary files?

a) Binary files are store in terms of bytes

b) When you open binary file in text editor will show garbage values

c) Binary files represent ASCII value of characters

d) All of the above

c) Binary files represent ASCII value of characters

 

 

3. What is the difference between wb and wb+ mode?

a) wb mode is used to open binary file in write mode and wb+ mode open binary file both for read and write operation.

b) In wb mode file open in write mode and wb+ in read mode

c) File pointer is at beginning of file in wb mode and in wb+ at the end of file

d) No difference

a) wb mode is used to open binary file in write mode and wb+ mode open binary file both for read and write operation.

 

 

 

4. The pickle module in Python is used for:

a) Serializing any Python object structure

b) De-serializing Python object structure

c) Both a and b

d) None of these

c) Both a and b

 

 

5. Which method is used to convert Python objects for writing data in binary file?

a) write()

b) load()

c) store()

d) dump()

 

d) dump()

 

 

6. seek() function is used for _______.

a) positions the file object at the specified location.

b) It returns the current position of the file object

c) It writes the data in binary file

d) None of these

 

a) positions the file object at the specified location.

 

 

7. Which is not the valid mode for binary files?

a) r

b) rb

c) wb

d) wb+

 

a) r

 

 

8. Which of the following function is used to read the data in binary file?

a) read()

b) open()

c) dump()

d) load()

d) load()

 

9. Suresh wants to open the binary file student.dat in read mode. He writes the following statement but he does not know the mode. Help him to find the same.

F=open(‘student.dat’, ____)

a) r

b) rb

c) w

d) wb

 

b) rb

 

 

10. This method returns an integer that specifies the current position of the file object.

a) seek()

b) load()

c) position()

d) tell()

 

d) tell()

 

 

11. What is pickling?

a) It is the process to read binary file

b) It is the process to position the file pointer

c) It is a process by which a Python object is converted to a byte stream

d) None of these

 

c) It is a process by which a Python object is converted to a byte stream

 

 

Case Study Based Question-1

Mr. Zack Sullivan loves programming. He joined an institute for learning. He is learning python. He learned all the python concepts like strings, lists, tuple , dictionaries etc. but he wants to learn file handling in python. He is trying to learn binary file handling. His teacher gave him partial code to write and read data from employee.dat having structure empno, name, salary. Help Zack to complete the code:

___________________ # statement 1

def addrecords():

fw= _____________ #statement 2

dict={}

ch=’y’

while ch==’y’:

eno=int(input(“enter employee number”))

nm= input(“enter employee name”)

sal=int(input(“enter employee salary”))

dict={‘empno’:eno,’name’:nm,’salary’:sal}

____________________ # statement 3

ch=input(“add more record”)

fw.close()

# function to diplay records

def display():

dict={}

fr= _____________ # statement 4

dict=____________ # statement 5

fr.close()

print(“data :”,dict)

 

Answer questions 12-16 based on above case study

12. Help Zack to import the module to perform binary file operation in statement 1.

a) csv

b) random

c) pickle

d) file

 

c) pickle

 

 

13. Which statement is used from the following for statement 2 to open the binary file in write mode?

a) open(“employee.dat”,’w’)

b) open(“employee.dat”,’wb’)

c) open(“employee.dat”,’w+’)

d) open(“employee.dat”,’r’)

 

b) open(“employee.dat”,’wb’)

 

 

14. Which statement is used from the following for statement 3 to write dictionary data created in above code, namely dict, is written in binary file employee.dat file?

a) pickle.dump(dict,fw)

b) pickle.write(dict,fw)

c) pickle.save(dict,fw)

d) pickle.store(dict)

 

a) pickle.dump(dict,fw)

 

 

15. Which statement is used from the following for statement 4 to open the binary file in read mode?

a) open(“employee.dat”,’r’)

b) open(“employee.dat”,’r+’)

c) open(“employee.dat”,’a’)

d) open(“employee.dat”,’rb’)

 

d) open(“employee.dat”,’rb’)

 

 

16. Compelete statement 5 to read data in dictionary namely dict from the opened binary file?

a) dict=pk.read(fr)

b) dict=pickle.load(fr)

c) pickle.load(dict,fr)

d) none of these

 

b) dict=pickle.load(fr)

 

 

Case Study Based Question-2

Now Mr. Zack has given the following code to modify the records of employees from employee.dat used in above code. He has to increase Rs. 2000 in the salary of those who are getting less than 15000. Mr. Zack has to find the records and change the salary in place. His teacher gave him partial code. Help him to complete the code.

import pickle as pk

found=False

emp={}

fin = ___________ #1 statement : open file both in read write mode

# read from file

try:

while true:

pos= _______ #2 store file pointer position before reading record

emp=_______ #3 to read the record in emp dictionary

if emp[‘salary’]<15000:

emp[‘salary’]+=10000

_________ #4 place file pointer at exact location of record

pickle.dump(emp,fin)

found=True

except EOFError:

if found==False:

print(“record not found”)

else:

print(“successfully updated”)

fin.close()

 

17. In #1 statement open the file in read and write mode. Which statement is used out of the followings?

a) open(“employee.dat”,’rb+’)

b) open(“employee.dat”,’r+’)

c) open(“employee.dat”,’a’)

d) open(“employee.dat”,’rb’)

 

a) open(“employee.dat”,’rb+’)

 

 

18. Choose the appropriate statement to complete #2 statement to store file pointer position before reading record.

a) pk.seek(pos)

b) fin.tell()

c) pk.position()

d) pk.tell()

 

b) fin.tell()

 

 

19. Choose the appropriate statement to complete #3 statement to read record in emp dictionary.

a) pk.read(fin)

b) pickle.load(fin,emp)

c) pk.dump(emp)

d) pk.load(fin)

 

d) pk.load(fin)

 

 

20. Choose the appropriate statement to complete #4 statement to place file pointer at exact location of record

a) fin.seek(pos)

b) pos=fin.seek()

c) fin.position()

d) none of the above

 

b) pos=fin.seek()

 

 

#TRENDING

Text File MCQs for Class 12 Term 1

CSV File MCQs for Class 12 Term 1

 

Binary file: basic operations on a binary file: open using file open modes (rb, rb+, wb, wb+, ab, ab+), close a binary file

21. If a file is opened for reading, which of the following statements is not true?

a) The file must exist on the disk on the specified path

b) If the file exists at the specified path, the file is successfully opened.

c) The file even if at a different location on disk other than the specified path, will get opened

d) Python gives error if the file does not exist at the specified path

 

c) The file even if at a different location on disk other than the specified path, will get opened

 

 

22. To read 24 characters from a file object infi, we use

a) Infi.read()

b) infi.read(24)

c) Infi.readline()

d) infi.readlines

b) infi.read(24)

 

 

23. The readlines() method returns__________

a) a str

b) a list of integers

c) a lit of single characters

d) a list of lines

d) a list of lines

 

 

24. Which of the following is not a valid mode to open a file.

a) ab

b) rw

c) wb

d) w+

b) rw

 

 

25. Which of the following functions do you use to write data in the binary format?

a) Write()

b) output()

c) dump()

d) send()

c) dump()

 

 

26. Which of the following command is used to open a file “c:\path.txt” in read mode only?

a) Fin=open(“c\path.txt”,”r”)

b) fin=open(“c\\path.txt”,”r”)

c) Fin=open(file=”c:\path.txt”,”r+”)

d) fin=open(file=”c\\path.txt”,”r+”)

b) fin=open(“c\\path.txt”,”r”)

 

 

27. Which of the following is not a correct statement for binary files?

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‟

d) Every line ends with new line character „\n‟

 

 

28. Which of the following commands can be used to read the entire contents of a file as a string using the file object <tmpfile>?

a) tmpfile.read(n)

b) tmpfile.read()

c) tmpfile.readline()

d) tmpfile.readlines()

b) tmpfile.read()

 

 

29. Which of the following command is used to open a file “c:\temp.txt” for writing in binary format only?

a) outfile = open(“c:\temp.txt”, “w”)

b) outfile = open(“c:\\temp.txt”, “wb”)

c) outfile = open(“c:\temp.txt”, “w+”)

d) outfile = open(“c:\\temp.txt”, “wb+”)

b) outfile = open(“c:\\temp.txt”, “wb”)

 

 

30. Trying to open a binary file using a text editor will show:

a) Garbage values

b) ASCII values

c) Binary character

d) Unicodes

a) Garbage values

 

 

CASE STUDY QUESTIONS (R)

31. Ms. Suman is working on a binary file and wants to write data from a list to a binary file. Consider list object as l1, binary file suman_list.dat, and file object as f.

(i) Which of the following can be the correct statement for her?

a) f = open(“suman_list”,”wb”); pickle.dump(l1,f)

b) f = open(“suman_list”,”rb”); l1=pickle.dump(f)

c) f = open(“suman_list”,”wb”); pickle.load(l1,f)

d) f = open(“suman_list”,”rb”); l1=pickle.load(f)

a) f = open(“suman_list”,”wb”); pickle.dump(l1,f)

 

 

(ii ) Which option will be correct for reading file for suman?

a) f = open(„suman_list‟,‟rb‟)

b) f = open(„suman_list‟,‟r‟)

c) f = open(„suman_list‟,‟r+‟)

d) f = open(„suman_list‟,‟ab‟)

a) f = open(„suman_list‟,‟rb‟)

 

 

(iii) In which of the file mode existing data will be intact in binary file?

a) a

b) ab

c) w

d) wb

b) ab

 

 

(iv ) Which one of the following is correct statement?

a) import – pickle

b) pickle import

c) import pickle

d) All of the above

c) import pickle

 

 

(v) What are the binary files used for?

a) It is used to store data in the form of bytes.

b) To store data

c) To look folder good

d) None of these

a) It is used to store data in the form of bytes.

 

 

32. Ms. Sejal is working on the sports.dat file but she is confused about how to complete the code to read the data from the binary file. Suggest a suitable line for her to fulfil her.

____________ # Statement 1

def sports_read ():

f1 = _______________ # Statement 2

_________________ # Statement 3

print(data)

f1. close ()

sports.read()

 

(i) Identify the suitable code for blank space in line marked as Statement-1.

a) pickle import

b) import pickle

c) import.pickle

d) None of these Correct

b) import pickle

 

 

(ii) Identify the suitable code for blank space in line marked as Statement-2.

a) f1 =open(“sports.dat”,”wb”)

b) f1 =open(“sports.dat”,”r”)

c) f1 =open(“sports.dat”,”rb”)

d) None of these Correct

c) f1 =open(“sports.dat”,”rb”)

 

(iii) Identify the suitable code for blank space in line marked as Statement-3.

a) data = pickle.load(f1)

b) data = pickle.dump(f1)

c) data = pickle.load(f)

d) data = pickle.dump(f)

a) data = pickle.load(f1)

 

 

(iv) What is the description of ‘r+b’ in binary mode?

a) read and write

b) write and read

c) read only

d) none of these

a) read and write

 

 

(v) Which of the following file modes will not delete the existing data in binary file ?

a) wb

b) w

c) a

d) ab

d) ab

 

 

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

 

(i) To open the file for writing the data in line marked as Statement-1.

a) bfile=open(“empfile.dat”,”ab”)

b) bfile=open(“empfile.dat”,”a”)

c) bfile=open(“empfile.dat”,”wb”)

d) bfile=open(“empfile.dat”,”w”)

a) bfile=open(“empfile.dat”,”ab”)

 

 

(ii) To accept employee name from the user in line marked as Statement-2.

a) input(“\tEmployee Name : “)

b) input(Employee Name 🙂

c) input(“Employee Name )

d) None of these

a) input(“\tEmployee Name : “)

 

 

(iii) Identify the suitable code for blank space in line marked as Statement-3.

a) edata,bfile()

b) edata,bfile

c) data,bfile

d) edata,file

b) edata,bfile

 

 

(iv) Identify the suitable code for blank space in line marked as Statement-4.

a) bfile.close()

b) bfile.close

c) file.close()

d) none of these

a) bfile.close()

 

 

(v) Which of the following is the correct syntax to read from a file using load function ?

a) pickle.load(<filehandle>)

b) <object> – load.pickle(<filehandle>)

c) <object> – pickle.load(<filehandle>)

d) All of the above

c) <object> – pickle.load(<filehandle>)

 

 

34. 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()

 

(i) Identify the suitable code for blank space in line marked as Statement-1.

a) csv

b) CSV

c) pickle

d) PICKLE

c) pickle

 

 

(ii) Identify the suitable code for blank space in line marked as Statement-2.

a) wb

b) ab

c) w

d) a

b) ab

 

 

(iii) select correct statement to write data into file for Statement-3.

a) pickle.dump(record,file)

b) pickle.dump(record)

c) pickle.dump(file,record)

d) pickle.load(record,file)

a) pickle.dump(record,file)

 

 

(iv) Which method is used for object deserialization ?

a) Pickling

b) Unpickling

c) All of the above

d) None of the above

b) Unpickling

 

 

(v)What is the last action that must be performed on a file? 

a) save

b) close

c) end

d) write

b) close

 

 

 

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

 

(i) Identify the suitable code for blank space in line-1.

a) import

b) IMPORT

c) Import

d) None of the above

a) import

 

 

(ii) Identify the suitable code for blank space in line 2.

a) STUDENT.DAT

b) STUDENTS.DAT

c) SCHOOL.DAT

d) None of the above

a) STUDENT.DAT

 

 

(iii) select correct keyword to fill for line-3.

a) True

b) False

c) true

d) TRUE

a) True

 

 

36. Ms.Anita is unable understand what can be the output of the following code.Help her in getting the output.

Import pickle

L=[20,40,50]

f=open(“list.dat”,‟wb‟)

Pickle.dump(l,f)

Print(“Data added successfully”)

f.close()

f=open(“list.dat”,‟rb‟)

data=pickle.load(f)

f.close()

print(data)

 

a) Data added successfully

[20,40,50]

b) [20,30,50]

Data added successfully

c) [20,30,50]

d) No output

a) Data added successfully

[20,40,50]

 

 

 

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

 

(i) To open the file for writing the data in line marked as line-1.

a) fobj=open(“salary.dat”,”rb”)

b) fobj=open(“salary.dat”,”r”)

c) fobj=open(“salary.dat”,”r+”)

d) fobj=open(“data.dat”,”rb”)

a) fobj=open(“salary.dat”,”rb”)

 

 

(ii) The module used in line2

a) PICKLE

b) pickling

c) pickle

d) None of these

c) pickle

 

 

(iii) Identify the salary to be checked in the code marked as line-3.

a) 50000

b) 20000

c) 24000

d) 10000

b) 20000

 

 

(iv) Which of the following File Modes creates a new file, if the file does not exist? (choose one/more)

a) “r”

b) “bw”

c) “w”

d) “a”

c) “w”

 

 

(v) What is true about Binary files

a) They are not human readable

b) the file extension is .dat

c) the file stores same format as held in memory.

d) All of the above

d) All of the above

 

 

38. 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()

 

(i) Identify the code in line1.

a) int(input(“Enter employee id”))

b) int(“Enter employee id”)

c) int(INPUT(“Enter employee id”))

d) None of the above

a) int(input(“Enter employee id”))

 

 

(ii) The module used in line2

a) PICKLE

b) pickling

c) pickle

d) None of these

c) pickle

 

 

(iii) Fill in the code marked as line-3.

a) w

b) wb

c) r

d) rb

b) wb

 

 

39. 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()

 

(i) Identify the code in line1.

a) Athletics

b) Sports

c) Games

d) None of the above

a) Athletics

 

 

(ii) The function to copy the data into other binary file2

a) DUMP

b) close

c) dump

d) None of these

c) dump

 

 

(iii) Information stored on a storage device with a specific name is called as __________.

a) array

b) dictionary

c) file

d) tuple

c) file

 

 

iv) Which of the follwong is not a valid mode to open a file?

a) ab

b) rw

c) r+

d) w+

b) rw

 

 

40. 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()

 

(i) Identify the method in line1.

a) close

b) open

c) OPEN

d) None of the above

b) open

 

 

(ii) The variable used to accept product code entered by the user for the line2

a) pcode

b) pc

c) code

d) None of these

b) pc

 

 

import pickle module, dump() and load() method, read, write/create, search, append and update operations in a binary file

 

41.  _____________ is the process of converting Python object hierarchy into a byte stream so that it can be written into a file.

a) Pickling

b) Unpickling

c) Dumping

d) Loading

a) Pickling

 

 

42. _______ is the process of reading from a binary file

a) Pickling

b) Unpickling

c) Dumping

d) Loading

b) Unpickling

 

 

43. ___________ of pickle module will unpickle the data coming from the binary file.

a) load()

b) dump()

c) writer()

d) insert()

a) load()

 

 

44. ___________ of pickle module will pickle the data in the binary file.

a) load()

b) dump()

c) writer()

d) insert()

b) dump()

 

 

45. _______________ will return the current position of file pointer in the file

a) seek()

b) search()

c) tell()

d) print()

c) tell()

 

 

46. ________ places the file pointer at the specified position in the open file.

a) seek()

b) search()

c) tell()

d) print()

a) seek()

 

 

47. F.seek(20,0) will move the file pointer 20 bytes in forward direction from beginning of file. State True or False

a) True

b) False

a) True

 

 

48. F1.seek(-5,1) will move the file pointer 5 bytes backwards from end of file. State True or False

a) True

b) False

b) False

 

 

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

a) 0

 

 

50.Which of the following statements 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

b) pickling is used for object serialization

 

 

CASE STUDY QUESTIONS (R)

 

51. Archit wants to create and display a Binary file named “Myfile.DAT”. Complete the missing code to open, create and display the file.

import __________ #Line1

double=[]

for i in range(1,11):

double.append(2*i)

fo=__________________ #Line2

pickle.________________ #Line 3

fo.close()

fin=___________________ #Line4

result=________________ #Line 5

fin.close()

print(” The content of file :”, result)

 

(i) Name the module he should import in Line 1.

a) csv

b) pickle

c) binary

d) bin

b) pickle

 

 

(ii)Fill in the blank in Line 2 to open the file for writing the contents of the file.

a) open(“Myfile.dat”,”w”)

b) open(“Myfile.dat”,”r”)

c) open(“Myfile.dat”,”wb”)

d) open(“Myfile.dat”,”rb”)

c) open(“Myfile.dat”,”wb”)

 

 

(iii) Fill in the blank in Line 3 with the function to write entire contents to file.

a) load(double,fo)

b) dump(double,fo)

c) writer(double)

d) insert(double,fo)

b) dump(double,fo)

 

 

(iv) Fill in the blank in Line 4 to open the file for displaying contents of file.

a) open(“Myfile.dat”,”w”)

b) open(“Myfile.dat”,”r”)

c) open(“Myfile.dat”,”wb”)

d) open(“Myfile.dat”,”rb”)

d) open(“Myfile.dat”,”rb”)

 

 

(v) Fill in the blank in Line 5 read the contents of the file.

a) pickle.read(fin)

b) pickle.readline(fin)

c) pickle.readlines(fin)

d) pickle.load(fin)

d) pickle.load(fin)

 

 

52. Rohit has been given the following incomplete code for entering his details(Name,contact number and address) to a file “Personal.DAT” and display the contents. Complete the missing code to open, create and display the file.

import __________ #Line1

mydata=[]

name=input(“Enter Name:”)

contactno=int(input(“Enter contact number:”))

address=input(“Enter address:”)

mydata=[name,contactno,address]

f1=__________________ #Line2

pickle.________________ #Line 3

f1.close()

f2=___________________ #Line4

result=________________ #Line 5

f2.close()

print(” The content of file :”, result)

 

(i) Name the module he should import in Line 1.

a) csv

b) pickle

c) binary

d) bin

b) pickle

 

 

(ii) Fill in the blank in Line 2 to open the file for writing the contents of the file.

a) open(“Personal.dat”,”w”)

b) open(“Personal.dat”,”r”)

c) open(“Personal.dat”,”wb”)

d) open(“Personal.dat”,”rb”)

c) open(“Personal.dat”,”wb”)

 

 

(iii)Fill in the blank in Line 3 with the function to write entire contents to file.

a) load(mydata,f1)

b) dump(mydata,f1)

c) writer(mydata)

d) insert(mydata,f1)

b) dump(mydata,f1)

 

 

(iv) Fill in the blank in Line 4 to open the file for displaying contents of file.

a) open(“Personal.dat”,”w”)

b) open(“Personal.dat”,”r”)

c) open(“Personal.dat”,”wb”)

d) open(“Personal.dat”,”rb”)

d) open(“Personal.dat”,”rb”)

 

 

(v) Fill in the blank in Line 5 read the contents of the file.

a) pickle.read(f2)

b) pickle.readline(f2)

c) pickle.readlines(f2)

d) pickle.load(f2)

d) pickle.load(f2)

 

 

 

53. You are provided with some incomplete code for entering student‟s details (Rollno, Name and marks) to a file “Student.DAT” and display the contents. Complete the missing code to open, create and display the file.

import __________ #Line1

data=[]

rollno=int(input(“Enter Roll number:”))

name=input(“Enter Name:”)

marks=int(input(“Enter mark:”))

data=[rollno,name,marks]

fout= open(“Student.dat”,”wb”)

pickle.________________ #Line 2

fout.close()

fin=___________________ #Line3

output=________________ #Line 4

fin.close()

if ____________>=33: #Line5

print(output[1],” passed”)

else:

print(output[1],” failed”)

 

(i) Name the module to import in Line 1.

a) csv

b) pickle

c) binary

d) bin

b) pickle

 

 

(ii) Fill in the blank in Line 2 with the function to write entire contents to file.

a) load(data,fout)

b) dump(data,fout)

c) writer(data)

d) insert(data,fout)

b) dump(data,fout)

 

 

(iii) Fill in the blank in Line 3 to open the file for displaying contents of file.

a) open(“Student.dat”,”w”)

b) open(“Student.dat”,”r”)

c) open(“Student.dat”,”wb”)

d) open(“Student.dat”,”rb”)

d) open(“Student.dat”,”rb”)

 

 

(iv) Fill in the blank in Line 4 read the contents of the file.

a) pickle.read(fin)

b) pickle.readline(fin)

c) pickle.readlines(fin)

d) pickle.load(fin)

d) pickle.load(fin)

 

 

(v) Fill in the blank in Line 5 to display he status of students(passed/failed) based on their mark.

a) Output[0]

b) Output[1]

c) Output[2]

d) output

c) Output[2]

 

 

54. Ritesh wants to perform the following binary file operations, as a part of his assignment, with the help of two user defined functions/modules:

  1. AddEmp() to create a binary file called Employee.DAT containing employee information – employee number, name and salary.
  2. ViewEmp() to display the name and salary of employees who are getting Rs.50000 above as salary. The function should also display the average salary.

Help him in filling incomplete code.

import pickle

def AddEmp():

#Line1 to open the binary file to write data

while True:

Empno = int(input(“Employee number: :”))

Name = input(“Name : “)

Salary = int(input(“Enter Salary :”))

L = [Empno, Name, Salary]

#Line2 to write the list L into the file

Choice = input(“enter more (y/n): “)

if Choice in “nN”:

break

F.close()

def ViewEmp():

Total=0

Countrec=0

C50K=0

F=open(“Employee.DAT”,”rb”)

while True:

try:

#Line3 to read from the file

Countrec+=1

Total+=R[2]

if _______ > 50000: #Line4

print(R[1],”has salary “,R[2])

C50K+=1

except:

break

if C50K==0:

print(“No employee with salary more than 50000”)

average=______________ #Line5 to find average salary

print(“average Salary = “,average)

AddEmp()

ViewEmp()

 

(i) Write statement #Line1, to open the file “Employee.DAT” for writing only in binary format?

a) F= open(“Employee.DAT”,’wb’)

b) F= open(“Employee.DAT”,’w’)

c) F= open(“Employee.DAT”,’wb+’)

d) F= open(“Employee.DAT”,’w+’)

a) F= open(“Employee.DAT”,’wb’)

 

 

(ii) Write statement, #Line2, to write the list L into the binary file, Employee.DAT?

a) pickle.write(L,f)

b) pickle.write(f, L)

c) pickle.dump(L,F)

d) f=pickle.dump(L)

c) pickle.dump(L,F)

 

 

(iii) Write statement, #Line3, to read each record from the binary file Employee.DAT?

a) R = pickle.load(F)

b) pickle.read(r,f)

c) r= pickle.read(f)

d) pickle.load(r,f)

a) R = pickle.load(F)

 

 

(iv) Write statement , #Line4, to find employees who are getting salary more than Rs.50000.

a) R[0]

b) R[1]

c) R[2]

d) R[3]

c) R[2]

 

 

(v) Write statement , #Line5, to find average salary of employees

a) Total/countrec

b) Total/C50K

c) Total/Countrec

d) average(Total)

c) Total/Countrec

 

 

55. Anand, a software developer, is asked to help a librarian to find some details of books in his library. The book information is stored in a binary file Books.DAT. Create two user defined functions/modules:

  1. AddBook() to create a binary file called Books.DAT containing Book information – Book name, Author and Price.
  2. ViewBook() to display the name, Author and price of books which are more than Rs.350 in price. The function should also display the average price of books.

Try to fill the incomplete code to get required information for librarian.

import pickle

def AddBook():

#Line1 to open the binary file to write data

while True:

Name = input(“Book Name : “)

Author= input(“Author Name : “)

Price = int(input(“Enter Book Price :”))

Lst= [Name, Author, Price]

#Line2 to write the list Lst into the file

Choice = input(“enter more (y/n): “)

if Choice in “nN”:

break

Fp.close()

def ViewBook():

Total=0

Count=0

C=0

F=open(________________) #Line3 to open file for reading

while True:

try:

#Line4 to read from the file

Count+=1

Total+=Row[2]

if _______ > 350: #Line5

print(“Price of “,Row[1],”= “,Row[2])

C+=1

except:

break

if C==0:

print(“All books are having price less than 350”)

avgprice=Total/Count

print(“Average Price = “,avgprice)

F.close()

AddBook()

ViewBook()

 

(i) Fill in the blank in Line1, to open the file “Books.DAT” for writing in binary format?

a) Fp= open(“Books.DAT”,’w’)

b) Fp= open(“Books.DAT”,’wb’)

c) Fp= open(“Books.DAT”,’wb+’)

d) Fp= open(“Books.DAT”,’w+’)

b) Fp= open(“Books.DAT”,’wb’)

 

 

(ii) Fill in the blank in Line2, to write the list Lst into the binary file, Books.DAT?

a) pickle.write(Lst,fp)

b) pickle.write(fp, Lst)

c) pickle.dump(Lst,Fp)

d) fp=pickle.dump(Lst)

c) pickle.dump(Lst,Fp)

 

 

(iii) Fill in the blank in Line3, to open the file “Books.DAT” for reading

a) F= open(“Books.DAT”,’r’)

b) F= open(“Books.DAT”,’r+’)

c) F= open(“Books.DAT”,’wb+’)

d) F= open(“Books.DAT”,’rb’)

d) F= open(“Books.DAT”,’rb’)

 

 

(iv) Fill in the blank in Line4, to read data from file.

a) Row=pickle.load(Fp)

b) Row=pickle.read(Fp)

c) Row=pickle.read(F)

d) Row=pickle.load(F)

d) Row=pickle.load(F)

 

 

(v) Fill in the blank in Line5 with suitable expression:

a) Row[0]

b) Row[1]

c) Row[2]

d) Row[3]

c) Row[2]

 

 

56. John, a student of class 12 student wants to complete a search() function to search in a pickled file Competition.dat.

  • File contains details of prizes in a competition [Rollno,name, prize] format.
  • File contains details of 10 participants‟ details

Arun has to complete the code and print details of prize 1.

def search():

f = open(“Competition.dat”, _______) # Line -1

______________: # Line -2

while True:

rec = pickle.____________# Line -3

if(_____________): #Line -4

print(rec)

except:

pass

_________________ # Line -5

 

(i) Fill in the blank in Line1, to open the file “Competition.DAT” for reading in binary format?

a) w

b) r

c) wb

d) rb

d) rb

 

 

(ii) Fill in the blank in Line2, to handle exceptions in statements

a) except

b) try

c) handle

d) statement

b) try

 

 

(iii) Fill in the blank in Line3, to read the records.

a) read(f)

b) readline(f)

c) load(f)

d) load()

c) load(f)

 

 

(iv) Fill in the blank in Line4, to read record from file.

a) rec[0]==1

b) rec[2]==1

c) rec[prize]==1

d) rec[“prize”]==1

b) rec[2]==1

 

 

(v) Fill in the blank in Line5 to close the file pointer:

a) f.end()

b) f.close()

c) f=close()

d) close(f)

b) f.close()

 

 

57. A binary file “Book.DAT” has structure [Bookno, Book_Name, Author, Price].

Ravi, a student of class 12 Computer Science is told to create the file and search for the number of books of a specific author by completing the blank lines.

import pickle

def createFile():

fobj=_______________ #Line1 to open file for entering data to file

BookNo=int(input(“Book Number : “))

Book_name=input(“Name :”)

Author = input(“Author: “)

Price = int(input(“Price : “))

rec=[BookNo,Book_Name,Author,Price]

_________________ #Line2 to store data to file

fobj.close()

def CountRec(Author):

fobj=_______________ #Line3 to open file for searching

num= 0

try:

while True:

rec=_____________ # Line4 to read a record

if Author==______: #Line5 to check for specific author

num = num + 1

except:

fobj.close()

return num

 

(i) Fill in the blank in Line1, to open the file “Books.DAT” for writing in binary format?

a) open(“Book.dat”,”ab”)

b) open(“Books.DAT”,’wb’)

c) open(“Books.DAT”,’a’)

d) open(“Books.DAT”,’w+’)

a) open(“Book.dat”,”ab”)

 

 

(ii) Fill in the blank in Line2, to write the list rec into the binary file, Books.DAT?

a) pickle.write(rec)

b) pickle.dump(rec,fobj)

c) pickle.dump(fobj,rec)

d) fobj=pickle.dump(rec)

b) pickle.dump(rec,fobj)

 

 

(iii) Fill in the blank in Line3, to open the file “Books.DAT” for searching.

a) open(“Book.dat”,”r”)

b) open(“Book.dat”,”rb”)

c) open(“Book.dat”,”wb”)

d) open(“Book.dat”,”ab”)

b) open(“Book.dat”,”rb”)

 

 

(iv) Fill in the blank in Line4, to read record from file.

a) pickle.read(f)

b) pickle.load(f)

c) pickle.load(fobj)

d) pickle.read(fobj)

c) pickle.load(fobj)

 

 

(v) Fill in the blank in Line5 with suitable expression:

a) rec[0]

b) rec[1]

c) rec[2]

d) rec[3]

c) rec[2]

 

 

58. Neha, a software developer is asked to complete a search() function to search in a pickled file Book.DAT.

  • File contains details of Books [Bookno,Bname, Price] format.
  • File contains details of 10 Books‟ details

Neha wants to complete the code and print details of Book with Book number 123.

def search():

fp= open(“Book.dat”, _______) # Line -1

______________: # Line -2

while True:

R = pickle.____________# Line -3

if(_____________): #Line -4

print(R)

except:

pass

_________________ # Line -5

 

(i) Fill in the blank in Line1, to open the file “Book.DAT” for reading in binary format?

a) w

b) r

c) wb

d) rb

d) rb

 

 

(ii) Fill in the blank in Line2, to handle exceptions in statements

a) except

b) try

c) handle

d) statement

b) try

 

 

(iii) Fill in the blank in Line3, to read the records.

a) read(fp)

b) readline(fp)

c) load(fp)

d) load()

c) load(fp)

 

 

(iv) Fill in the blank in Line4, to read record from file.

a) R[0]==123

b) R[2]==123

c) R[Bookno]==123

d) R[“Bookno”]==123

b) R[2]==123

 

 

(v) Fill in the blank in Line5 to close the file pointer:

a) fp.end()

b) fp.close()

c) fp=close()

d) close(fp)

b) fp.close()

 

 

59. Neha, a class 12 student is asked to write a binary file, “Admission.DAT”, for entering new admission student details and review the details when required. She faced some problems in statements and see if you can fill the missing code.

import pickle

def newadmission():

admnlst=[]

fobj=open(“Admission.dat”,”___”) #Line1 to open file for entering data to file

while True:

AdmnNo=int(input(“Admission Number : “))

Stud_name=input(“Name :”)

Fathername = input(“Father’s name: “)

Phone = int(input(“Phone No : “))

rec=[AdmnNo,Stud_Name,Fathername,Phone]

admnlst.append(______) #Line2 to add a record

choice=input(“Enter more y/n”)

if choice in “Nn”:

break

_________________ #Line3 to store data to file

fobj.close()

def getRecords(AdmNo):

fobj=_______________ #Line4 to open file for searching

result=pickle.load(fobj)

for rec in result:

if rec[0]==______: #Line5 to check for given admission number

print(rec)

fobj.close()

 

(i) Identify missing code in Line1 so that file can add more information

a) w

b) r

c) wb

d) rb

c) wb

 

 

(ii) Identify missing object in Line2

a) fobj

b) R

c) rec

d) admission

c) rec

 

 

(iii) Fill in the necessary function in Line3, to input data to binary file.

a) pickle.dump(“admnlst”,”fobj”)

b) pickle.dump(admnlst,fobj)

c) pickle.dump(“fobj”,“admnlst”)

d) pickle.dump(fobj,admnlst)

b) pickle.dump(admnlst,fobj)

 

 

(iv) Fill in the blank in Line4, to read data from file.

a) open(“Admission.dat”,”read”)

b) open(“Admission.dat”,”r”)

c) open(“Admission.dat”,”rd”)

d) open(“Admission.dat”,”rb”)

d) open(“Admission.dat”,”rb”)

 

 

(v) Fill in the blank in Line5 to check for given admission number:

a) AdmnNo

b) “AdmnNo”

c) AdmNo

d) “AdmNo”

c) AdmNo

 

 

60. Ananya, a class 12 student is asked to write a binary file, “Fees.DAT”, for entering fee details of students and review the details when required. She faced some problems in statements and see if you can fill the missing code.

import pickle

def feeEntry():

Feelst=[]

fobj=open(“Fees.dat”,”___”) #Line1 to open file for entering data to file

while True:

AdmnNo=int(input(“Admission Number : “))

Stud_name=input(“Name :”)

Class = input(“Enter Class: “)

Fee = int(input(“Enter Fee : “))

rec=[AdmnNo,Stud_Name,Class,Fee]

Feelst.append(______) #Line2 to add a record

choice=input(“Enter more y/n”)

if choice in “Nn”:

break

_________________ #Line3 to store data to file

fobj.close()

def getRecords(AdmNo):

fobj=_______________ #Line4 to open file for searching

result=pickle.load(fobj)

for rec in result:

if rec[0]==______: #Line5 to check for given admission number

print(rec)

fobj.close()

 

(i) Identify missing code in Line1 so that file can add more information

a) w

b) r

c) wb

d) rb

c) wb

 

 

(ii) Identify missing object in Line2

a) fobj

b) R

c) rec

d) admission

c) rec

 

 

(iii) Fill in the necessary function in Line3, to input data to binary file.

a) pickle.dump(“Feelst”,”fobj”)

b) pickle.dump(Feelst,fobj)

c) pickle.dump(“fobj”,“Feelst”)

d) pickle.dump(fobj,Feelst)

b) pickle.dump(Feelst,fobj)

 

 

(iv) Fill in the blank in Line4, to read data from file.

a) open(“Fees.dat”,”read”)

b) open(“Fees.dat”,”r”)

c) open(“Fees.dat”,”rd”)

d) open(“Fees.dat”,”rb”)

d) open(“Fees.dat”,”rb”)

 

 

(v) Fill in the blank in Line5 to check for given admission number:

a) AdmnNo

b) “AdmnNo”

c) AdmNo

d) “AdmNo”

c) AdmNo

 

Copywrite © 2020-2024, CBSE Python,
All Rights Reserved
error: Content is protected !!