MCQs Streams and Writers Advanced Python Programming

MCQs Streams and Writers

1. Can you write to file using “r+” along with file name

  • TRUE
  • FALSE

Ans: TRUE

2. what does “w+” instruction do with respect to file

  • It specify that we write in to the file
  • It specify we can write and read to file
  • It specify that we can only write into a file
  • It Specify we can’t read but only write to file

Ans: It specify we can write and read to file

3. Opened file without closing if executed what will be the output.

  • No output
  • Compile time error
  • Execute successfully
  • None of the above

Ans: Execute successfully

4. Once opened fill is closed can any other operation can be done on it or not

  • only read() can be done
  • only writing operation can be done
  • None of the operation can done once file is closed
  • Other operations than opening and closing can be done.

Ans: None of the operation can done once file is closed

5. What does seek(0,0) method do?

  • Moves the cursor to starting point
  • Moves the cursor to ending line
  • scans all information in the fill
  • Prints all the information in the fill

Ans: Moves the cursor to starting point

6. The readlines() method returns:

  • str
  • a list of lines
  • a list of single characters
  • a list of integers

Ans: a list of lines

7. To read the remaining lines of the file from a file object infile, we use:

  • infile.read(2)
  • infile.read()
  • infile.readline()
  • infile.readlines()

Ans: infile.readlines()

8. Which of the following statements are true?

  • When you open a file for reading, if the file does not exist, an error occurs
  • When you open a file for writing, if the file does not exist, a new file is created
  • When you open a file for writing, if the file exists, the existing file is overwritten with the new file
  • All of the mentioned

Ans: All of the mentioned

9. In file handling, what does this terms means  “r, a”?

  • read, append
  • append, read
  • rewrite,append
  • restore,append

Ans: read, append

10. Which function is used to write all the characters?

  • write()
  • writecharacters()
  • writeall()
  • writechar()

Ans: write()

11. class Store: def __init__(self,a,b,c): self.name=a self.contact=b self.address=c def display(self): print “Store Name:%s\nAddress:%s\nContact number:%s”%(self.name,self.address,self.contact) print l1=[] l=[] l=’store.txt’.read().split(“\n”) for i in l: li=i.split(“,”) e=Store(li[0],li[1],li[2]) l1.append(e) for i in l1: i.display() What will the above program do?

  • This program is used to read and write in a file
  • This program is used to read only
  • This program is used to write in to the file only
  • You cann’t access this file

Ans: You cann’t access this file

12. what will this program do?

class Store:
def __init__(self,a,b,c):
self.name=a
self.contact=b
self.address=c

 def display(self):
print "Store Name:%s\nAddress:%s\nContact number:%s"%(self.name,self.address,self.contact)
print
 

l1=[]
l=[]
f=open('store.txt','w+')
l=f.read().split("\n")
for i in l:
li=i.split(",")
e=Store(li[0],li[1],li[2])
l1.append(e)
for i in l1:
i.display()
  • This program can perform writing lines to a file and reading from a file
  • This program is used to read only
  • This is used only to write to a file
  • None of the above

Ans: This program can perform writing lines to a file and reading from a file

