250+ TOP MCQs on Python Files and Answers

Python Scripting Interview Questions & Answers on “Files”.

1. In file handling, what does this terms means “r, a”?
a) read, append
b) append, read
c) write, append
d) none of the mentioned
Answer: a
Clarification: r- reading, a-appending.

2. What is the use of “w” in file handling?
a) Read
b) Write
c) Append
d) None of the mentioned
Answer: b
Clarification: This opens the file for writing. It will create the file if it doesn’t exist, and if it does, it will overwrite it.
fh = open(“filename_here”, “w”).

3. What is the use of “a” in file handling?
a) Read
b) Write
c) Append
d) None of the mentioned
Answer: c
Clarification: This opens the fhe file in appending mode. That means, it will be open for writing and everything will be written to the end of the file.
fh =open(“filename_here”, “a”).

4. Which function is used to read all the characters?
a) Read()
b) Readcharacters()
c) Readall()
d) Readchar()
Answer: a
Clarification: The read function reads all characters fh = open(“filename”, “r”)
content = fh.read().

5. Which function is used to read single line from file?
a) Readline()
b) Readlines()
c) Readstatement()
d) Readfullline()
Answer: b
Clarification: The readline function reads a single line from the file fh = open(“filename”, “r”)
content = fh.readline().

6. Which function is used to write all the characters?
a) write()
b) writecharacters()
c) writeall()
d) writechar()
Answer: a
Clarification: To write a fixed sequence of characters to a file
fh = open(“hello.txt”,”w”)
write(“Hello World”).

7. Which function is used to write a list of string in a file?
a) writeline()
b) writelines()
c) writestatement()
d) writefullline()
Answer: a
Clarification: 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).

8. Which function is used to close a file in python?
a) Close()
b) Stop()
c) End()
d) Closefile()
Answer: a
Clarification: f.close()to close it and free up any system resources taken up by the open file.

9. Is it possible to create a text file in python?
a) Yes
b) No
c) Machine dependent
d) All of the mentioned
Answer: a
Clarification: Yes we can create a file in python. Creation of file is as shown below.
file = open(“newfile.txt”, “w”)
file.write(“hello world in the new filen”)
file.write(“and another linen”)
file.close().

10. 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+
Answer: a
Clarification: Here is the description below
“w” Opens a file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.
“wb” Opens a file for writing only in binary format. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.
“w+” Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.
“wb+” Opens a file for both writing and reading in binary format. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.

T scripting interview questions on Python,

Leave a Reply

Your email address will not be published. Required fields are marked *