Reading and Writing Files Questions and Answers

Practice Mode
Showing 10 of 25 questions
Q11
Which method writes a list of strings to a file?
  • A write()
  • B writelines()
  • C write_list()
  • D putlines()
Answer: Option B
Explanation: The writelines() method takes a list of strings and writes them to the file.
Q12
What does "w" mode do if the file already exists?
  • A Appends to it
  • B Throws an error
  • C Truncates the file
  • D Opens in read-only
Answer: Option C
Explanation: "w" mode truncates (erases) the file if it already exists, then opens it for writing.
Q13
Which mode appends to a file without truncating it?
  • A "w"
  • B "r"
  • C "a"
  • D "x"
Answer: Option C
Explanation: "a" mode opens a file for appending and creates the file if it doesn't exist.
Q14
What is the default mode when opening a file with open()?
  • A "w"
  • B "r"
  • C "a"
  • D "x"
Answer: Option B
Explanation: If no mode is specified, open() defaults to "r" (read mode).
Q15
Which method moves the file pointer to a specific position?
  • A move()
  • B position()
  • C seek()
  • D goto()
Answer: Option C
Explanation: The seek() method moves the file pointer to the specified byte position.
Q16
Which method returns the current file pointer position?
  • A position()
  • B current()
  • C tell()
  • D where()
Answer: Option C
Explanation: The tell() method returns the current position of the file pointer.
Q17
What does seek(0) do?
  • A Moves to end of file
  • B Moves to beginning
  • C Moves 0 bytes forward
  • D Closes the file
Answer: Option B
Explanation: seek(0) moves the file pointer to the beginning of the file.
Q18
Which mode opens a file for binary reading?
  • A "r"
  • B "br"
  • C "rb"
  • D "binary"
Answer: Option C
Explanation: "rb" mode opens a file for reading in binary mode.
Q19
Which method flushes the internal buffer?
  • A clear()
  • B flush()
  • C clean()
  • D buffer()
Answer: Option B
Explanation: The flush() method clears the internal buffer and writes data to the file immediately.
Q20
What is the correct syntax for opening a file using with statement?
  • A with open(file) as f:
  • B with open file as f:
  • C open(file) with f:
  • D using open(file) as f:
Answer: Option A
Explanation: The with statement ensures proper resource management and automatic file closing.
Questions and Answers for Competitive Exams Various Entrance Test