The big idea: A program that runs but gives the wrong answer has a logic error, and the computer cannot tell you where it is.
Debugging is narrowing down where the values first stop being what you expected.
Three kinds of error: Syntax — the code will not run at all; the editor usually points straight at it.
Run-time — it stops partway, with a message naming the problem.
Logic — it runs perfectly and produces the wrong answer. This is the one that needs debugging.
Free preview
This is the free notes preview
You're reading the free notes. Aimnova Pro unlocks the full study experience — and you can try it with your first topic free to keep:
- FlashcardsLock in vocabulary and key terms with spaced repetition.
- Practice questionsAnswer exam-style questions and get instant AI marking.
- Mock exams & past-paper vaultSit full mocks and see exactly how examiners award marks.
- Personalised study planA daily plan built around your exam date and weak areas.
Trace table — on paper
Work through the code by hand, writing every variable after every step.
Slow, and needs no tools at all — which is why it is what the exam asks for. It also finds errors in logic that no amount of running will reveal.
Print statements
Add an output line that shows a value at a chosen point, labelled with what it is.
Crude but effective, and works anywhere. Label every one, or you will not know which line produced which number.
Breakpoints
Mark a line and the program pauses there, letting you inspect every variable at that moment.
Better than prints when you do not yet know which value is wrong, because you can look at all of them at once.
Step-by-step execution
From a breakpoint, run one line at a time and watch the values change.
This is what finds a loop that runs one time too many, or a branch taken when it should not have been.
The trace table is the exam technique: Breakpoints need software. A trace table needs a pen.
So when a question gives you code and asks what is wrong, the intended method is nearly always to trace it by hand.
Get feedback like a real examiner
Submit your answers and get instant feedback — what you did well, what's missing, and exactly what to write to score full marks.
1 — Say what you expected
Write down the answer the program should give for one specific input.
Without that, you cannot tell which value first went wrong — only that something did.
2 — Find the first wrong value
Check the variables at a point roughly halfway through. If they are right, the fault is later; if wrong, it is earlier.
Halving the search each time finds it in a few checks, however long the program.
3 — Look at what changes it
Once you know which value first goes wrong, look only at the lines that set it.
That is usually two or three lines out of a hundred.
4 — Change one thing, then test
Fix one thing and run the same test again.
Changing several at once means that if it works you do not know why, and if it still fails you have a new set of suspects.
Test the boundary first: Most logic errors show up at the edges: an empty list, a single item, the first and last positions, values that are exactly equal.
If a program is wrong, try the boundary before anything else.
How this is tested — you must trace the code and say WHICH line is wrong, not just that it is. It comes up two ways:
Paper 2 — working with code
- Identify the error in a program and correct it, 3-5 marks
- Complete a trace table for faulty code
- Name a debugging technique and say what it finds
Paper 2 — the algorithmic-thinking question
- Reason about where a described program goes wrong
- Explain how you would narrow it down
The classic trap: Rewriting the whole program. The question asks which line is wrong and what it should be — a full rewrite usually loses marks even when the new version works.
This should print the largest of a list but prints 0 for a list of negative numbers. Find the error.
largest = 0
for value in numbers:
if value > largest:
largest = value
print(largest)
Model answer plan
See the answer broken into clear points, with guidance on how each point earns marks.
This should print the largest of a list but prints 0 for a list of negative numbers. Find the error.
int largest = 0;
for (int value : numbers) {
if (value > largest) {
largest = value;
}
}
System.out.println(largest);
Model answer plan
See the answer broken into clear points, with guidance on how each point earns marks.