A program reads a file inside a with open(...) as file: block, but an exception occurs before the block finishes. What should happen to the file?
6 File Input and Output Online Quiz Questions
Use this free practice quiz with 20 questions to review 6 File Input and Output, test your knowledge, and prepare for your next test or exam.
A logging program must preserve the existing contents of activity.log and add a new entry at the end. Which file mode should it use?
- A
r - B
w - C
a - D
x
When opening a CSV file for a csv.DictReader, which approach follows the recommended newline and text-handling conventions?
- A
Open it in binary mode and supply an encoding.
- B
Open it with
newline=""and an explicit encoding. - C
Open it with mode
xso rows cannot be overwritten. - D
Read the entire file with
read()before creating aDictReader.
Select all statements that correctly describe these pathlib.Path operations.
- A
Using
Path("data") / "input.txt"helps construct a path with platform-appropriate separators. - B
path.exists()checks whether the path exists. - C
path.suffixreturns the complete filename, including its extension. - D
path.namereturns the final component, such asinput.txt. - E
path.parentreturns the file's contents.
A program uses Path.read_text(encoding="utf-8") and can provide a useful message for specific failures. Select all exceptions it should reasonably handle separately based on the material.
- A
FileNotFoundError - B
PermissionError - C
UnicodeDecodeError - D
A bare
except:clause - E
Any exception without checking its cause
True or false: Python's write() method automatically adds a newline after every string it writes.
- A
True
- B
False
True or false: A relative file path is interpreted from the program's current working directory.
- A
True
- B
False
Which one-character file mode creates a new file and raises FileExistsError if a file with that name already exists?
What pathlib.Path method reads a text file and returns its decoded contents as a string while handling opening and closing automatically?
A program compares two ways to consume a text file: () returns the remaining contents as one string, while () returns a list of lines.
For JSON persistence, json.(settings, file) writes a Python value to a file, while json.(file) reconstructs a Python value from that file.
Design a robust approach for reading a potentially large text file of one numeric value per line and computing the average when some lines may be blank or invalid. Explain how the program should manage the file, process data incrementally, handle invalid input, and decide whether it can compute an average.
A program must read an image as bytes rather than as text. Which opening approach is correct?
- A
Open it with
randencoding="utf-8". - B
Open it with
wandencoding="utf-8". - C
Open it with
rbwithout supplying an encoding. - D
Open it with
aandnewline="".
True or false: Calling path.write_text("new contents", encoding="utf-8") replaces the existing contents of that path.
- A
True
- B
False
A Python program must save a new report while intentionally replacing any previous contents of the same file. Which file mode should it use?
- A
"a" - B
"w" - C
"r" - D
"x"
Which Python construct most reliably ensures that an opened file is closed after the file-processing block finishes, even if an exception occurs inside the block?
- A
A
trystatement with nofinallyblock - B
A call to
file.read() - C
A
withstatement - D
A call to
print()
Which file-object method should a program call when it needs to read exactly one line at a time?
A program must process a very large text file while minimizing memory use. Which approach is most appropriate?
- A
Call
file.read()before processing any line - B
Call
file.readlines()and then process the returned list - C
Convert the file to one large string with
str(file) - D
Iterate directly over the file object with
for line in file
What Python exception should a program catch when it tries to read a file that does not exist?
A program needs to read an image as raw bytes. Which call is consistent with the material's guidance?
- A
"r"withencoding="utf-8" - B
"rb"without an encoding - C
"r+"withencoding="utf-8" - D
"a"withencoding="utf-8"