# Claude Code Hooks — Quiz

## Question 1

What are Claude Code hooks?

A) AI-generated code snippets
B) Shell commands that run automatically in response to lifecycle events
C) Plugins that extend the Claude Code UI
D) Environment variables set by the user

<!-- ANSWER: B -->
<!-- EXPLANATION: Hooks are shell commands defined in hooks.json or .claude/settings.json. Claude Code executes them automatically when events like PostToolUse, UserPromptSubmit, or Stop occur. -->

## Question 2

<!-- VISUAL: fill-blank -->

Complete the hook configuration. To run a script after every tool use, you add it under the __________ key in `.claude/settings.json`:

```json
{
  "hooks": {
    "__________": [
      { "command": "./scripts/on-tool-use.sh", "timeout": 5000 }
    ]
  }
}
```

<!-- ANSWER: PostToolUse -->
<!-- EXPLANATION: PostToolUse is the lifecycle event that fires after any tool is used (Edit, Write, Bash, etc.). The configuration key must match the exact event name. -->

## Question 3

Which environment variable is available in a PostToolUse hook?

A) `USER_PROMPT`
B) `TOOL_NAME`
C) `STOP_REASON`
D) `TOKEN_USAGE`

<!-- ANSWER: B -->
<!-- EXPLANATION: PostToolUse hooks receive TOOL_NAME, TOOL_INPUT, and TOOL_OUTPUT. USER_PROMPT is for UserPromptSubmit; STOP_REASON and TOKEN_USAGE are for Stop. -->

## Question 4

<!-- VISUAL: drag-order -->

Put these lifecycle events in the order they occur during a single agent turn:

A) Stop
B) UserPromptSubmit
C) PostToolUse (may fire multiple times)

<!-- ANSWER: B,C,A -->
<!-- EXPLANATION: UserPromptSubmit fires when the user sends a message (start of turn). PostToolUse fires after each tool call during the turn. Stop fires when the agent finishes its response (end of turn). -->

## Question 5

Why should hook scripts "log, don't print"?

A) Printing is slower than file I/O
B) Stdout from hooks can be fed back to the agent and confuse it
C) Logs are required by Claude Code
D) Printing only works in certain shells

<!-- ANSWER: B -->
<!-- EXPLANATION: When a hook runs, its stdout may be captured and sent to the agent as context. Writing diagnostic output to stdout could pollute the agent's context. Writing to files keeps the agent's input clean. -->

## Question 6

What is a key reason to set a `timeout` on a hook?

A) Hooks are asynchronous by default
B) Hooks block the agent — a slow or stuck hook delays the whole session
C) Timeouts are required for security
D) Without a timeout, hooks run indefinitely in the background

<!-- ANSWER: B -->
<!-- EXPLANATION: Hooks run synchronously. While a hook is executing, the agent waits. A hung or very slow hook (e.g., a network call) would block the user's workflow. The timeout field prevents runaway hooks. -->
