Error Identification (College Board AP® Computer Science Principles): Revision Note
Error types
What types of errors can occur in a program?
An error is a mistake in a program that causes it to behave incorrectly or fail to run
Debugging is the process of finding and fixing errors in a program
Understanding the different types of errors helps developers identify problems more efficiently during development
Error type | Description | Example |
|---|---|---|
Syntax error | The code violates the language rules and cannot be executed | A missing parenthesis or misspelled keyword that prevents the program from running |
Logic error | The program runs without crashing but produces incorrect behaviour or wrong output | A program that adds two numbers instead of multiplying them |
Runtime error | The program crashes during execution because of an operation the computer cannot complete | Dividing a number by zero or accessing a list element that does not exist |
Overflow error | A value exceeds the range of numbers that can be stored in the allocated memory | A calculation that produces a result too large for the variable type to hold |
Key differences
A syntax error is caught before the program runs; the computer identifies the rule violation and refuses to execute the code
A logic error is the hardest to find because the program runs without any error messages but produces the wrong result
A runtime error only appears during execution, often because the program encounters data or conditions the developer did not anticipate
An overflow error is a specific type of runtime error caused by exceeding the limits of numerical storage
Error detection methods
How are errors detected in a program?
Error detection is the process of identifying where and why a program is not working as expected
Developers use a combination of methods to detect errors depending on the type of problem and the complexity of the program
Test cases
A test case is a specific set of inputs paired with the expected output for a program
Running test cases reveals whether the program produces the correct result for each input
Effective test cases cover normal inputs, boundary values, and unexpected data
If the actual output does not match the expected output, an error exists in the program
Hand tracing
Hand tracing involves manually stepping through the code line by line, tracking the value of each variable at every step
It is particularly useful for finding logic errors, where the program runs but produces the wrong result
Hand tracing does not require any software tools and can be done on paper
Visualizations
Visualizations such as flowcharts or variable state diagrams help developers see where a program's behaviour is different from expectations
They provide a visual overview of how data flows through a program, making complex logic easier to follow
Adding extra output statements
Developers can temporarily insert
DISPLAY()statements into the code to show the value of a variable at a specific point during executionThis helps pinpoint exactly where a variable takes on an unexpected value
These statements are removed once the error has been found and corrected
Using a debugger
A debugger is a software tool that allows developers to step through a program's execution one statement at a time
Debuggers display the current value of variables and highlight the line of code being executed
They allow developers to set breakpoints (specific lines where execution pauses) so they can inspect the program's state at critical moments
Debuggers are especially useful for complex programs where hand tracing would be impractical
Examiner Tips and Tricks
The AP exam may present a code segment with an error and ask you to identify the type or suggest how to find it. If the program does not run at all, it is likely a syntax error. If it runs but gives the wrong answer, it is a logic error. Hand tracing is the most common method tested on the exam, so practise tracking variable values through each line of code, especially in loops and conditionals.
For the AP Create Performance Task, you are expected to test and debug your program before submission — on exam day you may be asked to describe an error you encountered and how you corrected it, so keep notes on any bugs you find and fix during development.
Worked Example
A student writes a program to calculate the average of three numbers. The program runs without any error messages, but the result displayed is always higher than expected. The student discovers that the program adds all three numbers but divides by 2 instead of 3.
Which type of error does this describe?
(A) Syntax error, because the code contains a mistake that prevents execution
(B) Runtime error, because the program crashes during execution
(C) Logic error, because the program runs but produces an incorrect result
(D) Overflow error, because the result exceeds the range of the variable
[1]
Answer:
(C) Logic error, because the program runs but produces an incorrect result [1 mark]
The program executes without crashing but divides by 2 instead of 3 — the code is syntactically valid but performs the wrong calculation, which is the definition of a logic error
Unlock more, it's free!
Was this revision note helpful?