File Handling in Python MCQs Exercise II
Ques 1
File Handling
How can you close a file in Python?
A close()
B exit()
C quit()
D All of the above
Ques 2
File Handling
Which method is used to write data to a file in Python?
A read()
B write()
C append()
D open()
Ques 3
File Handling
What does the following Python code snippet do?
file = open("filename.txt", "r")
file.seek(10)
content = file.read()
file.close()
print(content)
A Moves the file cursor to the 10th byte of the file and prints the content from that position
B Deletes the file named "filename.txt"
C Opens the file for writing
D Renames the file to "filename.txt"
Ques 4
File Handling
How can you iterate over each line in a file using a for loop in Python?
A for line in file:
B for line in file.readlines():
C Both a and b
D None of the above
Ques 5
File Handling
What will be the output of the following Python code snippet?
file = open("filename.txt", "r")
position = file.tell()
file.close()
print(position)
A Prints the contents of the file
B Prints the current position of the file cursor
C Error: File is not open for reading
D None of the above