Mục lục bài họcĐang ở d01-b3
Identifying and Correcting Errors
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.
AP CSP muốn bạn phân loại lỗi theo lúc nào và cách nào nó lộ ra, không theo cảm giác nặng nhẹ.
- Lỗi cú pháp (syntax error): không theo đúng luật của ngôn ngữ — thiếu ngoặc, gõ sai từ khoá. Chương trình không dịch được nên không chạy được câu lệnh nào.
- Lỗi lúc chạy (runtime error): xảy ra trong khi chương trình thực thi. Chương trình đã chạy, đi qua vài câu lệnh, rồi gặp một thao tác không làm được — truy cập
list[0]hoặclist[LENGTH(list) + 1], chia cho 0. Trong pseudocode của đề thi, chỉ số nằm ngoài khoảng 1 đếnLENGTH(list)là lỗi lúc chạy và làm chương trình dừng. Cái gì tính là lỗi lúc chạy do từng ngôn ngữ quy định. - Lỗi logic (logic error): sai trong thuật toán hay chương trình khiến nó cư xử sai hoặc không như mong đợi. Chương trình chạy và kết thúc bình thường, chỉ có kết quả sai. Đây là loại khó tìm nhất vì không có gì báo hiệu.
- Lỗi tràn số (overflow error): máy cố xử lý một số nằm ngoài phạm vi giá trị đã định — ví dụ số quá lớn so với số bit cố định mà ngôn ngữ dùng. Unit 2 sẽ gặp lại.
Cùng một chỗ sai có thể cho triệu chứng khác nhau với đầu vào khác nhau. Thủ tục chia cho biến đếm có thể trả kết quả sai với danh sách này (lỗi logic) và dừng đột ngột với danh sách rỗng (lỗi lúc chạy). Hãy phân loại theo triệu chứng mà đề mô tả.
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.
Phần lớn lỗi logic trong mã đề thi đến từ một nhóm nhỏ khuôn mẫu. Hãy tập đi tìm chúng có chủ đích.
- Sai biên: dùng
>trong khi đặc tả cần≥, hoặc điều kiện vòng lặp kiểuREPEAT UNTIL(i = LENGTH(list))làm dừng sớm một phần tử. Đây gọi là lỗi lệch một (off-by-one). - Giá trị khởi đầu sai: đặt biến “lớn nhất tới giờ” bằng 0 thì đúng với danh sách số dương nhưng sai khi mọi giá trị đều âm. Đặt bằng phần tử đầu của danh sách thì luôn an toàn với danh sách khác rỗng.
- Câu lệnh đặt sai chỗ: lệnh đặt lại biến cộng dồn (
total ← 0) nằm trong vòng lặp thay vì trước vòng lặp; biến đếm tăng ở ngoài khốiIFlẽ ra phải canh nó. - Ghép điều kiện sai:
ANDthay choOR, hoặc hai lệnhIFrời nhau ở chỗ lẽ ra làIF … ELSE, khiến một giá trị bị sửa hai lần.
Để sửa lỗi: trước hết phát biểu chính xác hành vi mong muốn; tìm một đầu vào cụ thể mà hành vi thật khác mong muốn; truy vết mã với đầu vào đó; tìm câu lệnh đầu tiên có tác dụng không khớp ý định. Sửa chỗ nhỏ nhất đủ để đúng, rồi chạy lại các ca kiểm trước đây đã qua — bản sửa làm hỏng chỗ khác thì chưa phải bản sửa.
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.
(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.
Đọc xong rồi — làm thử ngay
Bài tập của chương Unit 1 — Creative Development gồm 14 câu trắc nghiệm và 5 đề tự luận. Đáp án hiện ngay khi chọn, miễn phí.