13. class Store: def __init__(self,a,d,b,c,e): self.name=a self.type1=d self.idi=b self.cost=float(c) self.no=int(e) def display(self): print”\nName :”,self,a print “Type:”,self.type1 print”Item ID:”,self.idi print”Quantity”,self.no print”Cost:”,self.cost class storeBO: def __init__(self): self.lis=[] self.d=dict() self.t=0 self.l1=[] def addToList(self): self.f=open(‘item.txt’,’r+’) self.f.seek(0,2) for i in l: self.f.write(i) self.f.seek(0,0) self.f1=self.f.readlines() #print self.f1 for i in self.f1: #print i self.k=i.split(“,”) #print self.k self.it=Store(self.k[0],self.k[1],self.k[2],self.k[3],self.k[4]) self.l1=self.addItemtolist(self.it) self.f.close() def addItemtolist(self,u): self.l1.append(u) self.d[u.type1]=0 return self.l1 def display(self): for i in self.l1: #print self.d.get(self.d[i.type1]) if self.d.get(self.d[i.type1])!=None: self.d[i.type1]==i.no else: self.t=self.d[i.type1] self.t+=i.no self.d[i.type1]=self.t #print self.d for i in self.d: print “\nItemType :”,i print “Number of elements:”,self.d.get(i) l=[] a=StoreBO() n=input(“Enter number od store details to be added\n”) for i in range(n): m=raw_input() l.append(“\n”) l.append(m) a.addToList() a.display()

  • This code only writes to the file
  • this code only reads the file
  • Output file consists only Input file
  • Output file consists of input file details along with writen information

Ans: Output file consists only Input file

14. what will this program do?

class Store:
def __init__(self,a,d,b,c,e):
self.name=a
self.type1=d
self.idi=b
self.cost=float(c)
self.no=int(e)

 def display(self):
print"\nName :",self,a
print "Type:",self.type1
print"Item ID:",self.idi
print"Quantity",self.no
print"Cost:",self.cost

class storeBO:
def __init__(self):
self.lis=[]
self.d=dict()
self.t=0
self.l1=[]

 def addToList(self,l):
self.f=open('item.txt','r+')
self.f.seek(0,2)
for i in l:
self.f.write(i)
self.f.seek(0,0)
self.f1=self.f.readlines()
#print self.f1
for i in self.f1:
#print i
self.k=i.split(",")
#print self.k
self.it=Store(self.k[0],self.k[1],self.k[2],self.k[3],self.k[4])
self.l1=self.addItemtolist(self.it)
self.f.close()

 def addItemtolist(self,u):
self.l1.append(u)
self.d[u.type1]=0
return self.l1

 def display(self):
for i in self.d:
print "\nItemType :",i
print "Number of elements:",self.d.get(i)
l=[]
a=StoreBO()
n=input("Enter number od store details to be added\n")
for i in range(n):
m=raw_input()
l.append("\n")
l.append(m)
a.addToList(l)
a.display()
  • The write() method will write to a different file
  • The Write method will write in the file before reading
  • It will read the content of the file and write the extra infromation after that in the same file
  • It will write all information to the file before reading

Ans: It will write all information to the file before reading

15. why do you us seek(0,2) in the above code?

class Store:
def __init__(self,a,d,b,c,e):
self.name=a
self.type1=d
self.idi=b
self.cost=float(c)
self.no=int(e)

 def display(self):
print"\nName :",self,a
print "Type:",self.type1
print"Item ID:",self.idi
print"Quantity",self.no
print"Cost:",self.cost

class storeBO:
def __init__(self):
self.lis=[]
self.d=dict()
self.t=0
self.l1=[]

 def addToList(self,l):
self.f=open('item.txt','r+')
self.f.seek(0,2)
for i in l:
self.f.write(i)
self.f.seek(0,0)
self.f1=self.f.readlines()
#print self.f1
for i in self.f1:
#print i
self.k=i.split(",")
#print self.k
self.it=Store(self.k[0],self.k[1],self.k[2],self.k[3],self.k[4])
self.l1=self.addItemtolist(self.it)
self.f.close()

 def addItemtolist(self,u):
self.l1.append(u)
self.d[u.type1]=0
return self.l1

 def display(self):
for i in self.l1:
#print self.d.get(self.d[i.type1])
if self.d.get(self.d[i.type1])!=None:
self.d[i.type1]==i.no
else:
self.t=self.d[i.type1]
self.t+=i.no
self.d[i.type1]=self.t
#print self.d
for i in self.d:
print "\nItemType :",i
print "Number of elements:",self.d.get(i)
l=[]
a=StoreBO()
n=input("Enter number od store details to be added\n")
for i in range(n):
m=raw_input()
l.append("\n")
l.append(m)
a.addToList(l)
a.display()
  • Seek(0,2) makes the cursor to move to end of the file
  • Seek(0,2) makes the cursor to move to starting point of the file
  • Seek(0,2) is used to filter file contents
  • Seek(0,2) this is used to seek for perticular word or object in file

