0% found this document useful (0 votes)
45 views5 pages

Python File Handling MCQs

Df

Uploaded by

jha430766
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views5 pages

Python File Handling MCQs

Df

Uploaded by

jha430766
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Multiple Choice Questions

a. Information stored on a storage device with a specific name is called a ...............


1. Array 2. Dictionary 3. File 4. tuple
b. Which of the following format of files can be created programmatically through Python to store
some data ?
1. Data files 2. Text files 3. Video files 4. Binary files
c. To open a file c:\ss.txt for appending data, we use
1. file = open("c:\\ss.txt", "a")
2. file = open("c:\\ss.txt", "rw")
3. file = open(r"c:\ss.txt", "a")
4. file = open(file = "c:\ss.txt", "w")
5. file = open(file = "c:\ss.txt", "w")
6. file = open("c:\res.txt")
d. To read the next line of the file from a file object infi, we use
1. infi.read(all) 2. infi.read() 3. infi.readline() 4. infi.readlines()
e. To read the remaining lines of the file from a file object infi, we use
1. infi.read(all) 2. infi.read() 3. infi.readline() 4. infi.readlines()
f. The readlines() method returns
1. str 3. a list of lines
2. a list of single characters 4. a list of integers
g. Which of the following mode will refer to binary data ?
1. r 2. w 3. + 4. b
h. Which of the following statement is not correct ?
1. We can write content into a text file opened using 'w' mode.
2. We can write content into a text file opened using 'w+' mode.
3. We can write content into a text file opened using 'r' mode.
4. We can write content into a text file opened using 'r+' mode.
i. Which of the following option is the correct Python statement to read and display the first 10
characters of a text file "Notes.txt" ?
1. F = open('Notes.txt') ; print(F.load(10)) 3. F = open('Notes.txt') ; print(F.dump(10))
2. F = open('Notes.txt') ; print(F.read(10)) 4. F= open('Notes.txt') ; print(F.write(10))
j. Which of the following is not a correct Python statement to open a text file "Notes.txt" to write
content into it ?
1. F = open('Notes.txt', 'w') 3. F = open('Notes.txt., 'a')
2. F = open('Notes.txt', 'A') 4. F = open('Notes.txt', 'w+')
k. Which function is used to read all the characters ?
1. read() 2. readcharacters() 3. readall() 4. readchar()
l. Which function is used to read a single line from file ?
1. readline() 3. readlines()
2. readstatement() 4. readfullline()
m. Which function is used to write all the characters ?
1. write() 2. writecharacters() 3. writeall() 4. writechar()
n. Which function is used to write a list of strings in a file ?
1. writeline() 3. writelines()
2. writestatement() 4. writefullline()
o. Which of the following represents mode of both writing and reading in binary format in file. ?
1. wb+ 2. w 3. wb 4. w+
p. Which of the following is not a valid mode to open a file ?
1. ab 2. rw 3. r+ 4. w+
q. Which of the following mode in file opening statement results or generates an error if the file does
not exist ?
1. a+ 2. r+ 3. w+ 4. None of these
r. Which of the following command is used to open a file "c:\pat.txt" in read-mode only ?
1. fin = open("c:\pat.txt", "r") 3. fin = open("c:\\pat.txt", "r")
2. fin = open(file = "c:\pat.txt", "r+") 4. fin = open(file = "c:\\pat.txt", "r+")
s. Which of the following statements are true regarding the opening modes of a file ?
1. When you open a file for reading, if the file does not exist, an error occurs.
2. When you open a file for writing, if the file does not exist, an error occurs.
3. When you open a file for reading, if the file does not exist, the program will open an empty
file.
4. When you open a file for writing, if the file does not exist, a new file is created.
5. When you open a file for writing, if the file exists, the existing file is overwritten with the new
file.
t. Which of the following command is used to open a file "c:\pat.txt" for writing in binary format
only ?
1. fout = open("c:\pat.txt", "w") 3. fout = open("c:\\pat.txt", "wb")
2. fout = open("c:\pat.txt", "w+") 4. fout = open("c:\\pat.txt", "wb+")
u. Which of the following command is used to open a file "c:\pat.txt" for writing as well as reading in
binary format only ?
1. fout = open("c:\pat.txt", "w") 3. fout = open("c:\\pat.txt", "wb")
2. fout = open("c:\pat.txt", "w+") 4. fout = open("c:\\pat.txt", "wb+")
v. Which of the following functions do you use to write data in the binary format ?
1. write() 2. output() 3. dump() 4. send()
w. Which of the following option is the correct usage for the tell() of a file object ?
1. It places the file pointer at a desired offset in a file.
2. It returns the entire content of a file.
3. It returns the byte position of the file pointer as an integer.
4. It tells the details about the file.
x. Which of the following statement is incorrect in the context of pickled binary files ?
1. The csv module is used for reading and writing objects in binary files.
2. The pickle module is used for reading and writing objects in binary files.
3. The load() of the pickle module is used to read objects.
4. The dump() of the pickle module is used to write objects.
y. What is the significance of the seek() method ?
1. It seeks the absolute path of the file.
2. It tells the current byte position of the file pointer within the file.
3. It places the file pointer at a desired offset within the file.
4. It seeks the entire content of the file.
z. The correct syntax of seek() is :
1. file_object.seek(offset[, reference_point]) 3. seek(offset[, reference_point])
2. seek(offset, file_object) 4. seek.file_object(offset)

Fill in the Blanks


1. The default file-open mode is ____________ mode.
2. The ______ file mode will open a file for read and write purpose.
3. The ____ or ____ file mode will open a file for write and read purpose.
4. To close an open file, _____________ method is used.
5. To read all the file contents in the form of a list, _____________ method is used.
6. To write a list in a file, _____________ method may be used.
7. To force Python to write the contents of file buffer on to storage file, ___________ method
may be used.
8. To read and write into binary files, ________ module of Python is used.
9. The ___________ method of pickle module writes data into a binary file.
10.The ________ method of pickle module reads data from a binary file.
11.The conversion of an object hierarchy in byte stream is
called _____________ or _________________.
12.The character that separates the values in csv files is called the __________.
13.The default delimiter of csv files is ________.
14.We can suppress EOL translation in text file by giving _________________argument in
open().
15.The file mode to open a binary file for reading as well writing is __________ .
16.The file mode to open a binary file for writing as well reading is __________.
17.The file mode to open a csv file for reading as well writing is _______.
18.The file mode to open a csv file for appending as well reading is ________.
19.To specify a different delimiter while writing into a csv file, ______________ argument is
used with csv.writer().

True/False Questions
1. When you open a file for reading, if the file does not exist, an error occurs.
2. When you open a file for writing, if the file does not exist, an error occurs.
3. The absolute paths are from the topmost level of directory structure.
4. The relative paths are relative to the current working directory.
5. The relative path for a file always remains the same even after changing the directory.
6. The types of operations that can be carried out on a file depend upon the file mode a file is
opened in.
7. If no path is given with a file name in the file open(), then the file must exist in the current
directory.
8. Functions readline() and readlines() are essentially the same.
9. Python automatically flushes the file buffers before closing a file with close() function.
10.When you open a file for writing, if the file does not exist, a new file is created.
11.When you open a file for appending, if the file exists, the existing file is overwritten with the
new file.
12.The load() function of the pickle module performs pickling.
13.The csv files can only take comma as delimiter.
14.The csv files are text files.

Assertions and Reasons


i) Both (A) and (R) are correct and (R) is the correct explanation of (A).
ii) Both (A) and (R) are correct but (R) is NOT the correct explanation of (A).
iii) (A) is correct but (R) is incorrect.
iv) (A) is incorrect but (R) is correct.
a. Assertion. Python is said to have broadly two types of files - binary and text files, even when there
are CSV and TSV files also.
Reason. The CSV and TSV are types of delimited text files only where the delimiters are comma and
tab respectively.
b. Assertion. The file modes "r", "w", "a" work with text files, CSV files and TSV files alike.
Reason. The CSV and TSV are types of delimited text files only.
c. Assertion. The file modes "r", "w", "a" also reveal the type of file these are being used with.
Reason. The binary file modes have 'b' suffix with regular file modes.
d. Assertion. 'Pickling' is the process whereby a Python object hierarchy is converted into a byte-
stream.
Reason. A binary file works with byte-streams.
e. Assertion. 'Pickling' is the process whereby a Python object hierarchy is converted into a byte-
stream.
Reason. Pickling process is used to work with binary files as a binary file works with byte-streams.
f. Assertion. Every open file maintains a file-pointer and keeps track of its position after every
operation.
Reason. Every read and write operation takes place at the current position of the file pointer.
g. Assertion. CSV (Comma Separated Values) is a file format for data storage which looks like a text
file.
Reason. The information is organized with one record on each line and each field is separated by
comma.

