WorksheetsData file handling
Total questions: 89
Worksheet time: 42mins
(a)
Mode in the following program to open file for writing in a mode where new data will be added at the end of old data
file =open("poem.txt","__")
r
a
w
x
see the program and fill in the blank with correct statement.
(a)
Choose the correct statement to close a file stream named myfile:
myfile.terminate()
myfile.end()
myfile.close()
myfile.remove()
See the following code snippet and fill in the blank with appropriate statement
(a)
Fill in the blank with appropriate statement.
(a)
which function is used to read records from a binary file?
pickle.load(f)
pickle.read()
pickle.open()
pickle.dump(l,f)
write statement to use the required module for working with comma separated values (csv) files.
(a)
Write the appropriate statement in the blank.
(a)
Which statement(s) is/are true to open a binary file "Record.dat" for reading and writing.
file =open("Record.dat","wb")
file =open("Record.dat","rb")
file =open("Record.dat","ab")
file =open("Record.dat","rb+")
What will be the output-
f=open('abc.txt','a')
text="xyz"
f.write(text)
f.close()
Writing into the file without erasing old content
Writing into the file by erasing old content
reading from the file without erasing old content
reading from the file by erasing old content
Mode in the following program to open file for writing in a mode where new data will be added at the end of old data
file =open("poem.txt","__")
r
w
a
r+
Choose the correct statement to close a file stream named myfile:
myfile.terminate()
myfile.end()
myfile.close()
myfile.remove()
Fill in the blank with appropriate statement.
(a)
Write the appropriate statement in the blank.
(a)
Which statement(s) is/are true to open a binary file "Record.dat" for reading and writing.
file =open("Record.dat","wb")
file =open("Record.dat","rb")
file =open("Record.dat","ab")
file =open("Record.dat","rb+")
Which function read single line at a time?
read()
read(n)
readline()
readlines()
Which file is not binary file?
student.mp4
student.jpeg
student.csv
student.exe
Where files are store permanently?
RAM
Hard Disk
Cache memory
None of these
Which function is used to write multiple lines in text file?
write()
writelines()
lines()
print()
Which statement(s) is/are true to open a binary file "Record.dat" for reading and writing ?
file =open("Record.dat","wb")
file =open("Record.dat","rb")
file =open("Record.dat","ab")
file =open("Record.dat","rb+")
Q.2 Which of the following statements are not true regarding the opening modes of a file?
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, 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.
Q.3 Which module is used to work with binary files?
CSV module
pickle module
math module
binary module
Q.5 'try' block is used to
execute a code
through the exceptions
to try some random code
to give errors
Function used to write data in binary format
write()
output()
load()
dump()
What is the use of “a” in file handling?
Read
Write
Append
None of the mentioned
Select the statement to open a file named MYDATA.dat in read binary mode. Let the file object name be myfile.
myfile=open("MYDATA.dat","rb")
myfile=open("MYDATA.dat","r+")
myfile=open("MYDATA.dat","r")
Mode in the following program to open file for writing in a mode where new data will be added at the end of old data
file =open("poem.txt","__")
r
w
a
r+
Choose the correct statement to close a file stream named myfile:
myfile.terminate()
myfile.end()
myfile.close()
myfile.remove()
What will be the ouput -
f=open('abc.txt','w')
x=f.read()
f.close()
print(x)
Error(not readable)
Error(not writable)
It will read all the data of the file.
none
What will be the output-
f=open('abc.txt','a')
text="xyz"
f.write(text)
f.close()
Writing into the file without erasing old content
Writing into the file by erasing old content
reading from the file without erasing old content
reading from the file by erasing old content
Which function is used to write multiple lines in text file?
write()
writelines()
lines()
print()
Which statement(s) is/are true to open a binary file "Record.dat" for reading and writing ?
file =open("Record.dat","wb")
file =open("Record.dat","rb")
file =open("Record.dat","ab")
file =open("Record.dat","rb+")
The readlines() method returns
a. str
b. a list of lines
c. a list of single characters
d. a list of integers
An absolute path name begins at the
leaf
stem
root
current directory
What will be the output of the following Python code?
True
False
None
error
Q1. Given the following file opened as song_file:
I can't write one song that's not about you
Can't drink without thinkin' about you
Is it too late to tell you that
Everything means nothing if I can't have you?
What is the result of
print(song_file.read(4)) ?
I ca
I can
I c
I can't write one song that's not about you
Can't drink without thinkin' about
I c
you
Is it too late to tell you that
Everything means nothing if I can't have you?
Given the following file opened as song_file:
I can't write one song that's not about you
Can't drink without thinkin' about you
Is it too late to tell you that
Everything means nothing if I can't have you?
What is the result of
print(text_file.readlines()[1]) ?
I can't write one song that's not about you
Can't drink without thinkin' about you
I can't write one song that's not about you
Can't drink without thinkin' about
you
Is it too late to tell you that
Everything means nothing if I can't have you?
I
What is the correct way to open the file file_in.txt for reading?
open('file_in.txt','a')
open('file_in.txt','r+')
open('file_in.txt', 'r')
open('file_in.txt','w')
What is the best way to read an entire file into a single string?
.read()
.readline()
.readlines()
.read(all)
What is the best way to read an entire file into a list of strings?
.read()
.readline()
.readlines()
.read(all)
What is the best way to read the first line of a file?
.read()
.readline()
.readlines()
.read(all)
When reading text files, what are the hidden characters typically at the end of each line?
\n
»
#
@
This command will open MyFile = open('MyText.txt', 'r')
To write to the file
To read the file
To append to the file
If I want to add to the end of a text file, I should:
MyCipher = open("MyCipher.txt", "w")
MyCipher = open("MyCipher.txt", "a")
MyCipher = open("MyCipher.txt", "w+")
MyCipher = open("MyCipher.txt", "s")
Which of these will generate an error?
MyCipher.write("writing")
MyCipher..write(writing)
MyCipher.write("writing",writing)
MyCipher.readline()
How do you close the file?
MyFile = close()
MyFile.close
close = MyFile()
MyFile.close()
Which is the best type of loop to write a file in?
While loop
for loop
repeat loop
Choose the correct statement to close a file stream named myfile:
myfile.terminate()
myfile.end()
myfile.close()
myfile.remove()
Why is it best to open a file with "with" in Python?
It automatically closes the file
Easier to read the code
Better for binary files
Closers to the pseudocode
Given a text file opened as file_in, which of the following will print the entire file? (Select ALL that apply.)
print(file_in.read())
lines = file_in.readlines()
print(file_in)
lines = file_in.readlines()
for line in lines:
print(line)
for i in range(len(lines)):
print(lines)
for line in file_in:
print(line)
What is the use of “a” in file handling?
Read
Write
Append
None of the mentioned
Which function is used to read single line from file?
Readline()
Readlines()
Readstatement()
Readfullline()
Which function is used to close a file in python?
Close()
Stop()
End()
Closefile()
Is it possible to create a text file in python?
Yes
No
Machine dependent
All of the mentioned
What is the use of seek() method in files?
sets the file’s current position at the offset
sets the file’s previous position at the offset
sets the file’s current position within the file
none of the mentioned
Which of the following mode will refer to binary data?
r
w
+
b
EOL stands for
End Of Line
End Of List
End of Lines
End Of Location
Which of the following is nor a proper file access mode?
close
read
write
append
In f=open(“data.txt”, “r”), r refers to __________.
File handle
File object
File Mode
Buffer
Which of the following functions do you use to write data in the binary format?
write
output
dump
send
Which of the following command is used to open a file “c:\temp.txt” for reading in binary format only?
outfile = open(“c:\temp.txt”, “r”)
outfile = open(“c:\\temp.txt”, “rb”)
outfile = open(“c:\temp.txt”, “r+”)
outfile = open(“c:\\temp.txt”, “rb+”)
Which of the following commands can be used to read “n” number of characters from a file using the file object <file>?
file.read(n)
n = file.read()
file.readline(n)
file.readlines()
Which of the following statements are true regarding the opening modes of a file?
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, an error occurs.
When you open a file for reading, if the file does not exist, the program will open an empty file.
Which of the following command is used to open a file “c:\temp.txt” in append-mode?
outfile = open(“c:/temp.txt”, “a”)
outfile = open(“c:\\temp.txt”, “rw”)
outfile = open(“c:\temp.txt”, “w+”)
outfile = open(“c:\\temp.txt”, “r+”)
outfile = open(“c:\\temp.txt”, “a”)
Mode in the following program to open file for writing in a mode where new data will be added at the end of old data
file =open("poem.txt","__")
r
a
w
x
Which file execute faster?
Text file
Binary file
CSV file
Mode used for reading and writing in binary file
wb
w
wb+
w+
Function used to write data in binary format
write()
output()
load()
dump()
The type of operation possible in an opened file is governed by
File Object
File Mode
File Path
open() Method
Module used to read and write into binary files
dump
load
pickle
open
Function returns the current position of file pointer in the file
tell()
seek()
read()
flush()
The default file - open mode is (a) mode.
Which of the following is not a valid attribute of a file object (fp)?
fp.name
fp.closed
fp.size
fp.mode
How do you change the file position to an offset value from the start?
fp.seek(offset, 0)
fp.seek(offset, 1)
fp.seek(offset, 2)
none of the mentioned
This command will open MyFile = open("MyText.txt")
To read the file
To append to the file
To write to the file
If I want to add to the end of a text file, I should:
MyCipher = open("MyCipher.txt", "w")
MyCipher = open("MyCipher.txt", "a")
MyCipher = open("MyCipher.txt", "w+")
MyCipher = open("MyCipher.txt", "s")
Which of these will generate an error?
MyCipher.write("writing")
MyCipher..write(writing)
MyCipher.write("writing",writing)
MyCipher.readline()
How do you close the file?
MyFile = close()
MyFile.close
close = MyFile()
MyFile.close()
Why is it best to open a file with with in Python?
It automatically closes the file
Easier to read the code
Better for binary files
Closers to the pseudocode
Which is the best type of loop to write a file in?
While loop
for loop
repeat loop
What command can you use to separate parts of a line?
separate()
split()
divide()
cut()
What command can you use to send back data from a function?
return
sendback
back
print()
How do you assign variables in pseudocode?
←
=
==
assign
How do you CALL the function name in Python?
name()
def name()
call name()
GOTO name()