Ans: Seek(0,2) makes the cursor to move to end of the file

16. What does this program specify?

class Store:
def __init__(self,a,d,b,c,e):
self.name=a
self.type1=d
self.idi=b
self.cost=float(c)
self.no=int(e)

 def display(self):
print"\nName :",self,a
print "Type:",self.type1
print"Item ID:",self.idi
print"Quantity",self.no
print"Cost:",self.cost

class storeBO:
def __init__(self):
self.lis=[]
self.d=dict()
self.t=0
self.l1=[]

 def addToList(self,l):
self.f=open('store.txt','r')
self.f1=self.f.readlines()
for i in self.f1:
self.k=i.split(",") self.it=Store(self.k[0],self.k[1],self.k[2],self.k[3],self.k[4])
self.l1=self.addItemtolist(self.it)
self.f.close()

 self.f11=open('storew.txt',w)
self.f.seek(0,2)
for i in l:
self.f11.write(i)
for i in self.l1:
self.f11.write(i)
self.f.seek(0,0)
def addItemtolist(self,u):
self.l1.append(u)
self.d[u.type1]=0
return self.l1

 def display(self):
for i in self.d:
print "\nItemType :",i
print "Number of elements:",self.d.get(i)
l=[]
a=StoreBO()
n=input("Enter number od store details to be added\n")
for i in range(n):
m=raw_input()
l.append("\n")
l.append(m)
a.addToList(l)
a.display()
  • In this program all the content is read from one file and store in another file
  • Both reading and writing is done with respect to same file
  • only reading operation done
  • Only writing opertion is done

Ans: In this program all the content is read from one file and store in another file

17. which of the following can be done?

class Store:
 def __init__(self,a,d,b,c,e):
 self.name=a
 self.type1=d
 self.idi=b
 self.cost=float(c)
 self.no=int(e)

 def display(self):
 print"\nName :",self,a
 print "Type:",self.type1
 print"Item ID:",self.idi
 print"Quantity",self.no
 print"Cost:",self.cost

class storeBO:
 def __init__(self):
 self.lis=[]
 self.d=dict()
 self.t=0
 self.l1=[]

 def addToList(self,l):
 self.f=open('store.txt','r')
 self.f1=self.f.readlines()
 for i in self.f1:
 self.k=i.split(",") self.it=Store(self.k[0],self.k[1],self.k[2],self.k[3],self.k[4])
 self.l1=self.addItemtolist(self.it)
 self.f.close()
 
 self.f11=open('storew.txt',w) 
 self.f.seek(0,2)
 for i in l:
 self.f11.write(i)
 for i in self.l1: 
self.f11.write(i) 
self.f.seek(0,0) 
 def addItemtolist(self,u):
 self.l1.append(u)
 self.d[u.type1]=0
 return self.l1

 def display(self):
 for i in self.d:
 print "\nItemType :",i
 print "Number of elements:",self.d.get(i)
l=[]
a=StoreBO()
n=input("Enter number od store details to be added\n")
for i in range(n):
 m=raw_input()
 l.append("\n")
 l.append(m)
a.addToList(l)
a.display()
  • Can’t write in store file
  • Can’t read from storew file
  • none of the file used can be used for both perpose
  • All the above

Ans: All the above

18. Which of the following correct option?

class Store:
 def __init__(self,a,b,c):
 self.name=a
 self.contact=b
 self.address=c
 
 def display(self):
 print "Store Name:%s\nAddress:%s\nContact number:%s"%(self.name,self.address,self.contact)
 print
 
 
l1=[]
l=[]
f=open('store.txt','w')
 l=f.read().split("\n")
