Files - Python Questions and Answers - Sanfoundry PDF
Files - Python Questions and Answers - Sanfoundry PDF
(//www.sanfoundry.com)
See More
Practice Python questions and answers for interviews, campus placements, online tests, aptitude tests, quizzes and competitive exams.
Get Started
https://www.sanfoundry.com/python-mcqs-files-1/
Practice Test 3 (https://rank.sanfoundry.com/python-programming-practice-test-3/) 4/15
3/27/2019 Files - Python Questions and Answers - Sanfoundry
Practice Test 4 (https://rank.sanfoundry.com/python-programming-practice-test-4/)
Practice Test 5 (https://rank.sanfoundry.com/python-programming-practice-test-5/)
Practice Test 6 (https://rank.sanfoundry.com/python-programming-practice-test-6/)
Practice Test 7 (https://rank.sanfoundry.com/python-programming-practice-test-7/)
Practice Test 8 (https://rank.sanfoundry.com/python-programming-practice-test-8/)
Practice Test 9 (https://rank.sanfoundry.com/python-programming-practice-test-9/)
Practice Test 10 (https://rank.sanfoundry.com/python-programming-practice-test-10/)
Mock Test 1 (https://rank.sanfoundry.com/python-programming-mock-test-1/)
Mock Test 2 (https://rank.sanfoundry.com/python-programming-mock-test-2/)
Mock Test 3 (https://rank.sanfoundry.com/python-programming-mock-test-3/)
Mock Test 4 (https://rank.sanfoundry.com/python-programming-mock-test-4/)
Mock Test 5 (https://rank.sanfoundry.com/python-programming-mock-test-5/)
Mock Test 6 (https://rank.sanfoundry.com/python-programming-mock-test-6/)
Mock Test 7 (https://rank.sanfoundry.com/python-programming-mock-test-7/)
Mock Test 8 (https://rank.sanfoundry.com/python-programming-mock-test-8/)
Mock Test 9 (https://rank.sanfoundry.com/python-programming-mock-test-9/)
Mock Test 10 (https://rank.sanfoundry.com/python-programming-mock-test-10/)
This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “files”.
Answer: b
Explanation: Execute help(open) to get more details.
https://www.sanfoundry.com/python-mcqs-files-1/ 5/15
3/27/2019 Files - Python Questions and Answers - Sanfoundry
advertisement
Answer: b
Explanation: w is used to indicate that file is to be written to.
Answer: a
Explanation: a is used to indicate that data is to be appended.
Answer: d
Explanation: The program will throw an error.
https://www.sanfoundry.com/python-mcqs-files-1/ 6/15
5. To read
3/27/2019 two characters from a file object infile, we use Files - Python Questions and Answers - Sanfoundry
a) infile.read(2)
b) infile.read()
c) infile.readline()
d) infile.readlines()
View Answer
Answer: a
Explanation: Execute in the shell to verify.
advertisement
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.
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
https://www.sanfoundry.com/python-mcqs-files-1/ 7/15
Answer:
3/27/2019 a Files - Python Questions and Answers - Sanfoundry
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.
advertisement
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
https://www.sanfoundry.com/python-mcqs-files-1/ 8/15
Answer:
3/27/2019 d Files - Python Questions and Answers - Sanfoundry
Answer: b
Explanation: Every line is stored in a list and returned.
To practice all areas of Python, here is complete set of 1000+ Multiple Choice Questions and Answers (https://www.sanfoundry.com/1000-python-
questions-answers/).
advertisement
Do You Know Your Strange
Tech Tidbits?
Bill Gates has a favorite tree at home
that he has monitored electronically 24
0/10
hours to make sure it’s never dry
SCORE
TRUE FALSE
Sponsored
Best Careers
Sanfoundry is No. 1 choice for Deep Hands-ON Trainings in SAN, Linux & C, Kernel Programming. Our Founder has trained employees of almost all
Top Companies in India such as VMware, Citrix, Oracle, Motorola, Ericsson, Aricent, HP, Intuit, Microsoft, Cisco, SAP Labs, Siemens, Symantec,
https://www.sanfoundry.com/python-mcqs-files-1/ 11/15
Redhat,
3/27/2019 Chelsio, Cavium, ST-Micro, Samsung, LG-Soft, Wipro,
Files -TCS, HCL, IBM,
Python Questions Accenture,
and Answers HSBC, Mphasis, Tata-Elxsi, Tata VSNL, Mindtree,
- Sanfoundry
Best Trainings
https://www.sanfoundry.com/python-mcqs-files-1/ 12/15
3/27/2019 Files - Python Questions and Answers - Sanfoundry
India Internships
About Sanfoundry
https://www.sanfoundry.com/python-mcqs-files-1/ 14/15
About Us
3/27/2019 (https://www.sanfoundry.com/about/) Files - Python Questions and Answers - Sanfoundry
Copyright (https://www.sanfoundry.com/copyright/)
Terms (https://www.sanfoundry.com/tos-privacy-policy/)
Privacy Policy (https://www.sanfoundry.com/privacy-policy/)
Jobs (https://www.sanfoundry.com/jobs/)
Bangalore Training (https://www.sanfoundry.com/coursesfees/)
Online Training (https://www.sanfoundry.com/online-training-san-linux-kernel-device-drivers/)
Developers Track (https://www.sanfoundry.com/salary-50l/)
Mentoring Sessions (https://www.sanfoundry.com/professional-mentoring-coaching-career-guidance-cto/)
Contact Us (https://www.sanfoundry.com/contact/)
Sitemap (https://www.sanfoundry.com/sitemap_index.xml)
https://www.sanfoundry.com/python-mcqs-files-1/ 15/15