Identifying and Correcting Errors
AP Computer Science Principles · Unit 1 — Creative Development
Four kinds of error
AP CSP expects you to classify an error by when and how it shows itself, not by how serious it feels.
- A syntax error is a mistake in which the rules of the programming language are not followed: a missing bracket, a misspelled keyword. The program cannot be translated, so it does not start at all.
- A runtime error is a mistake that occurs during the execution of a program. The program starts, runs some statements, and then reaches an operation that cannot be carried out — accessing
list[0]orlist[LENGTH(list) + 1], dividing by zero. In the exam's pseudocode, an index outside 1 toLENGTH(list)is a runtime error that terminates the program. What counts as a runtime error is defined by the language. - A logic error is a mistake in the algorithm or program that causes it to behave incorrectly or unexpectedly. The program starts and finishes normally; the output is simply wrong. Logic errors are the hardest to find because nothing announces them.
- An overflow error occurs when a computer attempts to handle a number that is outside the defined range of values — for example a value too large for the fixed number of bits the language uses. You will meet it again in Unit 2.
The same bug can produce different symptoms for different inputs. A procedure that divides by a count may give a wrong answer for one list (logic error) and crash for an empty list (runtime error). Classify the symptom described in the question.
Patterns of logic error that the exam reuses
Most logic errors in exam code come from a small set of patterns. Learn to look for them deliberately.
- Wrong boundary:
>where the specification needs≥, or a loop condition such asREPEAT UNTIL(i = LENGTH(list))that stops one element early. These are called off-by-one errors. - Bad initial value: starting a running maximum at 0 works for lists of positive numbers but fails when every value is negative. Starting it at the first element of the list is always safe for a non-empty list.
- Statement in the wrong place: an accumulator reset (
total ← 0) inside the loop instead of before it, or a counter incremented outside theIFthat should guard it. - Wrong combination of conditions:
ANDinstead ofOR, or two separateIFstatements where anIF … ELSEwas intended, so a value is changed twice.
To correct an error, first state the intended behaviour precisely, then find a specific input for which the actual behaviour differs, trace the code on that input, and locate the first statement whose effect does not match the intention. Change the smallest thing that fixes it, then re-run the tests that used to pass — a fix that breaks something else is not a fix.
Worked example: Một chỗ sai, hai triệu chứng
The procedure below is intended to return the average of the positive numbers in nums.
PROCEDURE positiveAverage(nums)
{
total ← 0
count ← 0
FOR EACH n IN nums
{
IF(n > 0)
{
total ← total + n
}
count ← count + 1
}
RETURN(total / count)
}(1) What does positiveAverage([4, -2, 8, 0]) return, and what should it return? (2) Classify the error and fix it. (3) After your fix, describe an input that still causes a different kind of error.
Solution
(1) Truy vết: n = 4 → total = 4, count = 1; n = −2 → không cộng, count = 2; n = 8 → total = 12, count = 3; n = 0 → không cộng (0 không dương), count = 4. Trả về 12 / 4 = 3. Giá trị đúng phải là trung bình của 4 và 8, tức 6.
(2) Chương trình chạy xong bình thường mà ra số sai, nên đây là lỗi logic, thuộc khuôn “câu lệnh đặt sai chỗ”: count ← count + 1 nằm ngoài khối IF nên đếm cả số âm và số 0. Sửa: chuyển dòng đó vào trong khối IF(n > 0), ngay sau total ← total + n. Khi đó lần chạy trên ra 12 / 2 = 6.
(3) Với danh sách không có số dương nào, ví dụ [-1, 0] hay danh sách rỗng, sau khi sửa thì count = 0 và lệnh total / count chia cho 0 — chương trình đã chạy được một đoạn rồi mới dừng, nên đó là lỗi lúc chạy. Muốn chặn, thêm một IF(count = 0) trước lệnh RETURN và trả về một giá trị quy ước do đặc tả định ra.