for i in l:
 li=i.split(",")
 e=Store(li[0],li[1],li[2])
 l1.append(e)
for i in l1:
 i.display()
  • mode specifed is for reading and writing the file
  • mode specified is only for writing hence writing is not posible
  • mode of accessing is to read the writen data to the file
  • none of the above

Ans: mode specified is only for writing hence writing is not posible

19. class Store: def __init__(self,a,b,c): self.name=a self.contact=b self.address=c def display(self): print “Store Name:%s\nAddress:%s\nContact number:%s”%(self.name,self.address,self.contact) print l=[] li=[] for i n range(n): l= raw_input().split(“,”) e=Store(l[0],l[1],l[2]) for i in li: li=’store.txt’.write(i)

  • executes successfully
  • complie time error
  • Writes all the content in a new file
  • Writes in store.txt file only

Ans: complie time error

20. which of the following code for this program is correct?

class Store: def __init__(self,a,d,b,c,e): self.name=a self.type1=d self.idi=b self.cost=float(c) self.no=int(e) def display(self): print"\nName :",self,a print "Type:",self.type1 print"Item ID:",self.idi print"Quantity",self.no print"Cost:",self.cost class storeBO: def __init__(self): self.lis=[] self.d=dict() self.t=0 self.l1=[] def addToList(self): self.f=open('item.txt','r+') self.f.seek(0,2) for i in l: self.f.write(i) self.f.seek(0,0) self.f1=self.f.readlines() #print self.f1 for i in self.f1: #print i self.k=i.split(",") #print self.k self.it=Store(self.k[0],self.k[1],self.k[2],self.k[3],self.k[4]) self.l1=self.addItemtolist(self.it) self.f.close() def addItemtolist(self,u): self.l1.append(u) self.d[u.type1]=0 return self.l1 def display(self): for i in self.l1: #print self.d.get(self.d[i.type1]) if self.d.get(self.d[i.type1])!=None: self.d[i.type1]==i.no else: self.t=self.d[i.type1] self.t+=i.no self.d[i.type1]=self.t #print self.d for i in self.d: print "\nItemType :",i print "Number of elements:",self.d.get(i) l=[] a=StoreBO() n=input("Enter number od store details to be added\n") for i in range(n): m=raw_input() l.append("\n") l.append(m) a.addToList() a.display()
  • The output file is different file
  • The output file is same file in which data is stired in the beginng
  • Output is stored in the same file with difffernet name
  • None of the above

Ans: The output file is same file in which data is stired in the beginng

Statement: StoreKeeper Govinda provisional stores was handled by Mr.Govind after many years due to the profit in the business he started another provisional store and due to high marketing he couldn’t handle all the pressure by himself so he requested help from him old grandfather but since he was aged he could not note down the items going out and add-ons hence Govind decided to ask a software Developer to develop an application in which it should note down the changes done or handle the store data base stored in form of a file. Create a class ‘Stores’ with following attributes.  Data type attribute String name String type String id Float cost Integer number This class include following method  Method Description display()

This method is used to print the details of the store items.  Create another class ‘StoreBO’ with following methods  Method Description addToList()

This method is used to add the Store class object into list which can be used latter display()

This method is used to display the items number along with item details.  In this program you need to store all item information in file ‘store.txt’Note:Sample Input-Output:123498231 Cost: 75.00 Number: 15 Item Name: Vessels Item type: Utensils Item id: 234567890

21. Which of following package is need to be import before used a file?

  • import sys
  • import data
  • import files
  • None of the mentioned

Ans: None of the mentioned

22. Read the problem description carefully and answer the question below:

The correct way of reading data from a file ‘store.txt'(contents in the file are separated with a comma)? 

1. self.f=open(‘store.txt’,’r’)
self.f1=self.f.readlines()
for i in self.f1:
self.k=i.split(“,”)
self.f.close()

