Teacher Guide: Debugging Strategies
Student page: Debugging Strategies IB Syllabus: B2.1.4 Estimated periods: 1 (90 min) Prerequisites: Variables & Data Types, Exception Handling
Lesson Plans
Period 1: Trace Tables and Bug Hunting
| Phase | Time | Activity | Student Page Section |
|---|---|---|---|
| Warm-up | 10 min | Give students a 5-line code fragment — trace by hand before running | — |
| Teach | 20 min | Trace table methodology, 3 types of bugs (syntax, runtime, logic) | Key Concepts |
| Practice | 45 min | Core #1-3, then Extension #4-5 | Practice Exercises |
| Wrap-up | 15 min | Discuss: “Which type of bug is hardest to find? Why?” | — |
Teaching notes:
- Trace table habit: read code before executing it — essential for exam conditions
- Print debugging: have students deliberately introduce a bug, then use print statements to find it
- Bug taxonomy: syntax (won’t compile), runtime (crashes during execution), logic (wrong output, no error)
Differentiation
Supporting weaker students:
| Strategy | When to use | Example |
|---|---|---|
| Pre-filled trace table headers | Can’t set up trace table | Provide table with column headers: Line, a, b, c, Output |
| Bug hint cards | Stuck on “Find the bug” | Hint: “Look at the loop condition — what value should i reach?” |
| One bug at a time | Overwhelmed by Fix Three Bugs | Give bugs separately, fix one, then reveal the next |
| Paired tracing | Can’t trace independently | One student reads code aloud, partner fills in trace table |
Extending stronger students:
| Strategy | When to use | Example |
|---|---|---|
| Create a buggy program | Finishes exercises early | Write a program with 3 deliberate bugs, swap with a partner to find |
| Advanced trace | After Core #1 | Trace a program with nested if-else inside a loop |
| Debugger tool introduction | Extra time | Brief IDE debugger walkthrough (stepping, breakpoints, variable watch) |
| Bug journal | Ongoing | Keep a log of bugs encountered and how they were fixed — builds metacognition |
Answer Keys
Core 1: Trace practice
The code:
int a = 8;
int b = 3;
int c = a / b;
a = a % b;
b = c + a;
Trace table:
| Line | a | b | c |
|---|---|---|---|
| int a = 8 | 8 | — | — |
| int b = 3 | 8 | 3 | — |
| int c = a / b | 8 | 3 | 2 |
| a = a % b | 2 | 3 | 2 |
| b = c + a | 2 | 4 | 2 |
Final values: a=2, b=4, c=2
Key point: 8 / 3 = 2 (integer division truncates), 8 % 3 = 2 (remainder)
Core 2: Find the bug
The code:
for (int i = 1; i < 10; i++) {
System.out.println(i);
}
Bug: < 10 should be <= 10 — the loop stops before printing 10.
Fixed code:
for (int i = 1; i <= 10; i++) {
System.out.println(i);
}
Common student error: Some students suggest < 11 which also works but <= 10 is clearer and more intentional.
Core 3: Add debug prints
Given a program that calculates array average:
int[] arr = {10, 20, 30, 40, 50};
int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
System.out.println("DEBUG: i=" + i + " arr[i]=" + arr[i] + " sum=" + sum);
}
double avg = (double) sum / arr.length;
System.out.println("Average: " + avg);
Expected debug output:
DEBUG: i=0 arr[i]=10 sum=10
DEBUG: i=1 arr[i]=20 sum=30
DEBUG: i=2 arr[i]=30 sum=60
DEBUG: i=3 arr[i]=40 sum=100
DEBUG: i=4 arr[i]=50 sum=150
Average: 30.0
Key point: Debug prints show the running total, making it easy to spot if sum is accumulating correctly.
Marking notes: Accept any reasonable placement and format of debug prints. Key is that they track the variables changing each iteration.
Extension 4: Trace a nested loop
The code:
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(i * j + " ");
}
System.out.println();
}
Trace:
| i | j values | Output for this row |
|---|---|---|
| 1 | 1 | 1 |
| 2 | 1, 2 | 2 4 |
| 3 | 1, 2, 3 | 3 6 9 |
Complete output:
1
2 4
3 6 9
Key point: Inner loop runs i times (not a fixed count). When i=2, j goes from 1 to 2, so output is 2*1=2 and 2*2=4.
Common mistakes: Thinking j <= i means j goes from 1 to 3 for every row.
Extension 5: Fix three bugs
The original code:
int[] data = {10, 20, 30, 40, 50}
int sum = 0;
for (int i = 0; i <= data.length; i++) {
sum += data[i];
}
double avg = sum / data.length;
System.out.println("Average: " + avg);
Three bugs:
- Syntax bug: Missing semicolon after array declaration — add
; - Runtime bug:
i <= data.length— change toi < data.length(ArrayIndexOutOfBoundsException at index 5) - Logic bug:
sum / data.lengthis integer division — use(double) sum / data.length
Fixed code:
int[] data = {10, 20, 30, 40, 50};
int sum = 0;
for (int i = 0; i < data.length; i++) {
sum += data[i];
}
double avg = (double) sum / data.length;
System.out.println("Average: " + avg);
Expected output: Average: 30.0
Marking notes: All three bugs must be identified and fixed. Students should name the bug type for each (syntax, runtime, logic).
Integration Notes
| Item | In Class | Homework | Link |
|---|---|---|---|
| Worked Examples | Walk through trace table method | — | Worked Examples |
| Quick Code Check | Mid-lesson checkpoint | — | Quick Code Check |
| Core #1-3 | Individual practice | — | Practice Exercises |
| Extension #4-5 | Later in period | Complete for homework if needed | Practice Exercises |
| Trace Exercises | Wrap-up activity | — | Trace Exercise |
| GitHub Classroom | — | Due end of week | GitHub Classroom |
Additional Resources
- Blank trace table templates (printable)
- Bug taxonomy poster: Syntax / Runtime / Logic with examples
- Partner debugging activity: write buggy code, swap, find bugs