Free Practice Quiz Question List

7 File Input and Output Online Quiz Questions

Use this free practice quiz with 20 questions to review 7 File Input and Output, test your knowledge, and prepare for your next test or exam.

20 questions
01
True or false
1 point

True or false: A file opened inside a Python with statement is closed automatically when execution leaves the block, even if an exception occurs inside it.

  1. A

    True

  2. B

    False

02
Choose one
1 point

Which mode should a Python program use to add a new line to the end of an existing log file while preserving its current contents?

  1. A

    "w"

  2. B

    "a"

  3. C

    "r"

  4. D

    "x"

03
Written response
1 point

Which encoding should you explicitly specify when opening an ordinary text file to make text processing predictable across platforms?

04
Fill in the blank
1 point

Complete the statement: To write a fresh version of a text file and replace any existing contents, pass mode to open().

05
Fill in the blank
1 point

Complete the statement: A relative path in Python is interpreted from the program's , not necessarily from the directory containing the source file.

06
Choose one
1 point

Which expression uses pathlib to construct a path for input.txt inside the data directory in a platform-aware way?

  1. A

    path.join("data", "input.txt")

  2. B

    Path.join("data", "input.txt")

  3. C

    Path("data") / "input.txt"

  4. D

    "data" + "/" + "input.txt"

07
Choose all
1 point

Select all correct statements about reading a text file in Python.

  1. A

    file.read() returns the remaining contents as one string.

  2. B

    file.readline() returns all remaining lines as a list.

  3. C

    A file object can be iterated directly to process it line by line.

  4. D

    file.readlines() returns one combined string containing all lines.

08
Written response
1 point

A program executes count = file.write("Hi"). What value is stored in count?

09
Choose one
1 point

Which statement correctly applies the pathlib text-file methods described in the material?

  1. A

    Path.read_text() always returns a list of lines.

  2. B

    Path.read_text() can read and close a simple text file internally.

  3. C

    Path.open() cannot accept an encoding.

  4. D

    Path.write_text() is used only for binary files.

10
Open ended
1 point

Write a short Python solution that reads integer scores from scores.txt, processes the file line by line without reading the entire file at once, removes each line's surrounding whitespace before conversion, and safely closes the file. Explain why the structure is appropriate.

11
True or false
1 point

True or false: Python's text mode line-ending translation is appropriate for ordinary text files but should not be used for binary formats such as images.

  1. A

    True

  2. B

    False

12
Choose one
1 point

A program must create first-run.txt only if it does not already exist; overwriting an existing file should raise an error. Which mode should it use?

  1. A

    "x"

  2. B

    "w"

  3. C

    "a"

  4. D

    "r+"

13
Choose all
1 point

A program should read message.txt only when the path exists and refers to a file rather than a directory. Which two pathlib methods directly test those conditions?

  1. A

    exists()

  2. B

    is_file()

  3. C

    is_directory()

  4. D

    read_text()

14
True or false
1 point

True or false: Omitting the encoding argument when opening a text file guarantees that the same character decoding behavior will occur on every computer.

  1. A

    True

  2. B

    False

15
Written response
1 point

What directory does Python use as the starting point when interpreting a relative path?

16
Written response
1 point

What exception may Python raise when text is read using an encoding that cannot decode the file's bytes?

17
Choose one
1 point

A program must inspect an existing configuration file and then update selected contents in that same file without automatically erasing the rest. Which mode should be used with open()?

  1. A

    Use mode "w".

  2. B

    Use mode "r+".

  3. C

    Use mode "a".

  4. D

    Use mode "x".

18
Choose one
1 point

Consider this code: quantity = 12; with open("summary.txt", "w", encoding="utf-8") as file: file.write("Quantity: " + quantity). What change correctly allows the quantity to be included in the written text?

  1. A

    Call file.read(quantity) instead.

  2. B

    Open the file in mode "rb".

  3. C

    Convert quantity with str(quantity) before writing.

  4. D

    Use mode "x" so Python converts the value.

19
Choose one
1 point

A program reads settings with Path("settings.txt").read_text(encoding="utf-8"). If settings.txt does not exist, which specific exception should the program catch to provide default settings?

  1. A

    FileNotFoundError

  2. B

    PermissionError

  3. C

    UnicodeDecodeError

  4. D

    IsADirectoryError

20
Choose one
1 point

A program must write the string "café" to a binary file opened with mode "wb". Which approach is appropriate?

  1. A

    Pass the text directly because all file modes accept str.

  2. B

    Use mode "r" so the text is converted automatically.

  3. C

    Use mode "w" and omit the encoding argument.

  4. D

    Encode the text to bytes before passing it to write().