Python Questions and Answers – Files – 1

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

Answer: b
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

Answer: b
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

Answer: a
Explanation: a is used to indicate that data is to be appended.
advertisement
advertisement

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

Answer: d
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

Answer: a
Explanation: Execute in the shell to verify.
Note: Join free Sanfoundry classes at Telegram or Youtube

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

Answer: b
Explanation: read function is used to read all the lines in a file.

7. What will be the output of the following Python code?

advertisement
  1. f = None
  2. for i in range (5):
  3.     with open("data.txt", "w") as f:
  4.         if i > 2:
  5.             break
  6. print(f.closed)

a) True
b) False
c) None
d) Error
View Answer

Answer: a
Explanation: The WITH statement when used with open file guarantees that the file object is closed when the with block exits.
advertisement

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

Answer: c
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

Answer: d
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

Answer: b
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.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.