Event Examples in CS Thinking

Start with the recap, study the fully worked examples, then use the practice problems to check your understanding of Event.

This page combines explanation, solved examples, and follow-up practice so you can move from recognition to confident problem-solving in CS Thinking.

Concept Recap

A detectable action or occurrence in a program—such as a user click, key press, mouse movement, or timer expiry—that the program can respond to by executing a predefined event handler function.

Something happens (click, keypress, timer) and the program reacts.

Read the full concept explanation →

How to Use These Examples

  • Read the first worked example with the solution open so the structure is clear.
  • Try the practice problems before revealing each solution.
  • Use the related concepts and background knowledge badges if you feel stuck.

What to Focus On

Core idea: Event-driven programs wait for things to happen rather than following a fixed sequence.

Common stuck point: Events can occur in any order—the program must handle any sequence.

Sense of Study hint: When working with events, first identify which actions your program needs to respond to (clicks, keypresses, timers). Then write an event handler function for each event type. Finally, register (attach) the handler so the system knows to call it when the event occurs.

Worked Examples

Example 1

easy
In a GUI application, a user clicks a 'Submit' button. Describe what happens in terms of events and event handling.

Answer

The click creates an event, which triggers an event handler function that processes the form submission.

First step

1
Step 1: The click generates an event — a signal that something has happened (a 'click event' on the Submit button).

Full solution

  1. 2
    Step 2: The operating system detects the click and passes the event to the application.
  2. 3
    Step 3: The application has an event handler (a function) linked to the Submit button's click event. This handler runs — for example, it might validate the form and send the data.
Event-driven programming revolves around responding to events (user actions, timers, messages). Instead of running linearly, the program waits for events and runs the appropriate handler.

Example 2

medium
Compare event-driven programming with sequential programming. Give an example of each.

Example 3

medium
List three events involved in a typical photo-upload flow on a web page.

Example 4

medium
Identify the events in a 'sign in with password' form: typing in the email field, typing in the password field, clicking 'Sign in.'

Example 5

medium
A web app shows a tooltip on hover. List the events it must handle to show and hide the tooltip correctly.

Example 6

hard
Two events fire at almost the same time and modify the same variable. In a single-threaded event-driven system, can they corrupt each other's data?

Example 7

hard
A long-running handler runs for 22 seconds. During that time, the user clicks 33 more times. After the original handler finishes, how many more click handler invocations will run?

Example 8

hard
Throttling differs from debouncing. In one sentence each, explain both.

Example 9

hard
A custom event 'game-over' is dispatched. Two handlers are subscribed: one updates UI, one logs analytics. In what order do they run?

Example 10

challenge
A web app tracks click counts. Each click increments a counter, but the developer notices that fast double-clicks sometimes only register one increment. Identify two plausible causes.

Practice Problems

Try these problems on your own first, then open the solution to compare your method.

Example 1

medium
A simple alarm clock app has these features: set alarm time, snooze for 5 minutes, dismiss alarm. Identify the events and describe what each event handler should do.

Example 2

hard
In a game, multiple events can occur simultaneously: a key is pressed, a timer fires, and two objects collide. Explain how an event queue handles this, and why the order of handling might matter.

Example 3

easy
A user clicks a button and a function runs. What is the click called in event-driven programming?

Example 4

easy
The function that runs in response to an event is called the event ____.

Example 5

easy
Does an event-driven program follow a fixed top-to-bottom sequence, or react to occurrences?

Example 6

easy
Name two examples of events a program might respond to.

Example 7

easy
An event fires but nothing happens because no handler was attached. What was forgotten?

Example 8

easy
A timer expires and triggers code. Is the timer expiry an event?

Example 9

easy
If a click handler takes 5 seconds to run, what does the user experience?

Example 10

easy
True or false: in an event-driven UI, users may trigger events in any order.

Example 11

medium
A page has handlers for 'load', 'click', and 'keypress'. The user opens the page, then clicks. Which handlers ran, in order?

Example 12

medium
Two handlers are bound to the same button click. How many times does the button's click event fire on one click, and how many handler calls occur?

Example 13

medium
An event-driven simulation steps once per timer tick. After 4 ticks, the state update St+1=St+2S_{t+1}=S_t+2 starts at S0=1S_0=1. What is S4S_4?

Example 14

medium
A debounced handler ignores clicks within 300 ms of the last. Clicks arrive at 0, 100, 400 ms. How many times does the handler run?

Example 15

medium
Event A's handler triggers event B, whose handler triggers event A again with no stop condition. What bug results?

Example 16

medium
A keypress handler must run only for the Enter key. Inside it you check the key. What programming structure does this require?

Example 17

medium
Why might attaching an event handler inside a loop that runs each frame cause a bug?

Example 18

medium
A button has a click handler that increments a counter starting at 0. The user clicks it 5 times. What is the counter value?

Example 19

medium
A keypress handler logs the key. The user types 'cat' then presses Backspace. How many keypress events fired?

Example 20

challenge
An event queue holds events processed one at a time. Handler X (queued first) itself enqueues event Y. Given queue order is FIFO, when does Y's handler run relative to other already-queued events?

Example 21

challenge
Design: a thermostat must react to a temperature-sensor reading every second AND to a user dial change at any time. Which architecture fits and why?

Example 22

challenge
In an event loop, handler P runs and blocks for 2 s while a click event arrives at the 1 s mark. Exactly when is the click handler invoked, and what does this reveal about single-threaded loops?

Example 23

easy
Is moving the mouse over an element an event?

Example 24

easy
An event-driven program is waiting and nothing is happening. Is it 'broken' or 'idle'?

Example 25

easy
True or false: in event-driven code, the program decides ahead of time the exact order events will fire.

Example 26

medium
An app's UI freezes whenever a user clicks 'Export.' What likely went wrong with event handling?

Example 27

medium
An event fires while the previous event's handler is still running. Where does it usually go?

Example 28

medium
In a multiplayer game, two players' actions arrive nearly simultaneously over the network. What ordering does the event queue impose?

Example 29

medium
A 'double-click' is typically detected by: (a) the OS, (b) the application, or (c) either.

Example 30

medium
Does an event-driven program need to know in advance which events will fire?

Example 31

hard
An app needs to fire only after the user stops typing for 300300 ms. What technique uses input events plus a timer to achieve this?

Example 32

hard
What is an event source?

Example 33

hard
An event listener fires and calls `event.preventDefault()`. What happens?

Example 34

hard
A page has three buttons, each registered with the same click handler. Clicking any of them runs the handler. Inside the handler, what property of the event object tells you which button was clicked?

Background Knowledge

These ideas may be useful before you work through the harder examples.

function