Embercode
✕
Functions and ScopeQuick quiz

Callbacks and functions as values in Python

7 questions. Covers Callbacks and passing functions in Python.

Questions 1 to 5 of 7

  1. 1. Match each built-in function to what it does with the function you pass in.

    Choose the matching item for each row.

  2. 2. What does this program print? (one value per line) ``python def double_it(n): return n * 2 def add_one(n): return n + 1 def compose(f, g): return lambda x: g(f(x)) print(compose(double_it, add_one)(5)) print(compose(add_one, double_it)(5)) ``
  3. 3. What does this program print? ``python offset = 10 shifted = lambda a, b: a + b + offset offset = 20 print(shifted(1, 2)) ``
  4. 4. Match each situation to the type hint that fits it. The hints come from the typing module, and T and R stand for any types.

    Choose the matching item for each row.

  5. 5. What is the last line this program prints? ``python class Button: def __init__(self, name): self.name = name self.handlers = [] def on_click(self, handler): self.handlers.append(handler) def click(self): print(self.name, "clicked") for handler in self.handlers: handler() save_count = 0 def count_save(): global save_count save_count += 1 print("save count is", save_count) save = Button("Save") save.on_click(lambda: print("writing file")) save.on_click(count_save) save.click() save.click() print("total", save_count) ``

5 question(s) left on this page.