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.
An event handler is like setting a trap โ you define what should happen, then wait. When the event fires, your code runs automatically.
Showing a random 20 of 50 problems.
Example 1
easyAn event handler is a ____ that is called when an event fires.
Example 2
easyIn 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
mediumYou 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
mediumTrace: `count = 0; def onClick(): count += 1`. After registering once and clicking times, what is `count`?
Example 5
mediumYou register two click handlers on the same button: handler A logs 'A' and handler B logs 'B'. What does one click print?
Example 6
hardSame loop but with `var i` (function-scoped). Clicking any button after the loop finishes logs what?
Example 7
mediumTwo buttons share the same handler function. The handler prints a fixed string 'hi.' How does the handler tell which button was clicked?
Example 8
mediumWhat happens if an event handler throws an unhandled exception?
Example 9
hardExplain 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
easyWhich registers a handler correctly: `button.onClick(handler)` or `button.onClick(handler())`?
Example 11
easyTrue or false: registering an event handler runs its body immediately.
Example 12
easyA timer fires its handler every second. After seconds (first fire at ), how many times has the handler run?
Example 13
mediumTrace: `log=[]; def onKey(k): log.append(k)`. Keys 'a','b','a' are pressed (handler registered once). Final log?
Example 14
mediumFill in: registering the same handler function on the same event source twice usually results in the handler running ____ times per event.
Example 15
easyA button has one click handler. The user clicks it times. How many times does the handler run?
Example 16
easyTrue or false: registering a handler runs the handler's body immediately.
Example 17
mediumWhy does `btn.onClick(greet())` where `def greet(): return 'hi'` fail to react to clicks?
Example 18
challengeTwo 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
hardA handler runs for ms blocking the UI thread. During that time, the user clicks more times. How many handler invocations remain queued at the moment the first one finishes?
Example 20
mediumIn which order do handlers typically run when an event fires: handler-registration order or random?