Predicting log output from setup code and posted events

The pseudocode below runs on a single-threaded event loop with one FIFO queue. on(name, f) registers handler f for events called name. post(name) only adds an event to the back of the queue and returns immediately. A handler is looked up when its event is dispatched, not when the event is posted. Setup runs to completion before run_event_loop() starts dispatching.


Plain text
function h_tick():
    print("tick handled")

function h_click():
    post("save")
    print("click handled")

function h_save():
    print("saved")

print("start")
on("click", h_click)
post("tick")
on("tick", h_tick)
on("save", h_save)
print("setup done")
run_event_loop()


While setup is running, just after the post("tick") line executes, the user clicks the window, so a click event joins the queue behind the tick. No other events occur. Arrange the printed lines in the order they appear.

  1. 1.

    start

  2. 2.

    setup done

  3. 3.

    tick handled

  4. 4.

    saved

  5. 5.

    click handled

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Event-Driven Programming Questions: