File Handling MCQ 2 Aug 7
File Handling MCQ 2 Aug 7
Practice Questions:
Multiple Choice Questions:
1. To open a file c:\scores.txt for reading, we use _____________
a) infile = open(“c:\scores.txt”, “r”)
b) infile = open(“c:\\scores.txt”, “r”)
c) infile = open(file = “c:\scores.txt”, “r”)
d) infile = open(file = “c:\\scores.txt”, “r”)
Explanation: The WITH statement when used with open file guarantees that the file
object is closed when the with block exits.
8. To read the next line of the file from a file object infile, we use ____________
a) infile.read(2)
b) infile.read()
c) infile.readline()
d) infile.readlines()
Explanation: Python provides two built-in functions to read a line of text from standard
input, which by default comes from the keyboard. These functions are:
raw_input and input
Explanation: rename is not the attribute of file rest all are files attributes.
Attribute Description
file.closed Returns true if file is closed, false otherwise.
file.mode Returns access mode with which file was opened.
file.name Returns name of the file.
file.softspace Returns false if space explicitly required with print, true otherwise.
Explanation: The tell() method tells you the current position within the file; in other
words, the next read or write will occur at that many bytes from the beginning of the file.
15. What is the current syntax of rename() a file?
a) rename(current_file_name, new_file_name)
b) rename(new_file_name, current_file_name,)
c) rename(()(current_file_name, new_file_name))
d) none of the mentioned
Explanation: remove(file_name)
Explanation: It displays the output as shown below. The method next() is used when a
file is used as an iterator, typically in a loop, the next() method is called repeatedly. This
method returns the next input line, or raises StopIteration when EOF is hit.
Output:
Name of the file: foo.txt
Line No 0 - This is 1st line
Explanation: Sets the file’s current position at the offset. The method seek() sets the file’s
current position at the offset.
Following is the syntax for seek() method:
fileObject.seek(offset[, whence])
Parameters
offset — This is the position of the read/write pointer within the file.
whence — This is optional and defaults to 0 which means absolute file positioning, other
values are 1 which means seek relative to the current position and 2 means seek relative
to the file’s end.
19. What is the use of truncate() method in file?
a) truncates the file size
b) deletes the content of the file
c) deletes the file size
d) none of the mentioned
Explanation: The method truncate() truncates the file size. Following is the syntax for
truncate() method:
fileObject.truncate( [ size ])
Parameters
size — If this optional argument is present, the file is truncated to (at most) that size.
20. Which is/are the basic I/O connections in file?
a) Standard Input
b) Standard Output
c) Standard Errors
Arsha Vidya Mandir
Lecture Notes -2023-24
d) All of the mentioned
Explanation: Standard input, standard output and standard error. Standard input is the
data that goes to the program. The standard input comes from a keyboard. Standard
output is where we print our data with the print keyword. Unless redirected, it is the
terminal console. The standard error is a stream where programs write their error
messages. It is usually the text terminal.
Explanation: Pickle is the standard mechanism for object serialization. Pickle uses a
simple stack-based virtual machine that records the instructions used to reconstruct the
object. This makes pickle vulnerable to security risks by malformed or maliciously
constructed data, that may cause the deserializer to import arbitrary modules and
instantiate any object.
23. What is unpickling?
a) It is used for object serialization
b) It is used for object deserialization
c) None of the mentioned
d) All of the mentioned
Explanation: We have been working with simple textual data. What if we are working
with objects rather than simple text? For such situations, we can use the pickle module.
Arsha Vidya Mandir
Lecture Notes -2023-24
This module serializes Python objects. The Python objects are converted into byte
streams and written to text files. This process is called pickling. The inverse operation,
reading from a file and reconstructing objects is called deserializing or unpickling.
Explanation: Open() function correct syntax with the parameter details as shown below:
file object = open(file_name [, access_mode][, buffering])
Here is parameters’ detail:
file_name: The file_name argument is a string value that contains the name of the file
that you want to access.
access_mode: The access_mode determines the mode in which the file has to be opened,
i.e., read, write, append, etc. A complete list of possible values is given below in the
table. This is optional parameter and the default file access mode is read (r).
buffering: If the buffering value is set to 0, no buffering will take place. If the buffering
value is 1, line buffering will be performed while accessing a file. If you specify the
buffering value as an integer greater than 1, then buffering action will be performed with
the indicated buffer size. If negative, the buffer size is the system default(default
behavior).
25. What will be the output of the following Python code?
1. fo = open("foo.txt", "wb")
2. print "Name of the file: ", fo.name
3. fo.flush()
4. fo.close()
a) Compilation Error
b) Runtime Error
c) No Output
d) Flushes the file when closing them
Explanation: The method flush() flushes the internal buffer. Python automatically flushes
the files when closing them. But you may want to flush the data before closing any file.
26. Correct syntax of file.writelines() is?
a) file.writelines(sequence)
b) fileObject.writelines()
Arsha Vidya Mandir
Lecture Notes -2023-24
c) fileObject.writelines(sequence)
d) none of the mentioned
Explanation: The method writelines() writes a sequence of strings to the file. The
sequence can be any iterable object producing strings, typically a list of strings. There is
no return value.
Syntax
Following is the syntax for writelines() method:
fileObject.writelines( sequence ).
Explanation: The method readlines() reads until EOF using readline() and returns a list
containing the lines. If the optional sizehint argument is present, instead of reading up to
EOF, whole lines totalling approximately sizehint bytes (possibly after rounding up to an
internal buffer size) are read.
Syntax
Following is the syntax for readlines() method:
fileObject.readlines( sizehint );
Parameters
sizehint — This is the number of bytes to be read from the file.
28. In file handling, what does this terms means “r, a”?
a) read, append
b) append, read
c) write, append
d) none of the mentioned
Explanation: This opens the file for writing. It will create the file if it doesn’t exist, and if
Arsha Vidya Mandir
Lecture Notes -2023-24
it does, it will overwrite it.
fh = open(“filename_here”, “w”).
Explanation: The readline function reads a single line from the file fh = open(“filename”,
“r”)
content = fh.readline().
Explanation: With the writeline function you can write a list of strings to a file
fh = open(“hello.txt”, “w”)
lines_of_text = [“a line of text”, “another line of text”, “a third line”]
fh.writelines(lines_of_text).
Arsha Vidya Mandir
Lecture Notes -2023-24
34. Which of the following are the modes of both writing and reading in binary format in
file?
a) wb+
b) w
c) wb
d) w+
Explanation: Use r+, w+ or a+ to perform both read and write operations using a single
file object.
Explanation: In r+ the pointer is initially placed at the beginning of the file and the
pointer is at the end for w+.
37. How do you get the name of a file from a file object (fp)?
a) fp.name
b) fp.file(name)
c) self.__name__(fp)
d) fp.__name__()
Arsha Vidya Mandir
Lecture Notes -2023-24
38. How do you get the current position within the file?
a) fp.seek()
b) fp.tell()
c) fp.loc
d) fp.pos
Explanation: It gives the current position as an offset from the start of file.
39. How do you change the file position 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 mentioned