Topic 1.4: Identifying and Correcting Errors | AP CSP Big Idea 1 | APCSExamPrep.com
Identifying and Correcting Errors
After this lesson, you will be able to:
- Identify and distinguish between the four types of programming errors: logic, syntax, run-time, and overflow
- Apply debugging strategies including test cases, hand tracing, visualizations, debuggers, and extra output statements
- Design test cases that cover expected values and edge cases at input extremes
- Explain why programs that run without crashing can still contain errors
In 1999, NASA lost a $327 million Mars orbiter because one engineering team used metric units and another used imperial units. The software ran perfectly — no crashes, no error messages. It just silently calculated the wrong trajectory. That's a logic error at $327 million scale. Understanding the difference between 'the program ran' and 'the program was correct' is the core insight of this topic.
The Four Types of Errors
The AP CSP CED defines four specific types of programming errors. You must be able to identify each type from a description or code example.
1. Logic Error
A logic error is a mistake in the algorithm or program that causes it to behave incorrectly or unexpectedly. Programs with logic errors can still run — they just produce wrong results. This makes logic errors the hardest to catch: there's no error message telling you something is wrong.
Example: A program that calculates a student's letter grade uses > instead of >= for the boundary between A and B. The program runs, assigns grades, and produces output — but every student right at the boundary gets the wrong grade.
2. Syntax Error
A syntax error is a mistake where the rules of the programming language are not followed. The program typically cannot run at all until the syntax error is fixed. The interpreter or compiler catches these before execution.
Example: Writing if x = 5 instead of if x == 5 in Python. The language doesn't understand the single equals in this context and refuses to run the program.
3. Run-Time Error
A run-time error is a mistake that occurs during execution — after the program has started running. The program may run fine until it hits a specific condition, then crash or produce unexpected behavior. Each programming language defines its own run-time errors.
Example: A program that divides a number by a user-provided value runs fine until the user enters 0. Division by zero causes a run-time error that crashes the program.
4. Overflow Error
An overflow error occurs when a computer attempts to handle a number outside the defined range of values for a data type. Computers store numbers in fixed amounts of memory, which limits the size of numbers they can represent.
Example: If a system uses 8 bits to store a non-negative integer, it can represent 0–255. Adding 1 to 255 produces an overflow — the result wraps around or causes an error because 256 can't be stored in 8 bits.
The most commonly confused pair is logic errors vs. run-time errors. Remember: a logic error means the program runs but produces wrong results. A run-time error means the program crashes during execution. If the program runs to completion but gives the wrong answer — that's a logic error.
Debugging Strategies
Finding and fixing errors requires systematic approaches. The CED identifies five effective debugging strategies:
1. Test Cases
A test case is a defined input used to check whether a program produces the expected output. Good test cases cover:
- Typical values in the middle of the input range
- Boundary values at or just beyond the minimum and maximum of valid input
- Edge cases that might trigger unexpected behavior
For a function that accepts integers 1–100: test with 1, 100, 50, 0, and 101. Testing only with 50 would miss most bugs.
2. Hand Tracing
Hand tracing means manually following the program's logic step by step on paper, tracking the value of each variable through each line. It's slow but thorough — and it's the method that reveals logic errors that hide from automated tools.
3. Visualizations
Visualizations are tools that display the state of a program's variables and data structures during execution. Watching a sorting algorithm rearrange an array in real time, for example, makes it immediately obvious when the algorithm is wrong.
4. Debuggers
A debugger is a software tool that pauses execution at specified points (called breakpoints) and allows the developer to inspect the current state of the program — variable values, the call stack, which line is executing. Most modern development environments include built-in debuggers.
5. Adding Extra Output Statements
Adding temporary DISPLAY or print statements throughout a program to show variable values at key points is one of the simplest and most effective debugging techniques. Once the bug is found and fixed, the extra statements are removed.
The exam sometimes asks which debugging strategy is 'most appropriate' for a given scenario. Remember that hand tracing and test cases are used to find logic errors. Debuggers are used when you need to inspect program state during execution. Extra output statements work anywhere but are especially useful when you can't use a debugger.
Key Vocabulary
| Term | AP Definition | Plain English |
|---|---|---|
| Logic error | A mistake in the algorithm causing incorrect or unexpected behavior; program still runs | The program works but does the wrong thing |
| Syntax error | A violation of programming language rules; program typically cannot run | The program breaks the grammar rules of the language |
| Run-time error | An error that occurs during execution; program crashes or behaves unexpectedly | The program starts fine but crashes when it hits a problem |
| Overflow error | An error when a number exceeds the defined range of values a system can handle | The number is too big (or too small) for the computer to store |
| Test case | A defined input used to verify a program produces expected output | A specific test you run to check if the program works correctly |
| Hand tracing | Manually following program logic step by step, tracking variable values | Walking through the code yourself on paper to find bugs |
| Debugger | A tool that pauses execution and allows inspection of program state | A tool that lets you freeze a running program and look inside it |
The concepts in this topic connect directly to your Create Performance Task. Understanding program purpose, iterative design, and how to identify errors will help you write stronger CPT responses. See the Create Task module →
Get a free AP CSP question every day
Join 3,000+ students. Daily practice questions, study tips, and exam strategies — straight to your inbox.
Loading...
Frequently Asked Questions
🔗 Continue studying AP CSP
The Superpack includes a full lesson plan for this topic with day-by-day pacing, editable slides, student guided notes, and a unit test with answer key covering all of Big Idea 1. View what's included →
Get in Touch
Whether you're a student, parent, or teacher — I'd love to hear from you.
Just want free AP CS resources?
Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]