Free Practice Quiz Question List

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.

20 questions
01
Choose one
1 point

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?

  1. A

    It converts every file into JSON automatically.

  2. B

    It closes the file and releases its resources when the block ends.

  3. C

    It prevents all possible file-related exceptions.

  4. D

    It changes the file mode to append after reading.

02
Choose one
1 point

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?

  1. A

    r

  2. B

    w

  3. C

    a

  4. D

    x

03
Choose one
1 point

When opening a CSV file for a csv.DictReader, which approach follows the recommended newline and text-handling conventions?

  1. A

    Open it in binary mode and supply an encoding.

  2. B

    Open it with newline="" and an explicit encoding.

  3. C

    Open it with mode x so rows cannot be overwritten.

  4. D

    Read the entire file with read() before creating a DictReader.

04
Choose all
1 point

Select all statements that correctly describe these pathlib.Path operations.

  1. A

    Using Path("data") / "input.txt" helps construct a path with platform-appropriate separators.

  2. B

    path.exists() checks whether the path exists.

  3. C

    path.suffix returns the complete filename, including its extension.

  4. D

    path.name returns the final component, such as input.txt.

  5. E

    path.parent returns the file's contents.

05
Choose all
1 point

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.

  1. A

    FileNotFoundError

  2. B

    PermissionError

  3. C

    UnicodeDecodeError

  4. D

    A bare except: clause

  5. E

    Any exception without checking its cause

06
True or false
1 point

True or false: Python's write() method automatically adds a newline after every string it writes.

  1. A

    True

  2. B

    False

07
True or false
1 point

True or false: A relative file path is interpreted from the program's current working directory.

  1. A

    True

  2. B

    False

08
Written response
1 point

Which one-character file mode creates a new file and raises FileExistsError if a file with that name already exists?

09
Written response
1 point

What pathlib.Path method reads a text file and returns its decoded contents as a string while handling opening and closing automatically?

10
Fill in the blank
1 point

A program compares two ways to consume a text file: () returns the remaining contents as one string, while () returns a list of lines.

11
Fill in the blank
1 point

For JSON persistence, json.(settings, file) writes a Python value to a file, while json.(file) reconstructs a Python value from that file.

12
Open ended
1 point

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.

13
Choose one
1 point

A program must read an image as bytes rather than as text. Which opening approach is correct?

  1. A

    Open it with r and encoding="utf-8".

  2. B

    Open it with w and encoding="utf-8".

  3. C

    Open it with rb without supplying an encoding.

  4. D

    Open it with a and newline="".

14
True or false
1 point

True or false: Calling path.write_text("new contents", encoding="utf-8") replaces the existing contents of that path.

  1. A

    True

  2. B

    False

15
Choose one
1 point

A Python program must save a new report while intentionally replacing any previous contents of the same file. Which file mode should it use?

  1. A

    "a"

  2. B

    "w"

  3. C

    "r"

  4. D

    "x"

16
Choose one
1 point

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?

  1. A

    A try statement with no finally block

  2. B

    A call to file.read()

  3. C

    A with statement

  4. D

    A call to print()

17
Written response
1 point

Which file-object method should a program call when it needs to read exactly one line at a time?

18
Choose one
1 point

A program must process a very large text file while minimizing memory use. Which approach is most appropriate?

  1. A

    Call file.read() before processing any line

  2. B

    Call file.readlines() and then process the returned list

  3. C

    Convert the file to one large string with str(file)

  4. D

    Iterate directly over the file object with for line in file

19
Written response
1 point

What Python exception should a program catch when it tries to read a file that does not exist?

20
Choose one
1 point

A program needs to read an image as raw bytes. Which call is consistent with the material's guidance?

  1. A

    "r" with encoding="utf-8"

  2. B

    "rb" without an encoding

  3. C

    "r+" with encoding="utf-8"

  4. D

    "a" with encoding="utf-8"