Python MCQs on Files: Set 1

Python MCQs on Files

In this section, we are going to introduce you to the most frequently asked Python MCQs on Files. These Python MCQs on Files will be going to help you in Interviews and Exams.

 

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”)

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

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

Check Answer

Answer: a
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()

Check Answer

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

Check Answer

Answer: a
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()

Check Answer

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

Check Answer

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

Check Answer

Answer: b
Every line is stored in a list and returned.

Page  1  2  3  4  5

Python Interview Questions (MCQs)

We have divided these Python Questions and Answers into various parts based on the topics. Open the Topic of your choice and Practice these MCQs.

Python MCQs on Variables and Operators Python MCQs on Precedence and Associativity
Python MCQs on Data Type Python MCQs on Boolean
Python MCQs on Bitwise Operators Python MCQs on Formatting and Advance Formatting
Python MCQs on Decorators Python MCQs on While and For Loops
Python MCQs on List Python MCQs on List Comprehension
Python MCQs on String Python MCQs on Tuple
Python MCQs on SET Python MCQs on Dictionary
Python MCQs on Functions Python MCQs on Argument Parsing
Python MCQs on Global and Local Variables Python MCQs on Recursion
Python MCQs on Mapping Functions Python MCQs on Modules
Python MCQs on Regular Expressions Python MCQs on Files
Python MCQs on Overloading Python MCQs on Classes and Objects
Python MCQs on Inheritance Python MCQs on Polymorphism and Encapsulation
Python MCQs on Exception Handling

 

Leave a Comment