Topic 1.4 - Identifying and Correcting Errors

2 min read

Topic 1.4: Identifying and Correcting Errors

Covers CRD-2.I (error types and debugging) and CRD-2.J (testing for correctness).

1. Types of Errors

Error Type Key Idea
Syntax Error (CRD-2.I.2) Language rules broken — program won’t run at all
Logic Error (CRD-2.I.1) Program runs but gives wrong results
Run-Time Error (CRD-2.I.3) Program crashes during execution
Overflow Error (CRD-2.I.4) Number outside the defined range of values
# SYNTAX ERROR — missing colons, program won't run
# def calculate_average(scores)
#     for score in scores
#         total += score

# CORRECTED:
def calculate_average(scores):
    total = 0
    for score in scores:
        total += score
    return total / len(scores)

print("Average:", calculate_average([90, 85, 78, 92, 88]))
# LOGIC ERROR — runs but gives wrong answer
def find_max(numbers):
    max_val = 0  # Bug: assumes positive numbers
    for num in numbers:
        if num > max_val:
            max_val = num
    return max_val

print(find_max([-3, -7, -2]))  # Returns 0 — WRONG! Should be -2

# Fix: initialize to first element
def find_max(numbers):
    max_val = numbers[0]
    for num in numbers:
        if num > max_val:
            max_val = num
    return max_val

print(find_max([-3, -7, -2]))  # -2 (correct)
# RUN-TIME ERROR — crashes mid-execution
try:
    result = 10 / 0
except ZeroDivisionError as e:
    print(f"Run-time error: {e}")

try:
    print([10, 20, 30][5])
except IndexError as e:
    print(f"Run-time error: {e}")
# OVERFLOW ERROR — number exceeds defined range
import numpy as np

int8_max = np.int8(127)
overflowed = np.int8(int8_max + np.int8(1))
print(f"127 + 1 with 8-bit integer: {overflowed}")  # -128 (wraps around!)

2. Debugging Strategies (CRD-2.I.5)

  1. Test cases — check expected outputs with different inputs
  2. Hand tracing — step through code on paper, tracking variables
  3. Visualizations — tools that show program state graphically
  4. Debuggers — IDE tools to step through code line by line
  5. Extra output statements — print variables to find where things go wrong
# HAND TRACING example: count even numbers
def count_evens(numbers):
    count = 1  # Bug: should be 0
    for num in numbers:
        if num % 2 == 0:
            count += 1
    return count

# Hand trace [3, 4, 7, 8, 2]: count starts at 1, ends at 4 — should be 3
print("Broken:", count_evens([3, 4, 7, 8, 2]))

# Fix: start count at 0
def count_evens(numbers):
    count = 0
    for num in numbers:
        if num % 2 == 0:
            count += 1
    return count

print("Fixed:", count_evens([3, 4, 7, 8, 2]))

3. Testing for Correctness (CRD-2.J)

  • CRD-2.J.1 — Use defined inputs to check expected outcomes; revise based on results.
  • CRD-2.J.2 — Test at extremes (min/max) and just beyond boundaries.
  • CRD-2.J.3Program requirements determine what inputs to test.
# Boundary testing: letter grades
def get_grade(score):
    if score >= 90: return "A"
    elif score >= 80: return "B"
    elif score >= 70: return "C"
    elif score >= 60: return "D"
    else: return "F"

for score, expected in [(100,"A"),(90,"A"),(89,"B"),(80,"B"),(70,"C"),(60,"D"),(59,"F"),(0,"F")]:
    result = "PASS" if get_grade(score) == expected else "FAIL"
    print(f"  {result} | {score} -> {get_grade(score)}")

AP Exam Review

Essential Knowledge Remember
CRD-2.I.1 Logic errors = wrong results
CRD-2.I.2 Syntax errors = won’t run
CRD-2.I.3 Run-time errors = crashes
CRD-2.I.4 Overflow errors = out of range
CRD-2.I.5 5 strategies: test cases, hand tracing, visualizations, debuggers, print statements
CRD-2.J Test with defined inputs at boundaries; need program requirements

Course Timeline