Chapter 22 Quiz: Debugging and Troubleshooting with AI

Test your understanding of AI-assisted debugging concepts and techniques. Each question has one best answer unless otherwise indicated.


Question 1

What is the correct order of the four-phase AI debugging cycle?

  • A) Present to AI, Reproduce, Evaluate, Learn
  • B) Reproduce and Document, Present to AI, Evaluate and Apply, Learn and Document
  • C) Learn, Reproduce, Present to AI, Evaluate
  • D) Present to AI, Evaluate, Reproduce, Learn
Answer **B) Reproduce and Document, Present to AI, Evaluate and Apply, Learn and Document** The four-phase cycle begins with reproducing the bug and documenting what you observe, then presenting the structured information to AI, evaluating and applying the suggested fix, and finally learning from the experience and documenting the resolution.

Question 2

In a Python stack trace, where is the actual point of failure located?

  • A) The first line of the traceback
  • B) The middle of the traceback
  • C) The last line before the error message
  • D) The error type line at the very bottom
Answer **C) The last line before the error message** Python stack traces show the call chain from outermost (top) to innermost (bottom). The last frame before the error type line shows the exact line of code where the exception was raised. However, the root *cause* may be in an earlier frame.

Question 3

Which of the following is the MOST effective way to present an error to an AI assistant?

  • A) "I'm getting a KeyError. How do I fix it?"
  • B) "KeyError: 'username' on line 42 of models.py"
  • C) The full stack trace, relevant code, expected behavior, what you tried, and environment details
  • D) A screenshot of the error in your terminal
Answer **C) The full stack trace, relevant code, expected behavior, what you tried, and environment details** Providing comprehensive context enables the AI to diagnose the root cause rather than guessing. This follows the DESCRIBE framework taught in Section 22.9.

Question 4

What does the acronym DESCRIBE stand for in the debugging conversation template?

  • A) Debug, Error, Source, Context, Reproduce, Inspect, Behavior, Environment
  • B) Describe, Error, Source, Context, Reproduce, Investigated, Behavior, Environment
  • C) Document, Examine, Stack trace, Code, Review, Iterate, Build, Evaluate
  • D) Debug, Explore, Search, Check, Run, Identify, Break, Examine
Answer **B) Describe, Error, Source, Context, Reproduce, Investigated, Behavior, Environment** DESCRIBE stands for: Describe the expected behavior, Error message and stack trace, Source code, Context (environment, recent changes), Reproduce steps, Investigated already, Behavior observed, Environment details.

Question 5

When should you NOT use AI for debugging?

  • A) When you encounter an unfamiliar error message
  • B) When you have a clear syntax error with an obvious fix
  • C) When dealing with dependency conflicts
  • D) When analyzing profiling output
Answer **B) When you have a clear syntax error with an obvious fix** Simple syntax errors with clear messages (like a missing colon) are better fixed by the developer directly. This builds muscle memory and avoids wasting time for trivial issues. AI is best used for unfamiliar errors, complex conflicts, and analysis tasks.

Question 6

What is the "rubber duck effect" in the context of AI debugging?

  • A) AI assistants are as useful as rubber ducks for debugging
  • B) The act of structuring a bug report for AI often helps you solve the problem yourself
  • C) You should always debug alone before asking for help
  • D) AI provides the same quality of help as explaining to an inanimate object
Answer **B) The act of structuring a bug report for AI often helps you solve the problem yourself** The process of organizing your thoughts, gathering the error details, and formulating a clear description of the problem forces you to think through the issue systematically. Many developers report solving bugs while preparing to ask AI for help.

Question 7

When presenting a chained exception (with "During handling of the above exception, another exception occurred"), what should you include?

  • A) Only the final exception
  • B) Only the original exception
  • C) Both exception traces
  • D) Neither — just describe the problem in words
Answer **C) Both exception traces** Both traces provide important context. The original exception shows what went wrong first, and the chained exception shows how the error handling propagated. AI needs both to understand the full error path.

Question 8

What type of pattern does AI look for when analyzing log entries showing errors that only occur at specific times of day?

  • A) Correlation pattern
  • B) Temporal pattern
  • C) Sequence pattern
  • D) Frequency pattern
Answer **B) Temporal pattern** Temporal patterns involve time-based clustering of events. Errors at specific times might indicate scheduled jobs, traffic spikes, external service maintenance windows, or time-zone-related bugs.

Question 9

What is the recommended approach when AI suggests a fix for your bug?

  • A) Apply it immediately and move on
  • B) Understand why the fix works, test it in isolation, and verify it does not introduce new issues
  • C) Always reject the first suggestion and ask for alternatives
  • D) Apply the fix only if you would have thought of it yourself
