AP® Computer Science Principles review sheet from Aim for Five (aimforfive.com/csp/units/1/1-4)
Unit 1 · Topic 1.4
1.4 Identifying and Correcting Errors
Every program has errors at some point. This topic covers the four kinds the exam names (logic, syntax, run-time and overflow) and the ways programmers find and fix them, especially choosing good test cases that include values at the edges.
Key terms
- logic error
- syntax error
- run-time error
- overflow error
- test case (input and expected output)
- debugging
The four kinds of error
A logic error is a flaw in the steps themselves: the code does what you wrote, but what you wrote isn't what you meant. The program runs, but it gives the wrong answer. These are the hardest to spot because nothing crashes.
A syntax error breaks the rules of the programming language, like a missing parenthesis or a misspelled keyword. The program can't run at all until it's fixed. This segment is missing the closing parenthesis after the condition:
IF (temp > 90
{
DISPLAY("Too hot")
}
A run-time error happens while the program is running. Each language defines its own run-time errors. On the exam, the classic one is using a list index below 1 or above the list's length, which stops the program. Dividing by zero is another common one.
An overflow error happens when a number is too big (or too small) for the range a computer can represent. Many languages store whole numbers in a fixed number of bits, so there's a largest value; going past it causes overflow. You'll see more about this in 2.1.
A logic error in action
This procedure is supposed to count the scores of 70 or higher, because 70 is passing:
PROCEDURE countPassing(scores)
{
count ← 0
FOR EACH s IN scores
{
IF (s > 70)
{
count ← count + 1
}
}
RETURN(count)
}
countPassing([70, 85, 40]) returns 1, but the right answer is 2. The 70 is skipped because 70 > 70 is false. The program runs without any error message, which is what makes it a logic error. The fix is to use s ≥ 70.
Notice that a test like [71, 85, 40] would return 2, the right answer, and hide the bug. Only a test right at the boundary, 70, reveals it.
Choosing test cases
Testing means running a program on inputs you choose and comparing what it does with what you expected. A test case is an input together with its expected output. You use the program's requirements to decide which inputs to try, and you use the results to revise the program.
Good tests cover each different outcome the program can produce, and they include values at the extremes: the minimum and maximum allowed values, and values just beyond them. If valid ages are 13 to 18, test 12, 13, 18 and 19, not just 15.
Also try unusual cases: an empty list, a list with one item, all items the same, the target at the first or last position, and zero.
Ways to find and fix errors
Programmers use several methods together:
- Test cases: run the program on chosen inputs and compare with expected outputs.
- Hand tracing: follow the code line by line on paper, writing down each variable's value as it changes.
- Visualizations: tools that draw what the program is doing, like a chart of variable values or a picture of a list changing.
- Debuggers: tools that let you pause a program, run it one line at a time and inspect variables.
- Extra output statements: temporarily DISPLAY variable values in the middle of the code to see where things go wrong.
Worked examples
Try each one yourself first, then open the solution.
- Example 1
Finding the error and the test that reveals it
This procedure is supposed to return the average of a list of numbers.
PROCEDURE average(nums) { total ← 0 FOR EACH n IN nums { total ← total + n } RETURN(total / 2) }Which call shows that the procedure has an error:average([4, 6])oraverage([3, 6, 9])? What kind of error is it, and how do you fix it?Show the solutionHide the solution
- Step 1: Trace
average([4, 6]): total becomes 4, then 10. It returns 10 / 2 = 5, which is the correct average. This test hides the bug. - Step 2: Trace
average([3, 6, 9]): total becomes 3, 9, 18. It returns 18 / 2 = 9, but the real average is 18 / 3 = 6. This test reveals the bug. - Step 3: The program runs without crashing but gives the wrong answer, so it's a logic error. The divisor is always 2 instead of the number of values.
- Step 4: Fix: change the last line to
RETURN(total / LENGTH(nums)). Nowaverage([3, 6, 9])returns 6 andaverage([4, 6])still returns 5.
Answer:
average([3, 6, 9])reveals a logic error (it returns 9 instead of 6). Fix it by dividing byLENGTH(nums)instead of 2. - Step 1: Trace
- Example 2
Spotting a run-time error
What happens when this code segment runs?
nums ← [4, 8, 15] i ← 1 REPEAT 4 TIMES { DISPLAY(nums[i]) i ← i + 1 }Show the solutionHide the solution
- Step 1: The list has 3 elements, at indexes 1, 2 and 3. The loop runs 4 times.
- Step 2: Pass 1: i = 1, displays 4. Pass 2: i = 2, displays 8. Pass 3: i = 3, displays 15.
- Step 3: Pass 4: i = 4, but there's no index 4. On the exam, an index greater than the list's length produces an error and the program stops.
Answer: It displays 4 8 15, then stops with a run-time error when it tries to access
nums[4].
Common mistakes
- Calling a wrong answer a run-time error. If the program finishes but the result is wrong, it's a logic error.
- Testing only typical values. Most bugs hide at the boundaries, so test the minimum, the maximum and just beyond each.
- Thinking one passing test proves a program is correct. A test can show an error exists; it can't prove there are none.
On the exam
- Expect questions that show code with a bug and ask which change fixes it, or which input would reveal it. Trace each choice with a specific value rather than guessing.
- Written Response 2(b) asks about errors and testing in your own program, such as a call or input that would cause a wrong result and why. Know what your procedure should return for a few specific arguments, including edge cases.
Connected topics
Videos
Check yourself
4 questions on 1.4 Identifying and Correcting Errors. Pick an answer to see if you got it, and why.
A program is supposed to display the area of a rectangle. When the user enters a length of 4 and a width of 3, the program runs without stopping but displays 16. Which kind of error is this most likely to be?
A programmer forgets to type a closing parenthesis in one statement. When she tries to run the program, it will not start and the programming environment reports a problem with that line. Which kind of error is this?
A video game stores each player's score using 8 bits, so the largest score it can store is 255. A player who has 250 points earns 10 more, and the score shown becomes 4. Which kind of error has occurred?
The following code segment is intended to display the number of times "yes" appears in the list answers.
i ← 0
count ← 0
REPEAT LENGTH(answers) TIMES
{
IF (answers[i] = "yes")
{
count ← count + 1
}
i ← i + 1
}
DISPLAY(count)
When the code segment is run with answers containing ["yes", "no", "yes"], the program stops with an error message on the first pass through the loop. Which of the following best describes the error?
0 of 4 answered