Decorators & Generators in Python Quiz – 15 Questions with Answers
15 questions · 12 min · Medium
Test your knowledge of File I/O in Python, including opening files, reading and writing data, file modes, context managers, file methods, and handling text and binary files.
No sign-up needed · Last updated:
Score: 0 00:00
Create a free account to keep your scores and track your progress. It is optional.
15 questions · 12 min · Medium
20 questions · 15 min · Medium
15 questions · 12 min · Medium
15 questions · 12 min · Medium
15 questions · 12 min · Medium
15 questions · 12 min · Medium
B. open()
The built-in open() function is used to open a file and return a file object.
C. r
The r mode opens a file for reading. It is the default mode when no mode is explicitly specified.
B. w
The w mode opens a file for writing and truncates the existing file if it already exists. If the file does not exist, it is created.
C. a
The a mode opens a file for appending. New data is written at the end of the file.
A. read()
The read() method reads and returns the requested amount of data, or the remaining contents when no size is specified.
A. readline()
readline() reads one line from the file, including the newline character when present.
A. readlines()
readlines() reads the remaining lines and returns them as a list of strings.
A. write()
The write() method writes a string to the file and returns the number of characters written for text files.
A. It automatically closes the file when the block is exited
The with statement uses a context manager, ensuring that the file is properly closed when the block is exited, including in many error situations.
B. b
The b mode indicates binary I/O. It is commonly combined with another mode, such as rb or wb.
C. A FileExistsError is raised
The x mode is used for exclusive creation. It creates a new file and raises FileExistsError if the file already exists.
A. seek()
The seek() method changes the file's current position to a specified location.
B. tell()
The tell() method returns the current file position as an integer.
C. open('data.txt', 'r+')
The r+ mode opens an existing file for both reading and writing without truncating it.
A. The file object is closed and should no longer be used for I/O
close() closes the file object and releases associated resources. Attempting further I/O through the closed file generally raises an error.