Answer **B) Understand why the fix works, test it in isolation, and verify it does not introduce new issues** As discussed in Chapter 14, AI can confidently suggest incorrect solutions. Always understand the reasoning behind a fix before applying it, test it, and verify it does not create new problems.

Question 10

In the following cProfile output, what is the most likely performance bottleneck?

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.001    0.001   15.234   15.234 main.py:1(process)
    10000    0.045    0.000   14.876    0.001 api.py:10(fetch_record)
    10000   14.500    0.001   14.500    0.001 {method 'read' of 'http'}
    10000    0.234    0.000    0.234    0.000 utils.py:5(transform)
  • A) The process function in main.py
  • B) The transform function in utils.py
  • C) The HTTP read operations (10,000 individual network calls)
  • D) The fetch_record function itself
Answer **C) The HTTP read operations (10,000 individual network calls)** The HTTP read method consumes 14.5 of the 15.2 total seconds (95% of execution time). The issue is the N+1 pattern: 10,000 individual network calls should be batched. The `fetch_record` function is just a wrapper; the actual time is in the network I/O.

Question 11

What is the purpose of including both successful and failed operations when sharing logs with AI?

  • A) To make the log file longer so AI has more data
  • B) To allow AI to compare successful and failed operations and identify differences
  • C) AI requires a minimum amount of text to function properly
  • D) To demonstrate that the application sometimes works correctly
Answer **B) To allow AI to compare successful and failed operations and identify differences** Including both successful and failed operations gives AI a baseline for comparison. It can identify what is different about the failing cases (parameters, timing, data values) compared to the successes.

Question 12

What is the most common cause of ModuleNotFoundError when you have confirmed the package is installed?

  • A) The package has a bug
  • B) Your Python interpreter and pip are pointing to different environments
  • C) The package name changed
  • D) You need to restart your computer
Answer **B) Your Python interpreter and pip are pointing to different environments** The most common cause is a mismatch between the Python interpreter being used and the pip that installed the package. For example, `pip` might install into a global location while `python` runs from a virtual environment, or vice versa.

Question 13

When debugging in a Docker Compose environment, why does localhost often fail for inter-service communication?

  • A) Docker does not support localhost
  • B) Each container has its own localhost; services communicate using service names
  • C) Localhost is blocked by Docker's firewall
  • D) You must use IP addresses in Docker
Answer **B) Each container has its own localhost; services communicate using service names** In Docker Compose, each service runs in its own container with its own network namespace. The service name defined in docker-compose.yml acts as the hostname for inter-service communication (e.g., `db` instead of `localhost`).

Question 14

What does the memory_profiler output showing memory doubling at each processing step most likely indicate?

  • A) A memory leak
  • B) Data is being copied at each step instead of transformed in place
  • C) The profiler itself is consuming memory
  • D) Insufficient system RAM
Answer **B) Data is being copied at each step instead of transformed in place** When memory doubles at each step of a data pipeline, it typically means each step creates a new copy of the data while keeping the old copy alive. The fix is to either process data in place, use generators, or explicitly delete intermediate copies.

Question 15

Which debugging approach is most appropriate for an intermittent bug that occurs approximately 5% of the time?

  • A) Adding a breakpoint and waiting for the bug to occur
  • B) Using conditional breakpoints or logging to capture state only when the failure condition is met
  • C) Reading the code and trying to spot the bug visually
  • D) Rewriting the function from scratch
Answer **B) Using conditional breakpoints or logging to capture state only when the failure condition is met** For intermittent bugs, you need to capture diagnostic information when the bug occurs without stopping execution every time. Conditional breakpoints or targeted logging that activates only under specific conditions is the most effective approach.

Question 16

In the debugging learning loop, what should you do AFTER AI helps you fix a bug?

  • A) Close the conversation and move on
  • B) Ask AI to explain the root cause in depth, identify the pattern, and write a regression test
  • C) Report the bug to the AI vendor
  • D) Immediately start working on the next feature
Answer **B) Ask AI to explain the root cause in depth, identify the pattern, and write a regression test** The learning loop involves understanding the root cause, identifying the bug pattern for future recognition, learning the diagnostic approach, and building your mental model. Writing a regression test (as discussed in Chapter 21) prevents the bug from recurring.

Question 17

What is the "mutable default argument" bug in Python?

  • A) Default arguments cannot be changed after function definition
  • B) Mutable default arguments (like lists) are shared across all function calls
  • C) Default arguments are always None
  • D) Functions with default arguments cannot be called without arguments