Type A: Short Answer Questions/Conceptual Questions


a. What is the difference between "w" and "a" modes ?
b. What is the significance of a file-object ?
c. How is file open() function different from close() function?
d. Write statements to open a binary file C:\Myfiles\Text1.txt in read and write mode by
specifying file path in two different formats.
e. Which function is used with the csv module in Python to read the contents of a csv file into an
object ?
f. When a file is opened for output in write mode, what happens when
(i) the mentioned file does not exist
(ii) the mentioned file does exist ?
g. What role is played by file modes in file operations ? Describe the various file mode constants
and their meanings.
h. What are the advantages of saving data in :
i. (i) binary form (ii) text form (iii) csv files ?
j. When do you think text files should be preferred over binary files ?
k. Write a statement in Python to perform the following operations :
(i) To open a text file "BOOK.TXT" in read mode
(ii) To open a text file "BOOK.TXT" in write mode
l. When a file is opened for output in append mode, what happens when
(i) the mentioned file does not exist
(ii) the mentioned file does exist.
m. How many file objects would you need to create to manage the following situations ? Explain.
(i) to process three files sequentially
(ii) to merge two sorted files into a third file.
n. Is csv file different from a text file ? Why/why not ?
o. Why are csv files popular for data storage ?
p. How do you change the delimiter of a csv file while writing into it ?
q. If you rename a text file's extension as .csv, will it become a csvfile ? Why/why not ?

You might also like