This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “files”.
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”)
View Answer
Explanation: Execute help(open) to get more details.
2. To open a file c:\scores.txt for writing, we use ____________
a) outfile = open(“c:\scores.txt”, “w”)
b) outfile = open(“c:\\scores.txt”, “w”)
c) outfile = open(file = “c:\scores.txt”, “w”)
d) outfile = open(file = “c:\\scores.txt”, “w”)
View Answer
Explanation: w is used to indicate that file is to be written to.
3. To open a file c:\scores.txt for appending data, we use ____________
a) outfile = open(“c:\\scores.txt”, “a”)
b) outfile = open(“c:\\scores.txt”, “rw”)
c) outfile = open(file = “c:\scores.txt”, “w”)
d) outfile = open(file = “c:\\scores.txt”, “w”)
View Answer
Explanation: a is used to indicate that data is to be appended.
4. Which of the following statements are true?
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, a new file is created
c) When you open a file for writing, if the file exists, the existing file is overwritten with the new file
d) All of the mentioned
View Answer
Explanation: The program will throw an error.
5. To read two characters from a file object infile, we use ____________
a) infile.read(2)
b) infile.read()
c) infile.readline()
d) infile.readlines()
View Answer
Explanation: Execute in the shell to verify.
6. To read the entire remaining contents of the file as a string from a file object infile, we use ____________
a) infile.read(2)
b) infile.read()
c) infile.readline()
d) infile.readlines()
View Answer
Explanation: read function is used to read all the lines in a file.
7. What will be the output of the following Python code?
f = None for i in range (5): with open("data.txt", "w") as f: if i > 2: break print(f.closed)
a) True
b) False
c) None
d) Error
View Answer
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()
View Answer
Explanation: Execute in the shell to verify.
9. To read the remaining lines of the file from a file object infile, we use ____________
a) infile.read(2)
b) infile.read()
c) infile.readline()
d) infile.readlines()
View Answer
Explanation: Execute in the shell to verify.
10. The readlines() method returns ____________
a) str
b) a list of lines
c) a list of single characters
d) a list of integers
View Answer
Explanation: Every line is stored in a list and returned.
More MCQs on Python Files:
Sanfoundry Global Education & Learning Series – Python.
To practice all areas of Python, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Practice Programming MCQs
- Apply for Python Internship
- Check Information Technology Books
- Apply for Programming Internship
- Check Python Books