Python Programming - Reading and Writing Files Questions with Answers help learners understand how to handle data in text or binary formats. These Python File Handling Questions are important for TCS, Infosys, and Bank PO exams to master file input and output operations
Reading and Writing Files
Showing 10 of
25 questions
Questions per page:
5
10
20
50
11. Which method writes a list of strings to a file?
write()
writelines()
write_list()
putlines()
Answer: Option
B
Hints: The writelines() method takes a list of strings and writes them to the file.
Show
Answer
Hide
Answer
Report
12. What does "w" mode do if the file already exists?
Appends to it
Throws an error
Truncates the file
Opens in read-only
Answer: Option
C
Hints: "w" mode truncates (erases) the file if it already exists, then opens it for writing.
Show
Answer
Hide
Answer
Report
13. Which mode appends to a file without truncating it?
Answer: Option
C
Hints: "a" mode opens a file for appending and creates the file if it doesn't exist.
Show
Answer
Hide
Answer
Report
14. What is the default mode when opening a file with open()?
Answer: Option
B
Hints: If no mode is specified, open() defaults to "r" (read mode).
Show
Answer
Hide
Answer
Report
15. Which method moves the file pointer to a specific position?
move()
position()
seek()
goto()
Answer: Option
C
Hints: The seek() method moves the file pointer to the specified byte position.
Show
Answer
Hide
Answer
Report
16. Which method returns the current file pointer position?
position()
current()
tell()
where()
Answer: Option
C
Hints: The tell() method returns the current position of the file pointer.
Show
Answer
Hide
Answer
Report
17. What does seek(0) do?
Moves to end of file
Moves to beginning
Moves 0 bytes forward
Closes the file
Answer: Option
B
Hints: seek(0) moves the file pointer to the beginning of the file.
Show
Answer
Hide
Answer
Report
18. Which mode opens a file for binary reading?
Answer: Option
C
Hints: "rb" mode opens a file for reading in binary mode.
Show
Answer
Hide
Answer
Report
19. Which method flushes the internal buffer?
clear()
flush()
clean()
buffer()
Answer: Option
B
Hints: The flush() method clears the internal buffer and writes data to the file immediately.
Show
Answer
Hide
Answer
Report
20. What is the correct syntax for opening a file using with statement?
with open(file) as f:
with open file as f:
open(file) with f:
using open(file) as f:
Answer: Option
A
Hints: The with statement ensures proper resource management and automatic file closing.
Show
Answer
Hide
Answer
Report