Answer **B) Mutable default arguments (like lists) are shared across all function calls** When a mutable object (list, dict, set) is used as a default parameter value, the same object is reused across all calls. This means modifications in one call persist in the next call. The fix is to use `None` as the default and create a new object inside the function.

Question 18

When debugging dependency conflicts, which tool helps visualize the dependency tree?

  • A) pip install
  • B) pip freeze
  • C) pipdeptree
  • D) python -m venv
Answer **C) pipdeptree** `pipdeptree` displays the installed packages as a dependency tree, showing which packages depend on which, and at what version constraints. This visualization makes it easy to identify conflicting version requirements.

Question 19

What is the primary difference between the beginner and advanced stages of AI-assisted debugging?

  • A) Advanced users do not need AI
  • B) Beginners ask "what is wrong?" while advanced users ask "which of these approaches has better tradeoffs?"
  • C) Advanced users use more expensive AI models
  • D) Beginners debug faster than advanced users
Answer **B) Beginners ask "what is wrong?" while advanced users ask "which of these approaches has better tradeoffs?"** As debugging intuition grows, developers move from asking AI to identify the problem, to confirming their own diagnosis, to asking AI to compare alternative solutions. Advanced users use AI as a sounding board and knowledge reference rather than a primary diagnostician.

Question 20

Which information should you NOT typically send to an AI assistant when debugging?

  • A) Stack traces
  • B) Production database credentials or API keys present in configuration
  • C) Python version and OS information
  • D) Package version numbers
Answer **B) Production database credentials or API keys present in configuration** Never send sensitive information like passwords, API keys, tokens, or credentials to AI assistants. Sanitize configuration files before sharing. Replace real credentials with placeholder values like `` or `your-api-key-here`.

Question 21

What does an "absence pattern" in logs indicate?

  • A) There are too many log entries
  • B) A log line that should be present between two expected entries is missing, indicating a silent failure
  • C) The logging system is broken
  • D) The application has no errors
Answer **B) A log line that should be present between two expected entries is missing, indicating a silent failure** When an expected log entry is missing from between two known entries, it means the code path that would produce that log was not executed. This often indicates a silent failure — an exception was caught and swallowed, a conditional branch was taken unexpectedly, or the process crashed.

Question 22

When asking AI to add debugging instrumentation to a function, what should you request?

  • A) Add print statements at the beginning and end only
  • B) Add detailed logging at key decision points, showing variable values and execution paths
  • C) Add breakpoints everywhere
  • D) Rewrite the function to be simpler
Answer **B) Add detailed logging at key decision points, showing variable values and execution paths** The most useful debugging instrumentation logs values at decision points (if/else branches, loop iterations, function entry/exit) and includes relevant variable values. This provides a trace of the execution path without overwhelming you with data from every single line.

Question 23

What is the N+1 query problem and how does it appear in profiling output?

  • A) An error that occurs when you have N+1 database tables
  • B) A pattern where N individual database queries are executed instead of one batch query, visible as N identical slow calls in profiling
  • C) A problem that only occurs with exactly N+1 records
  • D) A security vulnerability related to SQL injection
Answer **B) A pattern where N individual database queries are executed instead of one batch query, visible as N identical slow calls in profiling** The N+1 problem occurs when code executes one query to get a list of N items, then executes N additional queries (one per item) to get related data. In profiling, it shows as a database function called N times with high cumulative time. The fix is to batch the queries.

Question 24

Which Python feature requires version 3.10 or higher and might cause SyntaxError in earlier versions?

  • A) f-strings
  • B) Type hints
  • C) match/case statements (structural pattern matching)
  • D) Walrus operator (:=)
Answer **C) match/case statements (structural pattern matching)** Structural pattern matching (`match`/`case`) was introduced in Python 3.10 (PEP 634). Code using this feature will produce a `SyntaxError` on Python 3.9 and earlier. F-strings require 3.6+, basic type hints require 3.5+, and the walrus operator requires 3.8+.

Question 25

According to this chapter, what is the ultimate goal of AI-assisted debugging?

  • A) To never encounter bugs again
  • B) To replace all manual debugging with AI
  • C) To build your own debugging intuition over time, using AI as a teacher rather than a crutch
  • D) To generate perfectly formatted bug reports
Answer **C) To build your own debugging intuition over time, using AI as a teacher rather than a crutch** The chapter emphasizes that the goal is not to become dependent on AI for every bug, but to use each AI-assisted debugging session as a learning opportunity. Over time, you internalize common patterns, develop faster diagnostic skills, and evolve from asking "what is wrong?" to asking AI to compare your proposed solutions.