2. self.f=open(‘storeW.txt’,’r’)
self.f1=self.f.readlines()
for i in self.f1:
self.k=i.split(“,”)
self.f.close()

3. self.f=open(‘store.txt’,’w’)
self.f1=self.f.readlines()
for i in self.f1:
self.k=i.split(“,”)
self.f.close()

4. self.f=’store.txt’,’r’
self.f1=self.f.readlines()
for i in self.f1:
self.k=i.split(“,”)
self.f.close()

Ans: self.f=open(‘store.txt’,’r’)
self.f1=self.f.readlines()
for i in self.f1:
self.k=i.split(“,”)
self.f.close()

23. Read the problem description carefully and answer the question below:

The correct way of initializing an object for writing into a file ‘storew.txt’? 

1. self.f11=open(‘storew.txt’,’r’)
self.f.seek(0,2)
for i in l:
self.f11.write(i)
for i in self.l1:
self.f11.write(i)
self.f.seek(0,0)

2. self.f11=open(‘storew.txt’,’w’)
self.f.seek(0,2)
for i in l:
self.f11.write(i)
for i in self.l1:
self.f11.write(i)
self.f.seek(0,0)

3. self.f11=open(‘storew.txt’,w)
self.f.seek(0,0)
for i in l:
self.f11.write(i)
for i in self.l1:
self.f11.write(i)
self.f.seek(0,2)

4. self.f11=open(‘storew.txt’,’w+’)
self.f.seek(0,2)
for i in l:
self.f11.read(i)
for i in self.l1:
self.f11.write(i)
self.f.seek(0,0)

Ans: self.f11=open(‘storew.txt’,’w’)
self.f.seek(0,2)
for i in l:
self.f11.write(i)
for i in self.l1:
self.f11.write(i)
self.f.seek(0,0)

24. Correct way of reading data from a file ignoring ‘\n’ and ‘\r’?

  • self.f=open’store.txt’,’rU’) self.f1=self.f.readlines() for i in self.f1: self.k=i.split(“,”) self.f.close()
  • self.f=open’storeW.txt’,’r+’) self.f1=self.f.readlines() for i in self.f1: self.k=i.split(“,”) self.f.close()
  • self.f=open’store.txt’,’w+’) self.f1=self.f.readlines() for i in self.f1: self.k=i.split(“,”) self.f.close()
  • self.f=’store.txt’,’r’ self.f1=self.f.readlines() for i in self.f1: self.k=i.split(“,”) self.f.close()

Ans: self.f=open’storeW.txt’,’r+’) self.f1=self.f.readlines() for i in self.f1: self.k=i.split(“,”) self.f.close()

25. What will happen if read() is used instead of readlines()?

  • reads only words
  • reads only one line
  • reads one line and waits for next line prompt
  • reads all the file content and store it in a list

Ans: reads only one line

26. Read the problem description carefully and answer the below question:
Govind when tries to check the available stoke in the store he tries to read the data from the file store please help him in doing that.

def addToList(self,l):
self.f=__(1)__(‘store.txt’,’__(2)__’)
self.f1=self.f.____(3)____()
_____________ is the method used for opening the file for reading purpose(Fill the First Blank) 

Ans: open

27. Read the problem description carefully and answer the below question:
Govind when tries to check the available stoke in the store he tries to read the data from the file store please help him in doing that.

def addToList(self,l):
self.f=__(1)__(‘store.txt’,’__(2)__’)
self.f1=self.f.____(3)____()
_____________ instruction is used to represent file is used for reading data from a file(Fill the Second Blank)

Ans: r

28. Read the problem description carefully and answer the below question:
Govind when tries to check the available stoke in the store he tries to read the data from the file store please help him in doing that.

def addToList(self,l):
self.f=__(1)__(‘store.txt’,’__(2)__’)
self.f1=self.f.____(3)____()
_____________ is the method used for reading n number of lines from a file for reading purpose(Fill the Third Blank) 

Ans: readlines

