This set of Python Multiple Choice Questions & Answers (MCQs) focuses 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
View Answer
Explanation: 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
View Answer
Explanation: 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
View Answer
Explanation: This opens the 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()
View Answer
Explanation: 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()
View Answer
Explanation: 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()
View Answer
Explanation: 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()
View Answer
Explanation: With the writelines() 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()
View Answer
Explanation: 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
View Answer
Explanation: 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 file\n”)
file.write(“and another line\n”)
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+
View Answer
Explanation: 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.
Sanfoundry Global Education & Learning Series – Python.
To practice all areas of Python, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Apply for Python Internship
- Check Python Books
- Apply for Programming Internship
- Practice Programming MCQs
- Check Information Technology Books