File Handling
File Handling
File Handling
Q7. Assume that you are writing in a text file; now you want Knowledge
to identify where the cursor is currently at in the
file. Which function will you use for the same?
(A) seek()
(B) hide()
(C) tell()
(D) type()
Q8. Consider the statements given below: Evaluation
Fileobj = open(“Report.txt”, ‘r’)
Fileobj.seek(25,0)
Choose the statement that best explains the second
statement.
(A) It will place the pointer at 25 bytes ahead of the
current file pointer position.
(B) It will place the pointer at 25 bytes behind from the
end-of file.
(C) It will place the pointer at 25 bytes from the
beginning of the file.
(D) It returns 25 characters from the 0th index.
Q9. To read 4th line from text file, which of the following Knowledge
statement is true?
(A) dt = f.readlines();print(dt[3])
(B) dt=f.read(4) ;print(dt[3])
(C) dt=f.readline(4);print(dt[3])
(D) All of these
(A) 10
(B) 8
(C) 9
(D) 20
(A) write()
(B) writecharacters()
(C) writeall()
(D) writechar()
(A) No difference.
(B) In r+ mode, the pointer is initially placed at the
beginning of the file and for w+, the pointer is placed at
the end.
(C) In w+ mode, the pointer is initially placed at the
beginning of the file and for r+, the pointer is placed at
the end.
(D) Depends on the operating system.
Q21 Which of the following not a valid file extension for file
student?
A. student.txt
B. student.cvc
C. student.dat
D. student.pdf
Q25 Which file can open in any text editor and is in human
readable form?
A. Binary files
B. Text files
C. Data files
D. d. Video files
Q30 Trying to open a binary file using a text editor will show:
A. Garbage values
B. ASCII values
C. Binary character
D. Unicodes
Q31 Choose the correct syntax to open a file in write mode. KNOWLEDGE AND
(A) f.open(“file.txt”,’w’) UNDERSTANDING
(B) f = open(“file.txt”)
(C) f = open(“file.txt”,’w’)
(D) f = open(“file.txt”, ‘r’)
Q32 Choose the correct syntax to open a file in append mode. KNOWLEDGE AND
(A) with open(“file.txt”, “a”) as f : UNDERSTANDING
(B) with open(“file.txt”) as “f”
(C) with open(“file.txt”, “a”) as f
(D) with open(“file.txt”, “w”) as f :
Q33 Which of the following is not a valid file access mode? KNOWLEDGE AND
(A) a+ UNDERSTANDING
(B) r
(C) w
(D) b+
Q34 Choose the correct syntax to close a file. KNOWLEDGE AND
(A) f.closed UNDERSTANDING
(B) f.close()
(C) f.closed()
(D) f.close
Q35 Which method is used to write multiple strings/lines in a KNOWLEDGE AND
text file? UNDERSTANDING
(A) write()
(B) writeline()
(C) writelines()
(D) writesingle()
Q36 Which method is used to write a single string/line in a KNOWLEDGE AND
text file? UNDERSTANDING
(A) write()
(B) writeline()
(C) writelines()
(D) writesingle()
Q37 Which of the following statements is correct? KNOWLEDGE AND
(A) In r+ mode, a file opens for both reading and writing UNDERSTANDING
if it already exists. The file pointer placed at the
beginning of the file.
(B) In w+ mode, a file opens for both writing and
reading. Overwrites the existing file if the file exists. If
the file does not exist, create a new file for reading and
writing.
(C) Both of the above
(D) None of the above
Q38 By default, in which mode a file open. KNOWLEDGE AND
(A) r UNDERSTANDING
(B) w
(C) a
(D) None of the above
Q39 While using an open function in which mode a text file APPLICATION
will be created, if it does not exist already.
(A) r+
(B) w
(C) Both of the above
(D) None of the above
Q40 While using an open function in which mode a text file APPLICATION
will not be created, if it does not exist already.
(A) r+
(B) r
(C) Both of the above
(D) None of the above
Q41 Which of the following iterable objects can be passed to APPLICATION
the writelines() method for writing multiple lines/strings
in a text file.
(A) list
(B) tuple
(C) Both of the above
(D) None of the above
Q42 Which of the following is not a correct statement? APPLICATION
(A) write and append modes opens file for writing.
(B) In both modes file pointer for existing file sets to
beginning of file, when opened.
(C) In append mode existing data do not get overwritten,
whereas in writing mode existing data gets overwritten.
(D) By adding ‘+’ symbol both can be able to open files
in reading mode also.
Q43 A user opened a file using code : f = open(“story.txt”,’r’) ANALYSIS,
But the file “story.txt” does not get opened. Find the EVALUATION AND
appropriate reason for not opening the file. CREATION
(A) There is a syntax error in the code.
(B) File pointer name is not correct.
(C) User need to include the absolute path of file location.
(D) File does not exist.
Q44 Consider the code: ANALYSIS,
lines = ['This is line 1', 'This is line 2'] EVALUATION AND
with open('readme.txt', 'w') as f: CREATION
f.writelines(lines)
Which of the following statements is true regarding the
content of the file ‘readme.txt’?
(A) Both strings of list lines will be written in two
different lines in the file ‘readme.txt’.
(B) Both strings of list lines will be written in the same
line in the file ‘readme.txt’.
(C) string 1 of list lines will overwrite string 2 of list lines
in the file ‘readme.txt’.
(D) string 2 of list lines will overwrite string 1 of list lines
in the file ‘readme.txt’.
Q45 Consider the code: ANALYSIS,
lines = ['This is line 1', 'This is line 2'] EVALUATION AND
with open('readme.txt', 'w') as f: CREATION
for line in lines:
f.write(line+’\n’)
Which of the following statements is true regarding the
content of the file ‘readme.txt’?
(A) Both strings of list lines will be written in two
different lines in the file ‘readme.txt’.
(B) Both strings of list lines will be written in the same
line in the file ‘readme.txt’.
(C) string 1 of list lines will overwrite string 2 of list lines
in the file ‘readme.txt’.
(D) string 2 of list lines will overwrite string 1 of list lines
in the file ‘readme.txt’.
Q46 What are the file types in Python?
(A) Text File Understanding
(B) TSV File
(C) CSV File
(D) All of the above
Q47. Which command is used to open a text file in Python in Analysis
reading mode?
(A) F = open( )
(B) F = open(“Data.Txt”,”w”)
(C) F = open(“Data.Txt,”r”)
(D) All of the Above
Q48 What is the use of File Object? Knowledge
(A) File Object Serves as a link to a file
(B) File Objects are used to read and write data to the file
(C) A file Object is a reference to a file on disk
(D) All of the above
Q49 Which is the default mode to open a file? Knowledge
(A) read mode
(B) r+ mode
(C) write mode
(D) w+ mode
Q50 Which mode creates a new file, when open file ? Understanding
(A) w+ mode
(B) a mode
(C) a+ mode
(D) All of the above
Q51 Which function is used to read the contents from a file? Knowledge
(A) read ( )
(B) write ( )
(C) close ( )
(D) None of these
Q52 Which of the following command is used to open a file Application
“C:\\Data.txt for writing as well as reading mode ?
(A) f = open(“c:\\pat.txt”,”w”)
(B) f = open(“c:\\pat.txt”,”wb”)
(C) f = open(“c:\\pat.txt”,”w+”)
(D) f = open(“c:\\pat.txt”,”wb+”)
Q53 Which function is used to write a list of strings in a file? Understanding
(A) writeline ( )
(B) writelines ( )
(C) writelist ( )
(D) writefulline ( )
Q54 When open a file in append mode, by default new data Analysis
can be added at the?
(A) Beginning of the file
(B) End of the file
(C) At the middle of the file
(D) All of the above
Q55. Find the output of the following code – Application
fp = open("sample.txt", "r")
fp.read(8)
print(fp.tell())
fp.close()
(A) 0
(B) 7
(C) 8
(D) 9
Q56 What will be the output of the following code if content application
of the file “smile.txt” is: Smiling is infectious,
You catch it like the flu.
When someone smiled at me today,
I started smiling too.
file=open(“smile.txt”)
contents=file. read()
print(file. read (7))
(A) Smiling
(B) Smilin
(C) ng too.
(D) No output
Q57 If the content of the file “wish.txt” is – “Happy”, then Application
what will be the content of the file after executing the
following statements –
f=open (“wish.txt”, ‘w’)
f. write(“Birthday”)
f. close()?
(A) Happy Birthday
(B) HappyBirthday
(C) Happy
(D) Birthday
Q58 Consider a code: Creation
Fileobj = open("story.txt", 'a')
x= 200
a= Fileobj.write(x)
Fileobj.close()
What is the output of the above given code?
(A) Generates an error : x must be string .
(B) It will write 200 at the end of the file.
(C) It will write 200 at the beginning of the file.
(D)It will delete the existing content and write 200 in the
beginning.
Q63 The _____ file mode is used when user want to read and UNDERSTANDING
write data into Text file. KNOWLEDGE
(A) rb
(B) b
(C) r+
(D) w
Q64 The _____ file mode is used when user want to write and UNDERSTANDING
read data into Text file. KNOWLEDGE
(A) rb
(B) w+
(C) r+
(D) w
Q65 The _____ file mode is used when user want to append UNDERSTANDING
data into Text file. KNOWLEDGE
(A) rb
(B) w+
(C) r+
(D) a
Q67 The correct syntax to read from Text file is__________ UNDERSTANDING
(A) <filehandle>.read() KNOWLEDGE
(B) read.(filehandle) ANALYSIS
(C) read(filehanle).txt
(D) None of the above
Q68 The correct syntax to reads a line of input from Text file UNDERSTANDING
is__________ KNOWLEDGE
(A) <filehandle>.read() ANALYSIS
(B) readline.(filehandle)
(C) read(filehanle).txt
(D) <filehandle>.readline()
Q69 The correct syntax to reads all lines of input from Text UNDERSTANDING
file and returns them in a list is __________ KNOWLEDGE
(A) <filehandle>.readlines() ANALYSIS
(B) readline.(filehandle)
(C) read(filehanle).txt
(D) <filehandle>.readline()
Q70 In which file, delimiters are used for line and translations UNDERSTANDING
occur? KNOWLEDGE
(A) Text file ANALYSIS
(B) Binary file
(C) Both are correct
(D) None of the above
Q71 observe the following code and answer the follow UNDERSTANDING
f1=open("mydata","a") KNOWLEDGE
______#blank1 ANALYSIS
f1.close()
what type of file is mydata?
(A) Text file
(B) Binary file
(C) Both are correct
(D) None of the above
Q72 observe the following code and answer the follow UNDERSTANDING
f1=open("mydata","a") KNOWLEDGE
______#blank1 ANALYSIS
f1.close()
Fill in the blank1 with correct statement to write "abc"
in the file "mydata"
(A) f1.write(“abc”)
(B) write(abc)
(C) write.f1(abc)
(D) None of the above
Q73 In which of the following file modes the existing data of UNDERSTANDING
the file will not be lost? KNOWLEDGE
(A) ab and a+b mode ANALYSIS
(B) r mode
(C) w mode
(D) None of the above
Q74 To take file pointer to nth character with respect to r UNDERSTANDING
position in seek method___ KNOWLEDGE
(A) f1.seek(r) ANALYSIS
(B) f1.seek(n)
(C) f1.seek(n,r)
(D) seek(n,r).f1
(a) fobj.read(2)
(b) fobj.read()
(c) fobj.readline()
(d) fobj.readlines()
Q77 Which statement is used to change the file position Understanding
to an offset value from the start?
(a) fp.seek(offset, 0)
(b) fp.seek(offset, 1)
(c) fp.seek(offset, 2)
(d) None of the above
Q78 Which statement is used to retrieve the current Knowledge
position within the file?
(a) fp.seek()
(b) fp.tell()
(c) fp.loc
(d) fp.pos
Q79 What happens if no arguments are passed to the seek() Knowledge
method?
(a) file position is set to the start of file
(b) file position is set to the end of file
(c) file position remains unchanged
(d) results in an error
Q80
Which function is used to read single line from file? Knowledge
(a) readline()
(b) readlines()
(c) readstatement( )
(d) readfulline()
Q81 Which function is used to change the position of the Knowledge
File Handle to a given specific position.
a. seek()
b. read()
c. tail()
d. write()
Q82 What is the use of seek() method in files? Understanding
a) sets the file‟s current position at the offset
b) sets the file‟s previous position at the offset
c) sets the file‟s current position within the file
d) none of the mentioned
Q83 How do you change the file position to an offset Understanding
value from the start?
a) fp.seek(offset, 0)
b) fp.seek(offset, 1)
c) fp.seek(offset, 2)
d) none of the mentioned
Q84 How many arguments passed to the tell()? Knowledge
a. 0
b. 1
c. 2
d. variable number of arguments
Q85 How do you change the file position to an offset Understanding
value from the start?
a) fp.seek(offset, 0)
b) fp.seek(offset, 1)
c) fp.seek(offset, 2)
d) none of the mentioned
Q86 What happens if no arguments are passed to the Knowledge
seek function?
a) file position is set to the start of file
b) file position is set to the end of file
c) file position remains unchanged
d) error
Q87 readlines() method return _________ Knowledge
a. String
b. List
c. Dictionary
d. Tuple
Q88 Which of the following method is used to read a specified Knowledge
number of bytes of data from a data file.
a. read( )
b. read(n)
c. readlines( )
d. reading(n)
Q89 Write the output of the following: Understanding
f=open("test.txt","w+")
f.write("File-Handling")
f.seek(0)
a=f.read(5)
print(a)
a. File-
b. File
c. File-H
d. No Output
Q90 Write the output of the following: Understanding
f=open("test.txt","w+")
f.write("FileHandling")
f.seek(0)
a=f.read(-1)
print(a)
a. File
b. Handling
c. FileHandling
d. No Output
Q91 Knowledge
Soumya is learning to use word processing software for
the first time. She creates a file with some text
formatting features using notepad. What type of file is
she working on?
A. Text file
B. CSV file
C. Binary file
D. All of the above
Q92 To read the remaining lines of the file from a file object F, Knowledge
we use
A. F.read
B. F.read()
C. F.readlines()
D. F.readline()
Q93 Which statement is used to retrieve the current position Knowledge
within file?
A. seek()
B. tell()
C. loc()
D. pos()
Q94 In text file each line is terminated by a special character Knowledge
called ?
A. EOL
B. END
C. Full Stop
D. EOF
Q95 What error is returned by following statement, if file Knowledge &
“try.txt” does not exist? understanding
f = open(“try.txt”)
A. Not Found
B. File Not Found Error
C. File Does Not Exist
D. No Error
Q96 To open a file c:/story.txt reading we Knowledge &
use_________________ understanding
A. F=open(‘c:/story.txt’,r)
B. F=open(‘c://story.txt’,r)
C. F=open(‘c:\story.txt’,r)
D. F=open(‘c:\\story.txt’,r)
Q97 Another name of file object is Knowledge
A. File handle
B. File name
C. No another name
D. File
Q98 Name the module required for Text Files knowledge
A. Pickle
B. CSV
C. String
D. No module required
Q99 To open a file Myfile.txt for appending , we can use understanding
A. F=open("Myfile.txt","a")
B. F=open(“Myfile.txt","w+")
C. F=open(r"d:\Myfolder\Myfile.txt","w")
D. F=open("Myfile.txt","w")
Q100 To read the 02 characters of the file from a file object F, Knowledge &
we use Application
A. F.read(2)
B. F.read()
C. F.readlines()
D. F.readline()
Q101 To read the next line of the file from a file object F, we Knowledge &
use Application
A. F.read(2)
B. F.read()
C. F.readlines()
D. F.readline()
Q102 The readlines() method returns Knowledge
A. String
B. A List of integers
C. A list of characters
D. A List of Lines
Q103 Knowledge &
Assume you are reading from a text file from the
Application
beginning and you have read 10 characters. Now you
want to start reading from the 50th character from the
beginning. Which function will you use to do the same?
A. seek(50,1)
B. seek(50,2)
C. seek(40,0)
D. seek(50,0)
Q104 Consider the content of the file ‘Story.txt’ Knowledge ,
understanding &
application
Q105 Knowledge ,
Consider a file ‘rhyme.txt’
understanding &
application
What will be the output of the following code?
fileobj = open('forest.txt', 'r')
R1 = (fileobj.readline().split())
for word in R1:
for ch in range(len(word)-1,-1,-1):
print(word[ch],end='')
print(end='')
fileobj.close()
A. rewoPot.rewopmE
B. .rewopmE rewoPot
C. Empower. to Power
D. Power Empower. to
Q106 'n' characters from the file can be read by .................. Understanding
method
(A) readline(n)
(B) readlines(n)
(C) reads(n)
(D) read(n)
Q107 In which format does the readlines( ) function give the Knowledge
output?
(A) Integer type
(B) list type
(C) string type
(D) tuple type
Q108 Which of the following options can be used to read the Application
first line of a text file Myfile.txt?
(A) myfile = open('Myfile.txt');
myfile.read()
(B) myfile = open('Myfile.txt','r');
myfile.read(1)
(C) myfile = open('Myfile.txt');
myfile.readline()
(D) myfile = open('Myfile.txt');
myfile.readlines()
Q109 To read two characters from a file object infile, we use Application
____________
(A) infile.read(2)
(B) infile.read()
(C) infile.readline()
(D) infile.readlines()
Q110 To read the entire remaining contents of the file as a Application
string from a file object infile as string, we use
____________
(A) infile.read(2)
(B) infile.read()
(C) infile.readline()
(D) infile.readlines()
Q111 Read following Python code carefully and predict the Creation
correct output of the code
f=open("test.txt","r")
print(f.tell(),end="6")
f.seek(5)
print(f.tell())
(A) 605
(B) 506
(C) 065
(D) 560
Q112 What does the readline() method returns? Understanding
(A) str
(B) a list of lines
(C) list of single characters
(D) list of integers
Q113 How do you change the file position to an offset value Analysis
from the start?
(A) fp.seek(offset, 0)
(B) fp.seek(offset, 1)
(C) fp.seek(offset, 2)
(D) none of the mentioned
Q114 Which statement will return one line from a file (file Understanding
object is ‘f’) as string?
(A) f.readlines( )
(B) f.readline( )
(C) f.read( )
(D) f.line(1)
Q115 Which function is used to read data from Text File? Understanding
(A) read( )
(B) writelines( )
(C) pickle( )
(D) dump( )
Q116 The syntax of seek() is: file_object.seek(offset [, Analysis
reference_point])
What is reference_point indicate?
(A) reference_point indicates the starting position of
the file object
(B) reference_point indicates the ending position of
the file object
(C) reference_point indicates the current position of
the file object
(D) All of the above.
Q117 The syntax of seek() is:file_object.seek(offset [, Application
reference_point]) What all values can be given as a
reference point?
(A) 1
(B) 2
(C) 0
(D) All of the above
Q118 What will be the output of the following code snippet? Analysis
f = None
for i in range (5):
with open("myfile.txt", "w") as f:
if i > 2:
break
print (f.closed)
(A) Runtime Error
(B) True
(C) False
(D) Hello world
Q119 What happens if no arguments are passed to the seek Analysis
function?
(A) file position is set to the start of file
(B) file position is set to the end of file
(C) file position remains unchanged
(D) error
Q120 How do you change the file position to an offset value Application
from the current position?
(A) fp.seek(offset, 0)
(B) fp.seek(offset, 1)
(C) fp.seek(offset, 2)
(D) none of the mentioned
Q121 What are the file types in Python?
(A) Text File Understanding
(B) Binary File
(C) CSV File
(D) All of the above
Q122 What are the examples of Binary file in Python?
(A) .jpeg Understanding
(B) .gif
(C) .bmp
(D) All of the above
Q123 Which command is used to open a binary file in Python in Analysis
reading mode?
(A) F = open(“Data.dat”,”ab”)
(B) F = open(“Data.dat”,”wb”)
(C) F = open(“Data.dat, “rb”)
(D) All of the Above
Q124 Which command is used to open a binary file in Python in Analysis
append mode?
(A) F = open(“Data.dat”,”ab”)
(B) F = open(“Data.dat”,”wb”)
(C) F = open(“Data.dat, “rb”)
(D) None of the Above
Q125 Which operations can be performed in Python using Knowledge
“rb+” mode?
(A)Read
(B)Write
(C)Append
(D)Both Read and Write
Q126 Which operations can be performed in Python using Knowledge
“wb+” mode?
(A)Read
(B)Write
(C)Append
(D)Both Read and Write
Q127 Which operations can be performed in Python using Knowledge
“ab+” mode?
(A)Read
(B)Write
(C)Append
(D)Both Read and Write
Q128 Which function is used to close the contents of a binary Knowledge
file?
(A) read ( )
(B) write ( )
(C) close ( )
(D) None of these
Q129 When we open a Binary file in append mode , by default Understanding
new data can be added at the?
(A) Beginning of the file
(B) End of the file
(C) At the middle of the file
(D) All of the above
Q130 File object is also called: Knowledge
(A)read-handle
(B)file-handle
(C)data-Handle
(D) All of the above
Q131 In python binary file is treated as a : Understanding
(A)Sequence of bits
(B)Sequence of Bytes
(C)Sequence of numbers
(D)Sequence of characters
Q153 Steps to work with Binary File in Python import pickle Analysis
module. writing or appending data.
A) Open File in required mode (read, write or append).
B) Write statements to do operations like reading,
C) Close the binary file
D) All
Q157 Which of the following file mode open a file for reading Knowledge
and writing both in the binary file?
A) w
B) wb+
C) wb
D) rwb
Q158 Ms. Suman is working on a binary file and wants to write Application
data from a list to a binary file. Consider list object as l1,
binary file suman_list.dat, and file object as f. Which of
the following can be the correct statement for her?
A) f = open(‘sum_list’,’wb’); pickle.dump(l1,f)
B) f = open(‘sum_list’,’rb’); l1=pickle.dump(f)
C) f = open(‘sum_list’,’wb’); pickle.load(l1,f)
D) f = open(‘sum_list’,’rb’); l1=pickle.load(f)
Q159 In which of the file mode existing data will be intact in Understanding
binary file?
A) ab
B) a
C) w
D) wb
Q160 Which one of the following is correct statement? Evaluation
A) import – pickle
B) pickle import
C) import pickle
D) All of the above
Q161 Dump method is used to Knowledge
A) Write in a text file
B) Write in a binary file
C) Read from text file
D) Read from binary file
Q162 Which module is needed to be imported to work with Analysis
binary file?
A) csv module
B) random module
C) binary module
D) pickle module
Q163 Which method is used to change the current file pointer Knowledge
position?
A) offset()
B) tell()
C) seek()
D) point()
Q164 In binary file data is stored in which format Understanding
A) Binary
B) Text
C) ASCII
D) UNICODE
Q165 Binary file has advantage over text file. Analysis
A) It occupies less memory
B) It is fast to work with binary file
C) Computer understands this data without any
conversion
D) All of the above
Sl.No Question: Learning Objective
Q166 The module, which can use to process binary file About basic library to
use for binary file
(A) json
(B) lxml
(C) pickle
(D) none of these
Q167 Correct statement to declare necessary library to process Knowledge about syntax
the records by directly access the audio data as python to use pickle
objects
(A) pickle import
(B) import pickle as pkd
(C) import audio
(D) audio import
Q168 The method used to serialise the python objects for Awareness about method
writing data in binary file of pickle module to write
data
(A) dump( )
(B) open( )
(C) write( )
(D) serialise( )
Q169 The statement to prepare the buffer in memory to The correct syntax of use
examine the contents of the binary file (‘data.dat’) of open( ) function to
read binary file
(A) f=open( “data.dat” , “r”)
(B) f=open( data.dat , “rb” ).
(C) f=open( data.dat, “r” )
(D) f=open( “data.dat” , “rb” )
Q170 The method used to unpickling the data from a binary file Awareness about the
method to load data from
(A) unpickle( )
binary file to process
(B) load( )
(C) deserialise( )
(D) read( )
Q171 What is wrong with the following code ? Awareness about that
write( ) on binary file
listvalues = [1, “Geetika”, ‘F’, 26]
requires bytes only.
fileobject = open( ‘mybinary.dat’ , ‘wb’ )
fileobject.write ( listvalues)
fileobject.close( )
(A) error in open mode of file
(B) error, pickle is essential to use write( )
(C) error in write( )
(D) No error
Q172 Let the file ‘abc.csv’ contains the following records in Awareness about that,
comma separated way processing csv file using
binary methods will not
1,Geetika,F,26
work without converting
2,Jeevan,M,24 the loaded binary data to
text format before further
Consider the following code
processing
fileobject = open( ‘abc.csv’ , ‘rb’ )
contents=fileobject.read( )
lst=contents.split( )
for rec in lst:
print(lst.split(‘,’)[1],end=’#’)
fileobject.close( )
def loadData():
dbfile = open('examplePickle', 'rb')
db = pickle.load(dbfile)
p=0
for keys in db:
p += db[keys][‘pay’]
print(p)
dbfile.close()
storeData()
loadData()
(A) 45000
(B) 70000
(C) 25000
(D) none of these
Q181. Which module is used to store data into python objects Understanding
with their structure?
(A) pickle
(B) binary files
(C) unpickle
(D) None of these
Q182 Which one of the following is the correct statement? Understanding
(A) pickle import
(B) import – pickle
(C) import pickle
(D) None of the above
Q183 Which function is use for reading data from a binary file? Application
(A) load()
(B) read()
(C) dump()
(D) None of the above
Q184 Which function is use for writing data in binary file? Creation
(A) load()
(B) write()
(C) dump()
(D) None of the above
Q185 _______ the process in which an object converts into a Creation
byte stream
(A) unpickling
(B) pickling
(C) (A) and (B)
(D) None of the above
Q186 Serialization in binary file is also called __________ Creation
(A) Unpickling
(B) Pickling
(C) Both (A) and (B)
(D) None of the above
Q187 ________ function places the file pointer at the specified Knowledge
position by in an open file.
(A) tell()
(B) seek()
(C) both A and B
(D) None of the above
Q188. _______ function return the current position of the file Knowledge
pointer.
(A) tell()
(B) seek()
(C) both A and B
(D) None of the above
Q189 Fill in the blank: Understanding,
import pickle Knowledge,
f=open("data.dat",'rb') Application, Evaluation
d=_____________________.load(f) # Statement1
f.close()
(A) unpickle
(B) pickling
(C) pickle
(D) pick
Q190 Which of the following function takes two arguments? Knowledge
a) load()
b) dump()
c) Both of the above
d) None of the above
Q191 .pdf and .doc are examples of ________ files. Application
(A) Text
(B) Binary
(C) CSV
(D) None of the above
Q192 The syntax of seek() is : file_object.seek(offset Understanding
[,reference_point]). What all values can be given as a
reference point?
(A) 1
(B) 2
(C) 0
(D) All of the above
Q193 f.seek(10,0) will move 10 bytes forward from _______. Application
(A) beginning
(B) End
(C) Current
(D) None of the above
Q194 Which statement will move file pointer 10 bytes Understanding
backward from end position.
(A) F.seek(-10,2)
(B) F.seek(10,2)
(C) F.seek(-10,1)
(D) None of the above
Q195 Aman opened the file “myfile.dat” by using the following Evaluation
syntax. His friend told him few advantages of the given
syntax. Help him to identify the correct advantage.
with open(“myfile.dat”, ‘ab+”) as fobj:
(A) In case the user forgets to close the file explicitly
the file will closed automatically
(B) File handle will always be present in the
beginning of the file even in append mode.
(C) File will be processed faster
(D) None of the above.
Q196 Which of the following method is used to clear the Analysis
buffer?
(A) clear()
(B) buffer()
(C) flush()
(D) clean()
Q197 Write the output of the following: Understanding,
f=open(“t.dat”, “rb”) Knowledge,
print(f.tell()) Application, Evaluation
(A) 1
(B) 2
(C) -1
(D) 0
Q198 Write the output of the following: Understanding,
f=open(“t.dat”, “r”) Knowledge,
print(f.tell(), end=”6”) Application, Evaluation
f.seek(5)
print(f.tell())
(A) 165
(B) 650
(C) 065
(D) 506
S.No Question Learning Objective
Q199 Which mode create new file if the file does not exist? Knowledge
a) Write mode
b) Append mode
c) Both a & b
d) None of the above
Q200 seek() function is used to _____ Understanding
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
Q201 Which function is used to read data from Binary File? Knowledge
a) read()
b) load()
c) pickle()
d) dump()
Q202 Which symbol is used for append mode in Binary file? Understanding
a) a
b) a+
c) ab
d) None
Q203 Which function is used to write data in Binary File? Knowledge
a) read()
b) load()
c) pickle()
d) dump()
Q204 Fill in the blank Analysis
import pickle
file=open("data.dat",'rb')
d=_____________________.load(f)
f.close()
a) pick
b) pickling
c) file
d) pickle
Q205 ____________________ module is used for serializing Understanding
and de-serializing any Python object structure.
a) pickle
b) math
c) unpickle
d) random
Q222 Which of the following object you get after reading CSV Understanding
file?
(A) Character Vector
(B) Comma Vector
(C) Panel
(D) DataFrame
Q223 Which of the following is a function of csv module? Evaluation
(A) writerrow()
(B) reading()
(C) writing()
(D) readline()
Q224 To open a file c:\myscores.csv for reading, we use Application
_______ command.
(A) infile = open(“c:\myscore.csv”, “r”)
(B) infile = open(“c:\\myscore.csv”, “r”)
(C) infile = open(file = “c:\myscore.csv”, “r”)
(D) infile = open(file = “c:\\myscore.csv”, “r”)
Q225 Which of the following statement(s) are true for csv files? Understanding
(A) Existing file is overwritten with the new file if
already exist
(B) If the file does not exist, a new file is created for
writing purpose
(C) When you open a file for reading, if the file does not
exist, an error occurs
(D) All of the above
Q226 To read the entire content of the CSV file as a nested list Application
from a file object infile, we use __________ command.
(A) infile.read()
(B) infile.reader()
(C) csv.reader(infile)
(D) infile.readlines()
Q227 Which of the following is not a valid mode to open CSV Creation
file?
(A) a
(B) w
(C) r
(D) ab
Q228 The CSV files are popular because they are Understanding
(A) capable of storing large amount of data
(B) easier to create
(C) preferred export and import format for databases and
spread sheets
(D) All the above
Q229 When iterating over an object returned from csv.reader(), Application
what is returned with each iteration?
For example, given the following code block that assumes
csv_reader is an object returned from csv.reader(), what
would be printed to the console with each iteration?
for item in csv_reader:
print(i)
(A) The individual value data that is separated by the
delimiter
(B) The row data as a list
(C) The column data as a list
(D) The full line of the file as a string
Q230 Which of the following parameter needs to be added with Application
open function to avoid blank row followed by each row in
the CSV file?
(A) quotechar
(B) quoting
(C) newline
(D) skiprow
Q231 In separated value files such as .csv, what does the first Understanding
row in the file typically contain?
(A) The author of the table data
(B) The column names of the data
(C) The source of the data
(D) Notes about the table data
Q232 A _________ is a file format which stores records Knowledge
separated by comma.
(A) .jpg
(B) .pdf
(C) .csv
(D) None of the above
Q233 Which of the following Python statement is used to read Understanding
the entire content of the CSV file as a nested list from a
file object infile?
(A) infile.read()
(B) infile.reader()
(C) csv.reader(infile)
(D) infile.readlines()
Q234 What is the full form of CSV? Knowledge
(A) Comma Separated Values
(B) Comma Separated Value
(C) Comma Separated Variables
(D) Comma Separate Values
Q235 EOL character used in windows operating system in CSV Knowledge
files is
(A) \r\n
(B) \n
(C) \r
(D) \0
Q236 _________ is the default delimiter character of a CSV Knowledge
file.
(A) : (colon)
(B) \t (tab)
(C) , (comma)
(D) ; (semi-colon)
Q237 A CSV file marks.csv is stored in the storage device. Application
Identify the correct option out of the following options to
open the file in read mode.
i. myfile = open('marks.csv','r')
ii. myfile = open('marks.csv','w')
iii. myfile = open('marks.csv','rb')
iv. myfile = open('marks.csv')
(A) only i
(B) both i and iv
(C) both iii and iv
(D) both i and iii
Q238 Which of the following is a function of the csv module? Knowledge
(A) readline()
(B) reader()
(C) read()
(D) readrow()
Q239 What is the output of the following program? Application
import csv
d=csv.reader(open('city.csv'))
for row in d:
print(row)
break
If the file called city.csv contains the following details
Bhubaneshwar, Kolkata
Guwahati, Silchar
(A) Guwahati, Silchar
(B) Bhubaneshwar, Kolkata
(C) Bhubaneshwar, Kolkata
Guwahati, Silchar
(D) Bhubaneshwar
Guwahati
Q240 The CSV files are _______ files. Knowledge
(A) Python
(B) Binary
(C) Data
(D) Text
Q241 The writer() function has how many mandatory Understanding
parameters?
(A) 1
(B) 2
(C) 3
(D) 4
Q242 Vikas wants to separate the values in a csv file by a # Understanding
sign. Suggest to him a pair of function and parameter to
use it.
(A) open,quotechar
(B) writer,quotechar
(C) open,delimiter
(D) writer, delimiter
Q243 Which of the following option is correct? Understanding
i. if we try to read a csv file that does not exist, an
error occurs.
ii. if we try to read a csv file that does not exist, the
file gets created.
iii. if we try to write on a csv file that does not exist,
an error occurs.
iv. if we try to write on a csv file that does not exist,
the file gets Created.
(A) only i
(B) only iv
(C) both i and ii
(D) both i and iv
ANSWERS
Question No Answer
Q1 (D) No Output
Q2 (C) ..\First Term\Result1.xlsx
(D) In ‘a+’ mode, both reading and writing operations can take
Q3 place and new data is appended to the end of the existing file.
Q4 (B) disp = fileobj.readlines()
Q5 (C) Both (ii) and (iii)
Q6 (C) Binary file
Q7 (C) tell()
(C) It will place the pointer at 25 bytes from the beginning of the
Q8 file.
Q9 (A) dt = f.readlines();print(dt[3])
Q10 (A) reader(), writer()
Q11 (D) All of the above
Q12 (B) slowly but steadily
Q13 (C) 9
Q14 (A) write()
(B) In r+ mode, the pointer is initially placed at the beginning of
Q15 the file and for w+, the pointer is placed at the end.
Q16 C
Q17 B
Q18 B
Q19 A
Q20 D
Q21 B
Q22 c
Q23 B
Q24 C
Q25 B
Q26 B
Q27 D
Q28 B
Q29 A
Q30 A
(C) f = open(“file.txt”,’w’)
Q31
Q32 (A) with open(“file.txt”, “a”) as f :
Q33 (D) b+
Q34 (B) f.close()
Q35 (C) writelines()
Q36 (A) write()
Q37 (C) Both of the above
Q38 (A) r
Q39 (B) w
Q40 (C) Both of the above
Q41 (C) Both of the above
(B) In both modes file pointer for existing file sets to beginning of
Q42 file.
Q43 (D) File does not exist
(B) Both strings of list lines will be written in the same line in the
Q44 file ‘readme.txt’.
(A) Both strings of list lines will be written in two different lines
Q45 in the file ‘readme.txt’.
D
Q46
Q47 C
Q48 D
Q49 A
Q50 D
Q51 A
Q52 C
Q53 B
Q54 B
Q55 C
Q56 D
Q57 D
Q58 A
Q59 C
Q60 A
Q61 D
Q62 D
Q63 C
Q64 B
Q65 D
Q66 A
Q67 A
Q68 D
Q69 A
Q70 A
Q71 A
Q72 A
Q73 A
C
Q74
A
Q75
Q76 (b) fobj.read()
Q77 (a) fp.seek(offset, 0)
Q78 (b) fp.tell()
Q79 (d) results in an error
Q80 (a) readline()
Q81 a. seek()
Q82 a) sets the file‟s current position at the offset
Q83 a) fp.seek(offset, 0)
Q84 a. 0
Q85 a) fp.seek(offset, 0)
Q86 d) error
Q87 b. List
Q88 b. read(n)
Q89 a. File-
Q90 c. FileHandling
Q91 A
Q92 C
Q93 B
Q94 A
Q95 B
Q96 D
Q97 A
Q98 D
Q99 A
Q100 A
Q101 D
Q102 D
Q103 D
Q104 B
Q105 A
Q106 D
Q107 B
Q108 C
Q109 A
Q110 B
Q111 C
Q112 A
Q113 A
Q114 B
Q115 A
Q116 D
Q117 D
Q118 B
Q119 D
Q120 B
D
Q121
Q122 D
Q123 C
Q124 A
Q125 D
Q126 D
Q127 D
Q128 C
Q129 B
Q130 B
Q131 B
Q132 D
Q133 B
Q134 B
Q135 C
Q136 (A) A stream of bytes
Q137 (B) b
Q138 (A) Pickling
Q139 (D) Every line ends with new line character ‘\n’
Q140 (C) rb+
Q141 (D) B & C both
Q142 (C) ab+
Q143 (A) r
Q144 (B) rb
Q145 (B) Binary file
Q146 (B) Binary
Q147 (C) Binary File
Q148 (A) open( )
Q149 (C) A & B Both
Q150 (D) <fileobj>.close()
Q151 B
Q152 D
Q153 D
Q154 D
Q155 B
Q156 D
Q157 B
Q158 A
Q159 A
Q160 C
Q161 B
Q162 D
Q163 C
Q164 A
Q165 D
C
Q166
B
Q167
A
Q168
D
Q169
B
Q170
C
Q171
A
Q172
C
Q173
B
Q174
C
Q175
B
Q176
A
Q177
C
Q178
D
Q179
B
Q180
Q181 A
Q182 C
Q183 A
Q184 C
Q185 B
Q186 B
Q187 B
Q188 A
Q189 C
Q190 B
Q191 B
Q192 D
Q193 A
Q194 A
Q195 A
Q196 C
Q197 D
Q198 C
Q199 C
Q200 A
Q201 B
Q202 C
Q203 D
Q204 D
Q205 A
Q206 B
Q207 B
Q208 B
Q209 C
Q210 D
Q211 D
Q212 B
Q213 A
Q214 (C) csv
Q215 (B) Finding data uniquly
Q216 (D) 1
Q217 (B) Binary file
(A) source=”c:\\myfile.csv”
Q218 data = pd.read_csv(source)
Q219 (D) Both A & B
(A) Any character such as the comma (,) or tab ;) used to separate
Q220 the column data
Q221 (C) Column Heading
Q222 (D) DataFrame
Q223 (A) writerrow()
Q224 (B) infile = open(“c:\\myscore.csv”, “r”)
Q225 (D) All of the above
Q226 (C) csv.reader(infile)
Q227 (D) ab
Q228 (D) All the above
Q229 c) The column data as a list
Q230 c) newline
Q232 c) .csv
Q233 c) csv.reader(infile)
Q234 a) Comma Separated Values
Q235 a) \r\n
Q236 c) , (comma)
Q238 b) reader()
Q240 d) text
Q241 a) 1
Region- KOLKATA
E-mail ID - kamalkant.kv@gmail.com
Name of the Chapter- DATA FILE HANDLING
Q3. Assertion: When you open a file for writing, if the file Understanding
does not exist, an error occurs.
Reason: When you open a file for writing, if the file
exists, the existing file is overwritten with the new file.
Q26. Assertion: readline() function will read one line from the Knowledge
file. Understanding
Analysis
Reason: readlines() function will read all the lines from
the files.
Reason:
We can use readline( ) function which can read one line at
a time from the file.
Reason:
Seek() function with negative offset only works when file
is opened in binary mode.
myfile.close()
in the above code, data type of data_rec is a
list class.
Reason: Myfile.txt is a text file so it contains only list of
text lines.
(A) Both Assertion and reason are true and reason is
correct explanation of assertion.
(B) Assertion and reason both are true but reason is not
the correct explanation of assertion.
(C) Assertion is true, reason is false.
(D) Assertion is false, reason is true.
Q35 Assertion: f=open(‘story.txt’,’r+’) Knowledge and
Above statement will create a new file, it it is understanding
not exist.
Reason: when we open a file in write mode, it will create
a new file with same name.
(A) Both Assertion and reason are true and reason is
correct explanation of assertion.
(B) Assertion and reason both are true but reason is not
the correct explanation of assertion.
(C) Assertion is true, reason is false.
(D) Assertion is false, reason is true.
Q36 Read the following statements and give the answer Analysis
Assertion: readline() read the files line by line
Reason: Without realine() lines from the file cannot be read
Q38 Read the following statements and give the answer Analysis
Assertion: seek (5,0) moves cursor after 5 bytes
Reason: It helps to read entire content after 5
characters
Q39. Read the following statements and give the answer Analysis
Assertion: readline(5) reads 5 characters from the
line.
Reason: next readline() read the same line.
Q40. Read the following statements and give the answer Analysis
Assertion: readlines() returns one line at a time
Reason: readlines() helps in counting number of lines
of the text file
Q56 Assertion: pickle module can save python objects into pickling process
binary file
Reason: pickling process converts python objects into
byte stream
(A) Both Assertion and reason are true and reason is
correct explanation of assertion.
(B) Assertion and reason both are true but reason is not
the correct explanation of assertion.
(C) Assertion is true, reason is false.
(D) Assertion is false, reason is true.
Q57 Assertion: pickle.load( ) deserialise the python object unpickling process
Reason: Deserialise object process involves conversion of
python object into string
(A) Both Assertion and reason are true and reason is
correct explanation of assertion.
(B) Assertion and reason both are true but reason is not
the correct explanation of assertion.
(C) Assertion is true, reason is false.
(D) Assertion is false, reason is true.
Q58 Assertion: “import pickle” statement is required to use use of pickle
pickle in python program
Reason: “pip install pickle” command is required to
install pickle in python
(A) Both Assertion and reason are true and reason is
correct explanation of assertion.
(B) Assertion and reason both are true but reason is not
the correct explanation of assertion.
(C) Assertion is true, reason is false.
(D) Assertion is false, reason is true.
Q59 Assertion: “L=[3,4] ; f=open(‘L1.dat’,’wb’); Serialisation Process
pickle.dump(L,f); f.close( ) “ => Serialise the [3,4] as
byte squence into file L1.dat
Reason: pickle.dum( A , B) => Serialise the Python
object A as a byte stream onto the file using fileobject B
(A) Both Assertion and reason are true and reason is
correct explanation of assertion.
(B) Assertion and reason both are true but reason is not
the correct explanation of assertion.
(C) Assertion is true, reason is false.
(D) Assertion is false, reason is true.
Q60 Assertion: d={‘a’:12, ‘b’:10}; f=open(‘data.dat’, ‘wb’); Difference between
f.write( d ); f.close( ) => Python object d is serialised Writing memory block
onto file using file object f and Serialisation of
object
Reason: d={‘a’:12, ‘b’:10}; f=open(‘data.dat’, ‘wb’);
pickle.dump( d, f); f.close( ) => Python object d is
serialised onto file using file object f
(A) Both Assertion and reason are true and reason is
correct explanation of assertion.
(B) Assertion and reason both are true but reason is not
the correct explanation of assertion.
(C) Assertion is true, reason is false.
(D) Assertion is false, reason is true.
Q61 Assertion: CSV is a kind of plain text file Understanding
Reason: It can be opened in all type of programs
(A) Both Assertion and reason are true and reason is
correct explanation of assertion.
(B) Assertion and reason both are true but reason is not
the correct explanation of assertion.
(C) Assertion is true, reason is false.
(D) Assertion is false, reason is true.
Q62 Assertion: First use open() method to read data from file Creation
Reason: File File object is required to read data with the
help of csv.reader object/method
(A) Both Assertion and reason are true and reason is
correct explanation of assertion.
(B) Assertion and reason both are true but reason is not
the correct explanation of assertion.
(C) Assertion is true, reason is false.
(D) Assertion is false, reason is true.
Q63 Assertion: We should use with() statement to write csv Application
related statement
Reason: with() statement at end releases all the resources
automatically without using close()
(A) Both Assertion and reason are true and reason is
correct explanation of assertion.
(B) Assertion and reason both are true but reason is not
the correct explanation of assertion.
(C) Assertion is true, reason is false.
(D) Assertion is false, reason is true.
Q64 Assertion:CSV is best suitable to open in text editor Understanding
Reason: Most of the files can be opened in text editor
(A) Both Assertion and reason are true and reason is
correct explanation of assertion.
(B) Assertion and reason both are true but reason is not
the correct explanation of assertion.
(C) Assertion is true, reason is false.
(D) Assertion is false, reason is true.
Q65 Assertion:close() method must be called as last statement Evaluation
related to csv file object
Reason: As it releases the assigned resources
(A) Both Assertion and reason are true and reason is
correct explanation of assertion.
(B) Assertion and reason both are true but reason is not
the correct explanation of assertion.
(C) Assertion is true, reason is false.
(D) Assertion is false, reason is true.
Q66.. Assertion: While writing data to csv file, we set the value Application
of parameter newline to "" in the open() function to
prevent blank rows.
Reason: EOL translation is suppressed by using
newline="".
(A) Both Assertion and reason are true and reason is the
correct explanation of assertion.
(B) Assertion and reason both are true but reason is not
the correct explanation of assertion.
(C) Assertion is true, reason is false.
(D) Assertion is false, reason is true.
Q67. Assertion: The CSV module in Python’s standard library Knowledge
presents methods to perform read/write operations on
CSV files.
Reasoning: To write data to a CSV file, we can use
built-in function writer().
(A) Both Assertion and reason are true and reason is the
correct explanation of assertion.
(B) Assertion and reason both are true but reason is not
the correct explanation of assertion.
(C) Assertion is true, reason is false.
(D) Assertion is false, reason is true.
Q68. Assertion: To open a CSV file employee.csv in write Understanding
mode, we can give python statement as follows:
fh=open('employee.csv’)
Reasoning: If we try to write on a csv file that does not
exist, the file gets Created
(A) Both Assertion and reason are true and reason is the
correct explanation of assertion.
(B) Assertion and reason both are true but reason is not
the correct explanation of assertion.
(C) Assertion is true, reason is false.
(D) Assertion is false, reason is true.
Q69. Assertion: We can open a CSV file in the same way as a Understanding
text file for reading and writing data.
Reason: A CSV file is a text file that uses a delimiter to
separate values.
(A) Both Assertion and reason are true and reason is the
correct explanation of assertion.
(B) Assertion and reason both are true but reason is not
the correct explanation of assertion.
(C) Assertion is true, reason is false.
(D) Assertion is false, reason is true.
Q70. Assertion: To specify a different delimiter while writing Application
into a csv file, delimiter argument is used with
csv.writer().
Reason: The CSV file can only take a comma as a
delimiter.
(A) Both Assertion and reason are true and reason is the
correct explanation of assertion.
(B) Assertion and reason both are true but reason is not
the correct explanation of assertion.
(C) Assertion is true, reason is false.
(D) Assertion is false, reason is true.
ANSWER
Question No Answer
Q1. (D) Assertion is false, reason is true.
Q2. (C) Assertion is true, reason is false.
Q3. (D) Assertion is false, reason is true.
Q4. (C) Assertion is true, reason is false
Q5. (B) Assertion and reason both are true but reason is not the correct
explanation of assertion.
Q6. A
Q7. C
Q8. B
Q9. A
Q10. D
Q11. (A) Both Assertion and reason are true and reason is the correct
explanation of assertion.
Q12. (D) Assertion is false, reason is true.
Q13. (C) Assertion is true, reason is false
Q14. (B) Assertion and reason both are true but reason is not the correct
explanation of assertion.
Q15. (A) Both Assertion and reason are true and reason is the correct
explanation of assertion.
Q16. A
Q17. A
Q18. B
Q19. A
Q20. A
Q21. A
Q22. C
Q23. A
Q24. C
Q25. A
Q26. (B) Assertion and reason both are true but reason is not the correct
explanation of assertion.
Q27. (D) Assertion is false, reason is true.
Q28. (A) Both Assertion and reason are true and reason is correct
explanation of assertion.
Q29. (B) Assertion and reason both are true but reason is not the correct
explanation of assertion.
Q30. (C) Assertion is true, reason is false.
Q31 C
Q32 C
Q33 A
Q34 B
Q35 D
Q36 C
Q37 B
Q38 A
Q39. B
Q40. D
Q41. A
Q42. B
Q43. B
Q44. A
Q45. B
Q46. (C) Assertion is true, reason is false
Q47. (B) Assertion and reason both are true but reason is not the
correct explanation of assertion
Q48. (A) Both Assertion and reason are true and reason is correct
explanation of assertion.
Q49. (A) Both Assertion and reason are true and reason is correct
explanation of assertion.
Q50. (D) Assertion is false, reason is true.
Q51 D
Q52 C
Q53 B
Q54 D
Q55 D
Q56 A
Q57 D
Q58 C
Q59 A
Q60 D
Q61 B
Q62 A
Q63 A
Q64 C
Q65 A
Q66.. a) Both Assertion and reason are true and reason is the correct
explanation of assertion.
Q67. c) Assertion is true, reason is false.
Q68. d) Assertion is false, reason is true.
Q69. a) Both Assertion and reason are true and reason is the correct
explanation of assertion.
Q70. c) Assertion is true, reason is false.
Region- Kolkata
Mobile No-9493887480
E-mail ID - kamalkant.kv@gmail.com
Name of the Chapter- DATA FILE HANDLING
Incomplete Code:
______ #Statement 1
Csvfile = open(______, _______, newline=’’)
#Statement 2
CsvObj = csv. ________( ________) #Statement 3
Rows = []
Fields = [‘Empnumber’, ‘EmpName’, ‘EmpDesig’,
‘EmpSalary’]
Rows.append(Fields)
for i in range(5):
Empno = input(“Enter Employee No:”)
Name = input(“Enter Employee Name:”)
Desig = input(“Enter Designation:”)
Salary = int(input(“Enter Salary:”))
records= [________________] #Statement 4
Rows.________(______) #Statement 5
_______._______(Rows) #Statement 6
Csvfile.close()
A. w
B. r+
C. r
D. w+
def search():
f = open("EMP.txt",) #Statement-1
A=____________________ #Statement-2
ct=0
for x in A:
p=x.split()
if p==”Manager”:
ct+=1
print(ct)
c) readlines
d)readl()
Q11
(CASE STUDY: 1)
f=open("data.txt",'w')
f.write("Hello")
f.write("Welcome to my Blog")
f.close()
f=open("data.txt",'r')
d=f.read(5)
print(d) # First Print Statement
f.seek(10) Knowledge
d=f.read(3) Understanding
print(d) # Second Print Statement Analysis
f.seek(13) Application
d=f.read(5)
print(d) # Third Print Statement
d=f.tell()
print(d) # Fourth Print Statement
1 Refer to the above code (CASE STUDY:1) Write the
output of the First Print statements :
a. Hello
b. Hell
c. ello
d. None of the above
2 Refer to the above code (CASE STUDY:1) : Write the
output of Second Print Statement
a. om
b. me
c. co
d. None of the above
3 Refer to the above code (CASE STUDY:1) : Write the
output of Third Print Statement
a. e to m
b. e to my
c. to my
d. None of the above
4 Refer to the above code (CASE STUDY:1) : Write the
output of Fourth Print Statement
a. 17
b. 16
c. 19
d. 18
b) r
c) w
d) a
b) read(5)
c) readline(5)
d) get(5)
b) file.readlines()
c) file.readline()
d) readlines()
(i) Identify the suitable code for blank space in the line
marked as Statement-1.
A. ‘w’
B. ‘w+’
C. ‘rb’
D. ‘r+’
(ii) Identify the suitable code for blank space in the line
marked as Statement-2.
A. Vlist.split()
B. S.split(‘*’)
C. s.split()
D. S.split()
(iii) Identify the suitable code for blank space in the line
marked as Statement-3.
A. vlist
B. s
C. w
D. myfile
(iv) Identify the suitable code for blank space in the line
marked as Statement-4.
A. y & w
B. y[0] & s
C. y & s
D. y[0] & w
(v) Identify the suitable code for blank space in the line
marked as Statement-5.
A. Off()
B. Close()
C. close()
D. open()
Q14. Consider the content of ‘rhyme.txt’ file:
Amit, a student of class 12 Science, is learning Python. During
the examination he has been given a stub Python program
(shown below) to count no of lines in ‘rhyme.txt’ file starts
with H or T has started from vowels. Help him in completing
the code.
fileobj=_________(‘rhyme.txt', ____ ) #
Statement 1
count = 0
L=fileobj.______________ #
Statement 2
for i in L:
if ( ______=='H' or ______==’T’ ): #
Statement 3
count=________ #
Statement 4
print(count) #
Statement 5
fileobj.close()
(i) Identify the suitable code for blank space in the line
marked as Statement-1.
A. Open () & ‘w’
B. open() & ‘w+’
C. open() & ‘r’
D. Open() & ‘r+’
(ii) Identify the suitable code for blank space in the line
marked as Statement-2.
A. read()
B. readlines()
C. readline()
D. readlines(5)
(iii) Identify the suitable code for blank space in the line
marked as Statement-3.
A. L[0]
B. i[0]
C. i
D. L[i]
(iv) Identify the suitable code for blank space in the line
marked as Statement-4.
A. 1
B. count+2
C. count+count
D. count+1
(v) Identify the ouput that will be display by Statement-5.
A. 1
B. 2
C. 3
D. 4
Q15 A Text file ‘data.txt’ content following lines. A Python Learning Objective
code is given to read the content of the file. Read the code
given below carefully and answer the following
questions.
File Content
Hello
Welcome to my blog
One of the best blog for Python
Code:
f=open("data.txt",'r')
d=f.read(5)
print(d) # Statement 1
f.seek(10)
d=f.read(3)
print(d) # Statement 2
f.seek(13)
d=f.read(5)
print(d) # Statement 3
d=f.tell()
print(d) # Statement 4
f.close()
f.read() ## Statement 5
1 What is output of statement 1 Application
(A) Hello
(B) ello
(C) hello
(D) Hello W
2 What is output of statement 2 Application
(A) com
(B) lco
(C) eco
(D) None of the above
3 What is output of statement 3 Application
(A) e to
(B) to my
(C) o my b
(D) Error
4 What is output of statement 4 Creation
(A) 18
(B) 19
(C) 17
(D) 16
5 What is out of statement 5 Analysis
(A) my blog
One of the best blog for Python
(B) One of the best blog for Python
(C) Error
(D) None of the Above
Q16. A Text file ‘myfav.txt’ content following lines. Python
code is given to count the number of word ‘this’ present
in the file. Read the code given below and answer the
following questions.
File Content:
This is India, this is great country. this is the place where
great personalities were born. this is the nation harmony.
Code:
file=open('myfav.txt','r')
count = 0
l=file.________() #statement 1
word= l.______() #statement 2
for i in word:
if(i=='______'): #statement 3
count=count+_______ #statement 4
print(count) # statement 5
file.close()
1 Write the correct function name that reads entire content Creation
as string (statement 1).
(A) Read
(B) read
(C) readline
(D) Readlines
2 Write the correct function name that breaks the string into Creation
list of words (statement 1).
(A) split()
(B) break()
(C) split(‘space’)
(D) break(‘space’)
3 File the blank that correct count the desired word in the Application
file (statement 3).
(A) This
(B) ‘This’
(C) ‘this’
(D) this
4 What is the literal value to increase the value of count by Analysis
1 statement 4
(A) 1
(B) 2
(C) 3
(D) 4
5 What is the output of statement 5 Analysis
(A) 5
(B) 2
(C) 3
(D) 4
Q17. Amit has written the following code in Python. Application
f=open("empfile.dat","ab") # Statement 1
print(f.closed) # Statement 2
f.close() # Statement 3
print(f.closed) # Statement 4
Answer the following questions based on the above
program
i.)In which mode the file will be opened.
(A) read binary
(B) write binary
(C) append binary
(D) None of the above
ii.)What will be the output of statement 2?
(A) False
(B) True
(C) 0
(D) 1
iii.)What will be the output of statement 4?
(A) False
(B) True
(C) 0
(D) 1
iv.)What is f in statement 1?
(A) List
(B) Dictionary
(C) File object
(D) Tuple
v.)Can we perform any task on the file ‘empfile.dat’
through the file object ‘f’ after statement 3?
(A) YES
(B) NO
(C) Can't say
(D) None of the above
Q18. Ravi has written the following code in Python. Application
fb=open("stu.dat","wb+") # Statement 1
print(fb.closed) # Statement 2
fb.close() # Statement 3
print(fb.closed) # Statement 4
Answer the following questions based on the above
program
i.)In which mode the file will be opened.
(A) read binary
(B) write and read binary
(C) append binary
(D) None of the above
ii.)Which operation is possible after statement 1?
(A) write
(B) read
(C) append
(D) write and read
iii.)What will happen if the file “stu.dat” is unavailable?
(A) File is created with ‘wb+’ mode
(B) File will be created in append mode
(C) File will be created in write only mode
(D) Error
iv.)What is fb in statement 1?
(A) Variable
(B) String
(C) List
(D) File object
Q19. Given a binary file “emp.dat” has structure (Emp_id, Application
Emp_name, Emp_Salary). Write a function in Python
countsal() in Python that would read contents of the file
“emp.dat” and display the details of those employee
whose salary is greater than 20000.
import pickle
def ____________: Line1
f = open (“emp.dat”, “________”) Line 2
n=0
try:
while True:
rec = ____________ Line 3
if rec[2] > 20000:
print(rec[0], rec[1], rec[2], sep=”\t”)
num = num + _______ Line 4
except:
f.close() Line 5
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()
Q1. 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’)
Q2. 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()
Q3. 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)
Q4. 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
Q21 Given a binary file “emp.dat” has structure (Emp_id, Application
Emp_name, Emp_Salary). Write a function in Python
countsal() in Python that would read contents of the file
“emp.dat” and display the details of those employee
whose salary is greater than 20000.
import pickle
def_____: Line1
f = open (“emp.dat”, “_ ”) Line 2
n=0
try:
while True:
rec = ___________ Line 3
if rec[2] > 20000:
print(rec[0], rec[1], rec[2], sep=”\t”)
num = num +__________ Line 4
except:
f.______ Line 5
Q23. Read the program code given below and answer the Learning Objective:
following questions……..
((
Examine the code
Consider the following program to enter the following example to solve an
records as per below format in a binary file: application oriented
Item No integer problem and propose the
solution
Item_Name string
Qty integer
Price float
Number of records to be entered should be accepted from
the user. Read the file to display the records in the
following format:
Item No:
Item Name:
Quantity:
Price per item:
Amount: (to be calculated as Price * Qty)
))
import pickle
def input_record():
rec={}
rec['Item No']=int(input('Enter Item No'))
rec['Item_Name']=input('Enter Item Name')
rec['Qty']=int(input('Enter Quantity'))
rec['Price']=float(input('Enter Price'))
return rec
def create_file():
fileobject=open('data.dat','wb')
n=int(input('Enter Number of records to be entered'))
for i in range(n):
record=input_record()
pickle.dump(record,fileobject)
if i+1 < n:
print('Next Record')
print(n,'Records entered')
def show_records():
fileobject=open('data.dat','____') #statement1
i=1
while True: #statement2
try:
record=pickle.__________ #statement2
print('Record:',i)
print('Item No:',record['Item No'])
print('Item Name:',record['Item_Name'])
print('Quantity:',record['Qty'])
print('Price per item:',record['Price'])
print('Amount:', record['Qty'] * record['Price'])
i += 1
except:
pass
Q25. Raj has been given following incomplete code, which Understanding,
takes a Employee’s details (Ecode, Ename, and Salary) Knowledge,
and writes into a binary file emp.dat using pickling. Application, Evaluation
import pickle
eno = int((input(“enter emp. Code”))
ename = input(“Enter emp. Name”)
sal = int(input(“Enter Emp. Salary”))
emp1 = {“Ecode”:eno, “Ename”:ename, “Salary”:sal}
with ___________________ as fh: # Statement 1
__________________ # Statement 2
_______________________ as fin # Statement 3
__________________ # Statement 4
print(Remp)
if Remp[“Salary”] > = 85000:
print(“Eligible for increment”)
____ : # Statement 5
print(“Not eligible for increment”)
def ShowRecord():
with open(“LOCALITY.CSV”, ”r”) as file:
obj=csv.___________(file) # Line 5
for rec in obj:
print(rec[0], “#”, rec[2])
C_1=[1,’XYZ’, ‘Guwahati’]
C_2=[2,’PQRYZ’, ‘Dibrugarh’]
addRecord(C_1)
addRecord(C_2)
ShowRecord()
Understanding
1. Which module should be imported in Line 1?
(A) pickle
(B) csv
(C)file
(D) text
Creation
2. Which symbol is missing in Line 2?
(A) ::
(B) ,
(C) @
(D) :
Understanding
3. Which statement will be written at Line 4?
(A) f.close
(B) obj.close()
(C) f.close()
(D) obj.close
Application
4. Which function to be used in Line 5 to read the data
from a csv file.
(A) read()
(B) readline()
(C) readlines()
(D) reader()
Q29. Amayra has a branch1.csv file which has the name, class
and section of students. She receives a branch2.csv which
has similar details of students in second branch. She is
asked to add the details of branch2,csv into branch1.csv.
As a programmer, help her to successfully execute the
given task.
_____ csv #Statement1
file = open('branch1.csv', __b__ , newline="");
writer = csv. _____ (file) #Statement2
with open('branch2.csv','r') as csvfile ___#Statement3
data = csv.reader(csvfile)
for row in data:
writer.writerow(__)#Statement4
file. _____ ()#Statement5
Understanding
1. Which statement is to be used in place of blank space
of Statement 1?
(A) include
(B) import
(C) create space Creation
(D) includes
import ________
#Line 1
f=open("student.csv","w",newline=
'')
s=csv.______ (f)
#Line 2
s.writerow(['RollNo','Name','Mark
s'])
rec=[]
while True:
for i in rec:
s.______(i)
#Line 4
f.close()
import csv
# Function to add a new record in CSV
file
def _________(Country,Currency):
# Statement-1
f=open("CURRENCY.CSV","__")
# Statement-2
fwriter=csv.writer(f)
fwriter.writerow([_____])
# Statement-3
f.close()
# Function to display all records from
CSV file
def ShowRec():
with open("CURRENCY.CSV","r") as
NF:
NewReader=csv._____ (NF)
# Statement-4
for rec in NewReader:
if len(rec)!=0:
print(rec[0],rec[1])
AddNewRec("INDIA","RUPEE")
AddNewRec("JAPAN","YEN")
ANSWER
CBQ NO Answer
1.a. (D) import csv
1.b (C) “Employee.csv”, ’w’
1.c (D) writer(Csvfile)
1.d (A) Empno, Name, Desig, Salary
1.e (C) append(records)
1.f (D) CsvObj.writerows()
2.a (A) pickle 51
2.b (B) open ‘wb’
2.c (C) dump(records,file)
2.d (D) pickle.load(f)
2.e (C) total_Amt+=R[4]
2.f (B) except
3 1. D
3 2. C
3 3. B
3 4. A
3 5. B
4 i) (A) a
ii) (C) fil_str+’\n’
iii) (B) writelines(l)
iv) (D) fout.close()
5 i) (C) open(‘story.txt’,’a’)
ii) (D) write(fil_str)
iii) (D) fout.close()
iv) (B) PYTHON IS AN EASY LANGUAGE
PYTHON IS POPULAR LANGUAGE
6. B
7. A
8. i C ii B iii B
9. i B ii C iii B
10 1. b) r
10 2. b) read(5)
10 3. b) file.readlines()
10 4. b) len(line)
10 5. a) file.close()
11. Answer
(i) D
(ii) C
(iii) C
(iv) D
(v) C
12. Answer
(i) C
(ii) B
(iii) B
(iv) D
(v) D
13. 1 A
13. 2 A
13. 3 A
13. 4 A
13. 5 C
14. 1 B
14. 2 A
14. 3 D
14. 4 A
14. 5 C
15
(i.) A
(ii.) A
(iii.) B
(iv.) C
(v.) B
16.
(i.) B
(ii.) D
(iii.) A
(iv.) D
22. B
Region- Kolkata
E-mail ID - kamalkant.kv@gmail.com
Name of the Chapter-
Q11. When an existing file is opened in appending mode than KNOWLEDGE AND
file pointer is present at the end of the file. UNDERSTANDING
Q12. If a file is opened in r+, w+ or a+ mode, it can be used for KNOWLEDGE AND
both reading and writing. UNDERSTANDING
Q13. When we are writing strings using the write() method into KNOWLEDGE AND
a file one by one using iteration then a newline character UNDERSTANDING
is appended to each string by default so that all strings are
written in different lines in the file.
Q14 If a file is opened with “with open() as f” syntax then APPLICATION
after the control exiting the “with open()” block the file
get closed itself. (where f is file stream object)
Q15 A user wants to open a file ‘story.txt’ without losing the ANALYSIS,
content of the file and wants to continue writing after the EVALUATION AND
existing content. Also, if a file does not exist the file CREATION
should be created itself. He/she opened the file with
syntax :
f = open(“story.txt”, ‘r+”)
Is this statement true or false?
Q16. The file open using the with statement will close Understanding
automatically after with block is over.
Q17. The file mode “w” & “w+” are the same. Knowledge
Q18. When open a file into append mode , if the file does not Knowledge
exist , an error has been occurred.
Q19. The read( ) function reads the entire file at a time. Understanding
Q20 The readlines( ) function read the entire file in a list of Analysis
strings where each line is stored as one string.
Q21 Text file mode ‘a’ is in write only mode? KNOWLEDGE
(A)True (B) False
Q22. Text file mode ‘w+’ refer to write and read mode file? KNOWLEDGE
(A)True (B) False
Q23. A close() function breaks the link of file –object? KNOWLEDGE
(A)True (B) False
Q24. The following syntax is not correct- KNOWLEDGE
Myfile=open(r’E:\poem.txt’,”r”)
a) True b) False
a) True b) False
a) True b) False
Q30. dump( ) method is used to read data from a binary file Understanding
a) True b) False
Q31 read() method will read all the line of text file and return understanding
them in the form of list
(True/False)
Q32 Tell() method will identify the current location of file understanding
pointer in the file (True/False)
Q33 Open() method in read mode will create the file understanding
(True/False)
Q34 Readlines() method will returns the text file contents in understanding
the from of list of lines
(True/False)
Q35 Readline() method will returns the text file contents in the understanding
from of list of lines
(True/False)
Q36 f.seek (10,0) will move 10 bytes forward from beginning of file. Application
(A) True
(B) False
Q37 readlines( ) function returns all the words of the file in the Understanding
form of List. (T/F)
(A) True
(B) False
Q38 seek() function returns the current position of the file pointer Knowledge
(A) True
(B) False
Q39 readchar() function reads the all characters from the file Knowedge
(A) True
(B) False
Q50 The Binary files are used to store large data such as Analysis
images, video files, audio files etc.
Q51 Binary file stores information in ASCII or UNICODE Understanding
characters
A) TRUE
B) FALSE
Q52 The flush function forces the writing of data on the disk Understanding
still pending in output buffer.
A) TRUE
B) FALSE
Q53 Every line is a binary file is terminated with a EOL Knowledge
character.
A) TRUE
B) FALSE
Q54 The tell method tells the current position of the file Knowledge
pointer within the file
A) TRUE
B) FALSE
Q55 The dump method is used to read data from the binary file Knowledge
A) TRUE
B) FALSE
Q56 The pickle module can be used to directly store the Awareness about pickle
python module
objects onto the disk file.
Q57 pickle.loads( ) read the next line and store into disk file. Knowledge of basic
working of load( )
Q58 Deserialisation of records from file through pickle Awareness about basic
module should be guarded through try.. catch .. block. coding steps of using
pickle
Q59 A binary file 'employee.dat' contains 100 records, to add a Know about opening
new record into file, the file should be opened in 'wb' modes in case of binary
mode. file
Q60 Pickling is the process by which a Python object is Knowledge about saving
converted to a byte stream. process through pickle
module
Q61 A binary file can store data in the form of 0 and 1 Understanding
a) True
b) False
Q62 File object and file handle is the same thing Knowledge
a) True
b) False
Q63. Pickle.dump() function is use for writing data in binary Application
file.
a) True
b) False
Q64. pickle module contains read() and write() function. Knowledge
a) True
b) False
Q65. wb+ and w+b are same in respect of binary file. Knowledge
a) True
b) False
Q66. The load() function of the pickle module perform Evaluation
pickling.
a) TRUE
b) FALSE
Q67 File.seek(5,0) will move 5 bytes forward from the Analysis
beginning of the file.
a) TRUE
b) FALSE
Q68 There is a delimiter to end a line in Binary File. Knowledge
a) TRUE
b) FALSE
Q69 tell() method of Python tells us the current position Analysis
within the file.
a) TRUE
b) FALSE
Q70 When you open a file for writing, if the file exists, the Analysis
existing file is overwritten with the new file.
a) TRUE
b) FALSE
Q71 We cannot insert multiple rows in csv file using python Application
csv module
Q72 The default line terminator is \n in csv file Understanding
Q73 We can import csv module functions in following Creation
manner:
from csv import writerow, reader
Q74 A CSV file is a plain text file that can be opened in all kid Understanding
of programs
Q75 We have to insert header row in csv file using write() function Evaluation
as it is not included by default
Q76 A csv file is a binary file. Knowledge
(A) True
(B) False
Q77. The separator character of csv files is called delimiter. Knowledge
(A) True
(B) False
Q78 The csv files only take a comma as delimiter. Knowledge
(A) True
(B) False
Q79 You cannot insert multiple rows in a csv file using the csv Application
module.
(A) True
(B) False
Q80 You can import csv module functions for reading from Application
csv file and writing into csv file in the following manner:
from csv import writer, reader
(A) True
(B) False
ANSWER
Question No Answer
Q1 (B) False
Q2 (A) True
Q3. (A) True
Q4. (A) True
Q5. (A) True
Q6. True
Q7. True
Q8. False
Q9. True
Q10. False
Q11. True
Q12. True
Q13. False
Q14 True
Q15 False
Q16. True
Q17. False
Q18. False
Q19. True
Q20 True
Q21 True
Q22. True
Q23. True
Q24. False
Q25. False
Q26. True
Q27. True
Q28. True
Q29. False
Q30. False
Q31 False
Q32 True
Q33 False
Q34 True
Q35 False
Q36 A
Q37 A
Q38 B
Q39 B
Q40 B
Q41 False
Q42 False
Q43 False
Q44. True
Q45. True
Q46 True
Q47 True
Q48 False
Q49 False
Q50 True
Q51 FALSE
Q52 TRUE
Q53 FALSE
Q54 TRUE
Q55 FALSE
Q56 True
Q57 False
Q58 True
Q59 False
Q60 True
Q61 False
Q62 True
Q63. True
Q64. False
Q65. True
Q66. FALSE
Q67 TRUE
Q68 FALSE
Q69 TRUE
Q70 TRUE
Q71 False
Q72 True
Q73 True
Q74 False
Q75 True
Q76 b) False
Q77. a) True
Q78 b) False
Q79 b) False
Q80 a) True
Region- KOLKATA
E-mail ID - kamalkant.kv@gmail.com