Interfaces, coupling and what a module promises · seed 1 · A4, ink-friendly. The answer key prints on its own page for grown-ups.

Promises your modules keep

Computing · Software Engineering · ages 19-20
Name ______________________   Date ____________
  1. A module promises silent imports. Its top-level demo prints a banner. Keep its functions usable on import. Which change meets the promise?

    • Put the demo under the main guard
    • Keep the demo outside every guard
    • Put function definitions under the main guard
  2. A module documents its public functions and their behavior. What may callers treat as its contract?

    • Its private storage layout
    • Its documented public behavior
    • Its current private helper function names
  3. A function's documented error behavior is part of its interface, even when its public name stays unchanged.

    Circle one:   True   False

  4. A module removes its private _items attribute but preserves public total() and its documented behavior. Which caller's access now fails?

    • A caller reading module._items
    • A caller using its own list
    • A caller calling public module.total()
  5. Which import and use pair keeps the module name visible when creating a Circle instance?

    • Copy Circle locally, then call Circle()
    • Use from shapes import Circle, then call Circle()
    • Use import shapes and then call shapes.Circle()
  6. A change renames private _draft_total but preserves public total() and its behavior. Design A calls total(); design B calls _draft_total. Which caller needs no edit for this rename?

    • Design B, because its helper is private
    • Design A, because its public call stays compatible
    • Both designs, because renaming a helper leaves every caller name usable
  7. A caller needs an approved feature to reset a module's tally. There is no public reset operation. Which review response preserves the module boundary?

    • Request a documented reset operation
    • Write to private _sent_count directly
    • Copy private storage code into each caller
  8. A report reader accepts an existing report ID. Which statement promises a result without choosing its private implementation?

    • It scans report rows with a private loop counter.
    • It searches a private directory for the report.
    • It returns the saved report text for that ID.
LightMySky · lightmysky.comW1-mt_EUZyD26WFk-s1

Answer key

For grown-ups. Fold this page away before handing over the rest.

Promises your modules keep W1-mt_EUZyD26WFk-s1

  1. Put the demo under the main guard · Put the demo under `if __name__ == "__main__":`. Ordinary imports skip that block. Leave reusable function definitions outside it.
  2. Its documented public behavior · The contract covers documented public behavior. Private storage layouts and helper names describe implementation details.
  3. True · Documented errors tell callers what to expect. Keeping a function's name does not preserve a promise about errors if that behavior changes.
  4. A caller reading module._items · `module._items` requires the removed attribute. The public total operation stays compatible, and the caller's separate list does not access that attribute.
  5. Use import shapes and then call shapes.Circle() · `shapes.Circle()` includes the module name at the use site. `Circle()` does not, although both import styles can use the same public class.
  6. Design A, because its public call stays compatible · Design A uses the preserved public operation. Design B must update its call to the renamed private helper. This comparison concerns caller edits for this rename.
  7. Request a documented reset operation · A documented operation lets the module maintain its own state. Direct private writes and copied storage code make callers depend on its representation.
  8. It returns the saved report text for that ID. · Returning saved report text states an observable result. The other statements prescribe private algorithms or storage choices.
Worksheet · LightMySky