Embercode
✕
Object-Oriented ProgrammingQuick quiz

Cleanup and project

8 questions. Covers Inventory mini project in Python.

Questions 1 to 5 of 8

  1. 1. Put these steps in the order Python runs them when a with Res("A") as a, Res("B") as b: block ends normally. B was opened after A.

    Use the arrows to put the steps in order.

    1. 1.Python calls __exit__ on a
    2. 2.Execution moves on to the line after the with statement
    3. 3.Python calls __exit__ on b, the resource opened last
    4. 4.The statements inside the with block run
  2. 2. A class has a close method but no __enter__ or __exit__. What does Python report for this code? ``python class Plain: def close(self): print("closed") with Plain() as p: print("working") ``
  3. 3. What does this program print? ``python class Res: def __init__(self, name): self.name = name def __enter__(self): return self def __exit__(self, exc_type, exc, tb): print("close", self.name) try: with Res("A") as a, Res("B") as b: raise RuntimeError("oops") except RuntimeError as err: print("caught", err) ``
  4. 4. In with Flaky(): raise RuntimeError("body failed"), the __exit__ method of Flaky also raises RuntimeError("close failed"). What does the except block see? ``python class Flaky: def __enter__(self): return self def __exit__(self, exc_type, exc, tb): raise RuntimeError("close failed") try: with Flaky(): raise RuntimeError("body failed") except RuntimeError as err: print("caught:", err) print("context:", repr(err.__context__)) ``
  5. 5. Put these steps in the order that Store.sell_all uses for a whole order with several lines.

    Use the arrows to put the steps in order.

    1. 1.If every line passed, call remove_stock for each line
    2. 2.Check every line of the order against the stock, and raise if any line cannot be filled
    3. 3.The method returns, and every line has been sold

3 question(s) left on this page.