Lab 9: Knowledge Check

These questions check your knowledge of concepts from this lab. Use the button at the bottom to show your answers once you've attempted them.

  1. What is the primary purpose of using a with statement when opening a file?
    1. To assign the file object to a variable, you must use a with statement.
    2. You must use with to open a file in writing mode.
    3. If you use with, the file will be closed automatically.
    4. Using a with statement allows you to iterate over the lines in a file.
  2. When you open a file in 'write' mode and write some text into it, what happens? (pick one or more)
    1. If the file already exists, its contents are erased.
    2. The text is written into the file after any existing text that's already there.
    3. If the file doesn't already exist, it is created.
    4. The text is written over what was there, and the old text is returned by the call to .write.
  3. If you want to understand why an optimism test failed, you could: (pick one or more)
    1. Add print and/or trace calls to your code.
    2. Use the debugger to step through the code for the failing test.
    3. Add a series of simpler test cases to find the simplest one which doesn't work.
    4. Turn off your computer and go for a walk, take a nap, or chat with a friend about something unrelated to computer science.

Table of Contents