Practice Event Handler in CS Thinking

Use these practice problems to test your method after reviewing the concept explanation and worked examples.

Quick Recap

A function that is automatically called when a specific event occurs, such as a button click, key press, or timer tick. The handler is registered (attached) to an event source once, and then the system invokes it every time that event fires.

A smoke alarm is an event handler for smoke: installed once, it waits silently and reacts only when smoke arrives, as many times as that happens. You never tell it when to sound; the event does. Unlike an alarm on the ceiling, a handler can be attached twice by mistake, and then one event sets it off twice.

Showing a random 20 of 50 problems.

Example 1

easy
An event handler is a ____ that is called when an event fires.

Example 2

easy
In a web page, a button has this event handler: WHEN button.clicked: OUTPUT 'Button was clicked!'. Explain what an event handler is and when this code runs.

Example 3

medium
You attach a handler that prints `event.target.value` to an input. The user types `cat`. How many times does the handler fire and what does it print on the third fire?

Example 4

medium
Trace: `count = 0; def onClick(): count += 1`. After registering once and clicking 7 times, what is `count`?

Example 5

medium
You register two click handlers on the same button: handler A logs 'A' and handler B logs 'B'. What does one click print?

Example 6

hard
Same loop but with `var i` (function-scoped). Clicking any button after the loop finishes logs what?

Example 7

medium
Two buttons share the same handler function. The handler prints a fixed string 'hi.' How does the handler tell which button was clicked?

Example 8

medium
What happens if an event handler throws an unhandled exception?

Example 9

hard
Explain the problem of attaching an event handler inside a loop: FOR i = 0 TO 4: WHEN buttons[i].clicked: OUTPUT i. If you click button 3 after the loop finishes, what might be output and why?

Example 10

easy
Which registers a handler correctly: `button.onClick(handler)` or `button.onClick(handler())`?

Example 11

easy
True or false: registering an event handler runs its body immediately.

Example 12

easy
A timer fires its handler every second. After 10 seconds (first fire at t=1), how many times has the handler run?

Example 13

medium
Trace: `log=[]; def onKey(k): log.append(k)`. Keys 'a','b','a' are pressed (handler registered once). Final log?

Example 14

medium
Fill in: registering the same handler function on the same event source twice usually results in the handler running ____ times per event.

Example 15

easy
A button has one click handler. The user clicks it 4 times. How many times does the handler run?

Example 16

easy
True or false: registering a handler runs the handler's body immediately.

Example 17

medium
Why does `btn.onClick(greet())` where `def greet(): return 'hi'` fail to react to clicks?

Example 18

challenge
Two handlers are bound to a click: A increments a shared counter then B reads and prints it. Counter starts at 0. After two clicks, what does B print on the second click (A runs before B each time)?

Example 19

hard
A handler runs for 500 ms blocking the UI thread. During that time, the user clicks 4 more times. How many handler invocations remain queued at the moment the first one finishes?

Example 20

medium
In which order do handlers typically run when an event fires: handler-registration order or random?