NEW
Font size
WorksheetsPython File handling - 2
Total questions: 20
Worksheet time: 15mins
Which of the following command is used to open a file “c:\temp.txt” in read-mode only?
infile = open(“c:\temp.txt”, “r”)
infile = open(“c:\\temp.txt”, “r”)
infile = open(file = “c:\temp.txt”, “r+”)
infile = open(file = “c:\\temp.txt”, “r+”)
Which of the following command is used to open a file “c:\temp.txt” in write-mode only?
outfile = open(“c:\temp.txt”, “w”)
outfile = open(“c:\\temp.txt”, “w”)
outfile = open(file = “c:\temp.txt”, “w+”)
outfile = open(file = “c:\\temp.txt”, “w+”)
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”, “w+”)
outfile = open(“c:\\temp.txt”, “a”)
outfile = open(“c:\\temp.txt”, “r+”)
Which of the following statements are true regarding the opening modes of a file?
A. When you open a file for reading, if the file does not exist, an error occurs.
B. When you open a file for writing, if the file does not exist, an error occurs.
C. When you open a file for reading, if the file does not exist, the program will open an empty file.
D. When you open a file for writing, if the file does not exist, a new file is created.
E. When you open a file for writing, if the file exists, the existing file is overwritten with the new file.
Only A
A and B
A, D and E
A, B and E
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 commands can be used to read the entire contents of a file as a string using the file object <tmpfile>?
tmpfile.read(n)
tmpfile.read()
tmpfile.readline()
tmpfile.readlines()
Which of the following commands can be used to read the next line in a file using the file object <tmpfile>?
tmpfile.read(n)
tmpfile.read()
tmpfile.readline()
tmpfile.readlines()
What does the <readlines()> method returns?
str
a list of lines
list of single characters
list of integers
Which of the following commands can be used to read the remaining lines in a file using the file object <tmpfile>?
tmpfile.read(n)
tmpfile.read()
tmpfile.readline()
tmpfile.readlines()
What are the two built-in functions to read a line of text from standard input, which is by default the keyboard?
Input
Read
Scanner
What will be the output of the following code snippet?
TechBeamers
Hello viewers!!
Name of the file: myfile.txt
TechBeamers
Hello Viewers!!
TechBeamers Hello viewers!!
Syntax Error
What will be the output of the following code snippet?
TechBeamers
Hello viewers!!
Name of the file: myfile.txt
TechBeamers
Hello Viewers!!
TechBeamers Hello viewers!!
Syntax Error
What will be the output of the following code snippet?
Assuming that input given by user is :Tech Beamers
Tech Beamers
Tech
Tech
Beamers
Syntax Error
What will be the output of the following code snippet?
Runtime Error
Hello World how are you today
[‘Hello’, ‘World’, ‘how’, ‘are’, ‘you’, ‘today’]
Hello
What is the correct syntax of open() function?
file_name = open(file_name [, access_mode][, buffering])
file_object = open(file_name [, access_mode][, buffering])
file object = open(file_name)
None of the above
What will be the output of the following code snippet?
Runtime Error
True
False
Hello world
What will be the output of the following code snippet?
Runtime Error
[‘This is 1st line\n’, ‘ This is 2nd line\n’, ‘This is 3rd line’]
[‘ This is 2nd line\n’, ‘This is 3rd line’]
[ ]
Which of the following statements is correct regarding the seek() method?
sets the file position to the end of file
position of file pointer remains unchanged
sets the file's current position at the offset
the file pointer is set to the start of the file
What will be the output of the following code snippet?
red
yellow
blue
[‘red\n’, ‘yellow\n’, ‘blue\n’]
Error: I/O operation on closed file
Compilation error
What is the use of tell() method in python?
tells you the current position within the file
tells you the end position within the file
tells you the file is opened or not
none of the mentioned
