Vidyalelo
Python · all questions

Files Handling in Python
practice.

Practice every MCQ with options. Use Show answers when you want the correct option and solution.

80

Questions

1/4

Page

Pick an option on a question to see the right answer and solution.

How do you open a file named data.txt in read mode in Python?

Select an option to see the answer and solution.

What is the purpose of the with statement in file handling?

Select an option to see the answer and solution.

How can you write data to a file in Python?

Select an option to see the answer and solution.

What does the read() method of a file object do?

Select an option to see the answer and solution.

How can you read a single line from a file in Python?

Select an option to see the answer and solution.

What is the purpose of the seek() method in file handling?

Select an option to see the answer and solution.

How can you read the entire contents of a file line by line in Python?

Select an option to see the answer and solution.

What is the output of the following code:
with open('data.txt', 'a') as file:
file.write('Appended line.')

with open('data.txt', 'r') as file:
content = file.read()
print(content)

Select an option to see the answer and solution.

How can you create a new file in Python if it doesn't exist?

Select an option to see the answer and solution.

To open a file c:\scores.txt for reading, we use . . . . . . . .

Select an option to see the answer and solution.

To open a file c:\scores.txt for appending data, we use . . . . . . . .

Select an option to see the answer and solution.

How can you close a file in Python?

Select an option to see the answer and solution.

What is the purpose of the tell() method in file handling?

Select an option to see the answer and solution.

To open a file c:\scores.txt for writing, we use . . . . . . . .

Select an option to see the answer and solution.

Which of the following statements are true?

Select an option to see the answer and solution.

What is the purpose of the write() method in file handling?

Select an option to see the answer and solution.

To read two characters from a file object infile, we use . . . . . . . .

Select an option to see the answer and solution.

What is the output of the following code:
file = open('data.txt', 'r')
content = file.read()
print(content)

Select an option to see the answer and solution.

What is the output of the following code:
with open('data.txt', 'w') as file:
file.write('Hello, world!')

Select an option to see the answer and solution.

What will be the output of the following code:
with open('data.txt', 'r') as file:
lines = file.readlines()
print(len(lines))

Select an option to see the answer and solution.