Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Database System Concepts
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Starting Out with Python [4th Edition]
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals [11th Edition]
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program [8th Edition]
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education
Database System Concepts
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Starting Out with Python [4th Edition]
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals [11th Edition]
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program [8th Edition]
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education
This set of Python Multiple Choice Questions & Answers [MCQs] focuses on “files”. 1. To open a file c:\scores.txt for reading, we use _____________ Answer: b 2. To open a file c:\scores.txt for writing, we use ____________ Answer: b 3. To open a file c:\scores.txt for appending data, we use ____________ Answer: a 4. Which of the following statements are true? Answer: d 5. To read two characters from a file object infile, we use ____________ Answer: a 6. To read the entire remaining contents of the file as a string from a file object infile, we use ____________ Answer: b 7. What will be the output of the following Python code?
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.
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.
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.
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.
a] infile.read[2]
b] infile.read[]
c] infile.readline[]
d] infile.readlines[]
View Answer
Explanation: Execute in the shell to verify.
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.
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.
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.
Sanfoundry Global Education & Learning Series – Python.
To practice all areas of Python, here is complete set of 1000+ Multiple Choice Questions and Answers.
Next Steps:
- Get Free Certificate of Merit in Python Programming
- Participate in Python Programming Certification Contest
- Become a Top Ranker in Python Programming
- Take Python Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
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 & technical discussions at Telegram SanfoundryClasses.