# Debugging and validation

Physics I · Computer Science · https://tryals.app/learn/physics-i/debugging-and-validation

## Debugging Categories

Writing code that runs is not enough; it must produce **correct** results. The hardest bugs are **silent**: the code runs without errors but produces wrong answers.

| Bug Type | Example | Fix |
| --- | --- | --- |
| **Syntax** | Missing colon | Read error |
| **Runtime** | Division by zero | Add guard `if x != 0` |
| **Logic** | Off-by-one bounds | Trace small example |
| **Numerical** | Float drift | Smaller step $h$ |

**Common pitfall:** Fixing the crash site often leaves the root cause untouched. Trace the bad value back to where it was **born**; the crash is usually just the victim.

## Strategies and Validation

Use code structure to catch mistakes early. Print intermediate values (`print(f"x={x}")`), use **assertions** to catch invariants (`assert energy > 0`), and test known inputs.

Validate your work systematically:

| Level | Method | Purpose |
| --- | --- | --- |
| **1** | Unit tests | Check functions |
| **2** | Conservation | Check energy/momentum |
| **3** | Convergence | Halve $h$ to check error |
| **4** | Comparison | Match analytics |

Conservation checks are your best defense against silent bugs.

## Practice questions

6 of this lesson's 15 practice questions, with answers. The full set is in the app.

### 1. Your projectile simulation says a thrown ball climbs forever. Which debugging move gives the **most information first**?

A. Rerun the program, it may pass this time
B. Rewrite the whole simulation from scratch
C. Print the velocity each timestep and watch the sign
D. Reduce the timestep 1000× and hope precision fixes it

**Answer:** C. Print the velocity each timestep and watch the sign

**Why:** Making the invisible visible beats guessing: the velocity trace immediately shows whether gravity’s sign, the update order, or the integration is at fault. Rerunning changes nothing, rewriting risks new bugs, and smaller timesteps only mask logic errors.

Page: https://tryals.app/practice/physics-i/debugging-and-validation/your-projectile-simulation-says-a-thrown-ball-climbs-forever-which

### 2. A program runs without errors and produces plausible numbers that are wrong. Why is this harder to catch than a crash?

A. Because plausible numbers indicate the error is small and unimportant
B. Because debuggers cannot inspect a program that is not crashing
C. Because nothing signals the failure; a crash announces itself
D. Because such errors are always in the compiler rather than the code

**Answer:** C. Because nothing signals the failure; a crash announces itself

**Why:** Silence is the danger. Validating against analytical solutions, limiting cases and conserved quantities is how scientific code is made trustworthy, the absence of a crash proves nothing.

Page: https://tryals.app/practice/physics-i/debugging-and-validation/a-program-runs-without-errors-and-produces-plausible-numbers-that-are

### 3. Order the steps of a systematic debugging process.

**Answer:**

1. Reproduce the bug with a specific input
2. Narrow down the faulty code section
3. Form a hypothesis about the cause
4. Fix the code and run tests to confirm

**Why:** Disciplined debugging avoids random changes that may introduce new bugs.

Page: https://tryals.app/practice/physics-i/debugging-and-validation/order-the-steps-of-a-systematic-debugging-process

### 4. Order these steps into the scientific debugging method.

**Answer:**

1. Reproduce the failure reliably
2. Form a hypothesis about the cause
3. Design a minimal test that would confirm or refute it
4. Run the test and observe
5. Fix the cause, and keep the test to guard against regressions

**Why:** Debugging **is** science: reproduce, hypothesize, experiment, conclude. The final twist is uniquely software’s, the confirming test becomes a permanent tripwire so the same bug can never sneak back.

Page: https://tryals.app/practice/physics-i/debugging-and-validation/order-these-steps-into-the-scientific-debugging-method

### 5. Complete the description of debugging.

**Answer:** The first step in debugging is to **reproduce** the problem reliably. Then **isolate** the faulty section of code. After applying a **fix**, run **test**s to confirm correctness.

**Why:** Systematic debugging follows: reproduce $\to$ isolate $\to$ fix $\to$ test.

Page: https://tryals.app/practice/physics-i/debugging-and-validation/complete-the-description-of-debugging

### 6. Sort these errors by category.

**Answer:**

- Syntax Error: Forgetting a closing parenthesis, Misspelling a keyword
- Logic Error: Using < instead of <=, Infinite loop due to wrong update
- Runtime Error: Accessing index beyond list length

**Why:** Identifying the error category guides the debugging strategy.

Page: https://tryals.app/practice/physics-i/debugging-and-validation/sort-these-errors-by-category
