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
1. Which function is used to open a file in Python?
file_open()
open()
read_file()
file()
Answer: Option
B
Hints: The open() function is used to open files in Python and returns a file object.
Show
Answer
Hide
Answer
Report
2. Which mode opens a file for reading only?
Answer: Option
B
Hints: "r" mode opens a file for reading only and is the default mode.
Show
Answer
Hide
Answer
Report
3. Which mode creates a new file for writing and returns error if file exists?
Answer: Option
C
Hints: "x" mode creates a new file and returns an error if the file already exists.
Show
Answer
Hide
Answer
Report
4. Which method reads the entire content of a file as a string?
readline()
readall()
read()
get()
Answer: Option
C
Hints: The read() method reads the entire content of a file and returns it as a string.
Show
Answer
Hide
Answer
Report
5. Which method reads one line from a file?
read()
readline()
nextline()
getline()
Answer: Option
B
Hints: The readline() method reads a single line from the file and returns it as a string.
Show
Answer
Hide
Answer
Report
6. Which method returns all lines in a file as a list?
read()
readlines()
getlines()
lines()
Answer: Option
B
Hints: The readlines() method reads all lines in a file and returns them as a list of strings.
Show
Answer
Hide
Answer
Report
7. Which mode opens a file for both reading and writing?
Answer: Option
C
Hints: "r+" mode opens a file for both reading and writing.
Show
Answer
Hide
Answer
Report
8. What is the correct way to close a file in Python?
file.close()
close(file)
file->close()
close.file()
Answer: Option
A
Hints: The close() method is used to close an opened file and free up system resources.
Show
Answer
Hide
Answer
Report
9. Which statement automatically closes a file after usage?
Answer: Option
C
Hints: The with statement automatically closes the file when the block inside it is exited.
Show
Answer
Hide
Answer
Report
10. Which method writes a string to a file?
writeline()
put()
write()
output()
Answer: Option
C
Hints: The write() method writes a string to an opened file.
Show
Answer
Hide
Answer
Report