Reading and Writing Files Questions and Answers
Practice ModeShowing 10 of 25 questions
Q1
Which function is used to open a file in Python?
Answer: Option B
Explanation: The open() function is used to open files in Python and returns a file object.
Q2
Which mode opens a file for reading only?
Answer: Option B
Explanation: "r" mode opens a file for reading only and is the default mode.
Q3
Which mode creates a new file for writing and returns error if file exists?
Answer: Option C
Explanation: "x" mode creates a new file and returns an error if the file already exists.
Q4
Which method reads the entire content of a file as a string?
Answer: Option C
Explanation: The read() method reads the entire content of a file and returns it as a string.
Q5
Which method reads one line from a file?
Answer: Option B
Explanation: The readline() method reads a single line from the file and returns it as a string.
Q6
Which method returns all lines in a file as a list?
Answer: Option B
Explanation: The readlines() method reads all lines in a file and returns them as a list of strings.
Q7
Which mode opens a file for both reading and writing?
Answer: Option C
Explanation: "r+" mode opens a file for both reading and writing.
Q8
What is the correct way to close a file in Python?
Answer: Option A
Explanation: The close() method is used to close an opened file and free up system resources.
Q9
Which statement automatically closes a file after usage?
Answer: Option C
Explanation: The with statement automatically closes the file when the block inside it is exited.
Q10
Which method writes a string to a file?
Answer: Option C
Explanation: The write() method writes a string to an opened file.