29. Read the problem description carefully and answer the below question:

After checking the availability of stock in the store Govind order for insufficient goods and write the arrived stock details in to the file.

self.f11=open(‘storew.txt’,’___(1)___’)
self.f.___(2)___
for i in l:
self.f11.____(3)____
for i in self.l1:
self.f11.____(4)____
self.f.___(5)___
self.f.___(6)___
Instruction required to represent opened file is used for writing purpose only _____________(Fill the First Blank) 

Ans: w

30. Read the problem description carefully and answer the below question:

After checking the availability of stock in the store Govind order for insufficient goods and write the arrived stock details in to the file.

self.f11=open(‘storew.txt’,’___(1)___’)
self.f.___(2)___
for i in l:
self.f11.____(3)____
for i in self.l1:
self.f11.____(4)____
self.f.___(5)___
self.f.___(6)___
_____________ is the method used to bring the cursor to the end position of the file(Fill the Second Blank)

Ans: seek(0,2)

31. Read the problem description carefully and answer the below question: After checking the availability of stock in the store Govind order for insufficient goods and write the arrived stock details in to the file. self.f11=open(‘storew.txt’,’___(1)___’) self.f.___(2)___ for i in l: self.f11.____(3)____ for i in self.l1: self.f11.____(4)____ self.f.___(5)___ self.f.___(6)___ _____________method used for writing data into file.(Fill the Third Blank)

Ans: write(i)

32. Read the problem description carefully and answer the below question:
After checking the availability of stock in the store Govind order for insufficient goods and write the arrived stock details in to the file.

self.f11=open(‘storew.txt’,’___(1)___’)
self.f.___(2)___
for i in l:
self.f11.____(3)____
for i in self.l1:
self.f11.____(4)____
self.f.___(5)___
self.f.___(6)___
_____________method used for writing data into file.(Fill the Fourth Blank)

Ans: write(i)

33. Read the problem description carefully and answer the below question: After checking the availability of stock in the store Govind order for insufficient goods and write the arrived stock details in to the file. self.f11=open(‘storew.txt’,’___(1)___’) self.f.___(2)___ for i in l: self.f11.____(3)____ for i in self.l1: self.f11.____(4)____ self.f.___(5)___ self.f.___(6)___ _____________is the method used to bring the cursor to the starting position of the file(Fill the Fifth Blank)

Ans: seek(0,0)

34. Read the problem description carefully and answer the below question:

After checking the availability of stock in the store Govind order for insufficient goods and write the arrived stock details in to the file.
self.f11=open(‘storew.txt’,’___(1)___’)
self.f.___(2)___
for i in l:
self.f11.____(3)____
for i in self.l1:
self.f11.____(4)____
self.f.___(5)___
self.f.___(6)___
_____________ method is used for closing the opened file.(Fill the Sixth Blank)

Ans: close()

35. Read the problem description carefully and answer the below question:

Since Govind is reading data from a file it will be in String format we have to convert and use where ever we want different data type.

class Store:
def __init__(self,a,d,b,c,e):
self.name=a
self.type1=d
self.idi=b
self.cost=___(1)___
self.no=___(2)___
def display(self):
print”\nName :”,self,a
print “Type:”,self.type1
print”Item ID:”,self.idi
print”Quantity”,self.no
print”Cost:”,self.cost
_____________ is used for changing data type from string to float .(Fill the First blank)

Ans: float

36. Read the problem description carefully and answer the below question: Since Govind is reading data from a file it will be in String format we have to convert and use where ever we want different data type. class Store: def __init__(self,a,d,b,c,e): self.name=a self.type1=d self.idi=b self.cost=___(1)___ self.no=__(2)____ def display(self): print”\nName :”,self,a print “Type:”,self.type1 print”Item ID:”,self.idi print”Quantity”,self.no print”Cost:”,self.cost _____________ is used for changing data type from string to integer. (Fill the second blank)

Ans: int

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *