AP CSP topic 1.4
Identifying and Correcting Errors
Errors are a normal part of programming. A syntax error breaks the rules of the language, so the program will not run. A logic error lets the program run but produces the wrong result. A run-time error happens while the program runs, like dividing by zero, and an overflow error happens when a number is too large to store. Programmers find errors by testing with chosen inputs, including edge cases, tracing code by hand, and adding output statements to check values.
Identifying and Correcting Errors vocabulary
- Syntax error: A mistake in the rules of the language, such as a missing bracket, that stops the program from running.
- Logic error: A mistake in the algorithm that makes the program behave incorrectly or give a wrong answer.
- Run-time error: An error that happens while the program is running, often stopping it unexpectedly.
- Edge case: An input at the extreme end of what is allowed, such as 0, an empty list, or the largest value.
AP-style practice questions
Try each one, then open the answer. To save your progress, take this quiz in the interactive course.
The procedure below should return the average of three numbers.
PROCEDURE average(a, b, c)
{
RETURN (a + b + c / 3)
}
1. average(3, 6, 9) returns 12 instead of 6. Which of the following best describes the error?
- (A) A syntax error, because the procedure will not run
- (B) A logic error, because the division happens before the addition
- (C) An overflow error, because the numbers are too large
- (D) A run-time error, because it divides by zero
Show answer
(B) A logic error, because the division happens before the addition The code runs but gives the wrong answer: c / 3 is computed first. That is a logic error.
2. A program runs correctly for most inputs but crashes when a user enters 0 because it divides by that value. Which kind of error is this?
- (A) Syntax error
- (B) Run-time error
- (C) Lossless error
- (D) Rounding error
Show answer
(B) Run-time error Dividing by zero happens while the program is running, so it is a run-time error.
3. A program should accept ages from 13 to 18. Which of the following sets of test inputs is best for finding errors?
- (A) 15, 16, 17
- (B) 12, 13, 18, 19
- (C) 14, 14, 14
- (D) 100, 200, 300
Show answer
(B) 12, 13, 18, 19 Testing values just inside and just outside the boundaries checks the edge cases.
4. A programmer adds DISPLAY statements at several points in a program to see the value of a variable as it runs. What is the programmer most likely doing?
- (A) Compressing the program
- (B) Finding where a logic error happens
- (C) Encrypting the program
- (D) Adding a new feature
Show answer
(B) Finding where a logic error happens Displaying values while the program runs helps locate where results go wrong.