Programming and Problem Solving Through Python Language Theory Question
File Processing - Set 3
Which is the function to read the remaining lines of the file from a file object infile ?
infile.read(2)
infile.read()
infile.readlines()
Answer : B
Explain : infile.read() - reads the remaing line of file.
Which function is used to read all the characters?
readall()
read()
readcharacters()
readchar()
Answer : B
Explain : read() is used to read the entire document.
A function used for writing data in the binary format :
write
output
send
dump
Answer : D
Explain : dump is used to write in binary file where as load is use to display binary file.
Which of the following is an invalid mode ?
a
ar+
r+
w
Answer : B
Explain : a mode - open an existing file for append operation, r+ - open the file reading and writing the pointer at begin, and w - write the file.
Ravi opened a file in python using open( ) function but forgot to specify the mode. In which mode the file will open ?
write
append
read
Both read and write
Answer : C
Explain : The default file access mode is read mode.
Fill in the blank. Import pickle f=open(“data.dat”, „rb‟) d=_____________.load(f) f.close()
unpickle
pickling
pickle
pick
Answer : C
Explain : pickle mdodule is used to store data in binary file
Which statement will move file pointer 10 bytes backward from current position ?
f.seek(-10,0)
f.seek(-10,1)
f.seek(10,0)
None of the above
Answer : B
Explain : In this f.seek(-10,1) f is file name , -10 mean backward 10 bytes and 1 is use for current position
In f.seek(offset, whence), A whence value of 0 means :
from the beginning of the file.
current file position
the end of the file as the reference point
None of these
Answer : A
Explain : Whence value 0 means - starting point , 1 values means current position of cursor and 2 means the end of file.
Which statement will return one line from a file (file object is „f‟) ?
f.readlines()
f.readline()
f.read()
f.line()
Answer :readline read only one line of a file. B
Explain :
Which mode creates a new file if the file does not exist ?
write mode
append mode
both (A) & (B)
none of the above
Answer : C
Explain : If a file does not exist, append mode and write creates the file. The key difference between write and append modes is that append does not clear a file's contents.
Which function opens file in python ?
open()
Open()
new()
None of the above
Answer :A
Explain : open() function opens a file, and returns it as a file object.
Which statement will read 5 characters from a file(file object „f‟) ?
f.read()
f.read(5)
f.reads(5)
None of above
Answer : B
Explain : read(5) is use to read only 5 characters.
To read all contents from file object FILE at once we may use
FILE.read(*)
FILE.readlines()
FILE.read()
D.FILE.read()
Answer : C
Explain : read() method is used to read file at once.