QuizzesFiles & the with Statement — Concept Check
intermediate
python

Files & the with Statement — Concept Check

Questions on Files & the with Statement. open() + with reads and writes files with guaranteed closing; iterate file objects line by line for constant-memory processing of files of any size.

8 minutes 8 questions Pass at 70%
Time Limit
8 minutes - timer starts when you begin
Change Answers Freely
Revisit and change any answer before submitting
Full Review After Submit
Correct answers and explanations shown for every question
Auto-submit on timeout
Quiz submits automatically when time runs out

Questions in this quiz

8 questions on Files & the with Statement — Concept Check. Work through them here, then start the quiz to answer them and see your score.

  1. 1.You have a large log file that needs processing line by line. Which method should you use to ensure constant memory usage?

    • AUse read() to load the entire file into memory
    • BUse a for loop to iterate over the file object directly
    • CUse readlines() to get a list of all lines
    • DUse a while loop with readline() until EOF
  2. 2.A developer opens a file with `open('data.txt', 'w')` and writes data to it. What happens if the file already contains data?

    • AAn error is raised if the file is not empty
    • BThe existing data is overwritten
    • CThe new data is appended to the existing data
    • DThe file is opened in read-only mode
  3. 3.Why is it important to use the `with` statement when opening files in Python?

    • AIt prevents any exceptions from being raised
    • BIt ensures the file is closed even if an exception occurs
    • CIt automatically reads the entire file into memory
    • DIt is the only way to open files in Python
  4. 4.A script reads a configuration file using `read()`. What is a potential issue if the file is very large?

    • AThe script will read the file line by line
    • BThe script may run out of memory
    • CThe file will not be closed automatically
    • DThe file will be truncated
  5. 5.What happens if you open a file in 'a' mode and write to it?

    • AData overwrites the existing content
    • BData is appended to the end of the file
    • CThe file is opened in read-only mode
    • DAn error is raised if the file does not exist
  6. 6.A developer uses `with open('output.txt', 'w') as f:` to write data. What should they be cautious of?

    • AThe file will not be closed automatically
    • BThe file will be opened in binary mode
    • CThe file's existing contents will be erased
    • DThe file will be opened in append mode
  7. 7.Which mode should you use to read a text file while ensuring compatibility with different text encodings?

    • A'w' with encoding='utf-8'
    • B'r' with encoding='utf-8'
    • C'a' for appending
    • D'rb' for binary reading
  8. 8.Why might you choose to use `print(file=f)` instead of `f.write()` when writing to a file?

    • A`f.write()` is slower
    • B`print(file=f)` automatically adds a newline
    • C`print(file=f)` is the only way to write to files
    • D`f.write()` does not support text files