AP CSP 1.4 Identifying and Correcting Errors | Debugging and Testing

AP CSP Course Big Idea 1 1.4 Identifying and Correcting Errors
1.4
Big Idea 1 • Creative Development

Identifying and Correcting Errors

🕐 ~35 min FREE 📖 6 MCQ questions 🎮 Bug Squasher game CRD-2.H / CRD-2.I / CRD-2.J

After this lesson, you will be able to:

  • Distinguish logic, syntax, run-time, and overflow errors by how each one shows up
  • Explain why a program with a logic error still runs while a syntax error stops it from running
  • Design test cases that include typical, boundary, and invalid inputs to expose errors
  • Apply debugging strategies such as hand-tracing, adding display statements, and isolating sections of code
  • Describe how documentation and collaboration make errors easier to find and correct
📈 Big Idea 1 (Creative Development) is 10 to 13 percent of the AP CSP exam, and error-and-testing questions are among the most frequent. This topic also drives the written responses on the Create Performance Task, where you must describe how you tested your program and handled its behavior, so mastering it pays off twice.
💡 Think about this first

A program that calculates the average of a class quiz runs with no red error messages, prints a clean number, and looks finished. But when a student scores a perfect 100 on every quiz, the program reports an average of 91. Nothing crashed, nothing was underlined in red, and yet the answer is wrong. What kind of error is this, and what single test case would have caught it before the program shipped?

Four Kinds of Errors

An error is any flaw that keeps a program from doing what it should. The AP CSP framework names four types, and the exam expects you to tell them apart by how they show up, not just by their definitions. The single most useful question to ask is: does the program still run?

Error type Does it run? Example
Syntax error No. It violates the rules of the language, so the program will not run at all. A missing quotation mark or an unmatched parenthesis, such as DISPLAY("hi)
Logic error Yes. It runs, but produces incorrect or unintended output. Using + where you meant *, so the total is wrong but no error is shown
Run-time error It starts, then fails while running. Dividing by zero, or reading a list value that does not exist
Overflow error It runs, then breaks or wraps. A number grows too large to fit in the space allotted to represent it

Read that table carefully. A syntax error is caught before the program even starts because the language cannot understand it. A logic error is the sneakiest because everything looks fine, the program runs to completion, and only the answer is wrong. A run-time error happens partway through, often because of a specific input like a zero or a missing value. An overflow error is a special case where a value is simply too big for the number of bits allotted to store it.

🎯 What the exam rewards

If a program runs and finishes but the output is wrong, the answer is almost always a logic error. If the program will not run at all, it is a syntax error. If it runs for a while and then stops, look for a run-time error tied to a specific input. Anchor your choice to whether and when the program runs.

Quick check
A program to find the largest of three numbers runs completely and prints a result every time, but when the largest value is the last one entered, it prints the wrong number. What type of error is this?

Testing: Trying to Break Your Own Program

Testing is running a program with a variety of inputs to find errors and check that it behaves correctly. The goal is not to confirm that the program works on the one input you already tried; it is to actively hunt for the input that makes it fail. Good testing covers three kinds of input:

  • Typical inputs, the ordinary values a user is expected to enter, to confirm normal behavior.
  • Boundary (edge) cases, the extremes and just-past-extremes such as zero, an empty list, a maximum value, or the very first and very last items. Most bugs hide here.
  • Invalid inputs, values the program should reject, such as a letter where a number is expected or a negative age, to check that the program handles bad data gracefully.

The quiz-average program from the hook is a perfect example: it passed on a mix of scores but failed on the boundary case of all perfect scores. A single well-chosen edge case would have exposed the logic error immediately.

⚠ Common trap

"The program worked when I ran it, so it is correct." Running a program once with one friendly input is not testing. A program can pass every typical case and still fail on an empty list, a zero, or the last element of a range. If a question offers "test it with one normal input" versus "test it with typical, boundary, and invalid inputs," the thorough option is the right one.

Debugging: Finding and Fixing the Problem

Debugging is the process of finding and correcting errors. Testing tells you that something is wrong; debugging is how you locate where and fix it. The framework expects you to know several concrete strategies, not just the word "debug":

  • Hand-tracing (tracing code by hand): stepping through the program line by line on paper, tracking each variable's value, to see where reality diverges from what you expected.
  • Adding display or print statements to reveal intermediate values while the program runs, so you can watch a variable change and spot the moment it goes wrong.
  • Testing sections in isolation: running one procedure or block by itself, apart from the rest, to confirm that piece works before trusting the whole.
  • Commenting out code to temporarily remove a suspect section and see whether the problem disappears, which narrows down where the bug lives.
  • Studying the error message: a run-time or syntax error usually reports a line number and a description that points you straight at the trouble.

Consider a loop that is supposed to add up a list but reports a total that is always one item short:

total <- 0
FOR EACH n IN [3, 5, 8]
{
  total <- n
}
DISPLAY(total)

Hand-tracing exposes the bug at once: each pass replaces total instead of adding to it, so the program prints 8 rather than 16. The fix is total <- total + n. This is a classic logic error, invisible until you trace the values, and a display statement inside the loop would have made it obvious.

🎯 What the exam rewards

Documentation and collaboration are debugging aids, not just nice-to-haves. Clear comments and a teammate reviewing your code (a navigator, from Topic 1.1) make errors easier to find because a second reader with fresh eyes catches assumptions the author cannot see. Expect the exam to credit "add a comment / get a second reader" as a legitimate error-reduction strategy.

Quick check
You suspect a specific loop is producing wrong values, but the program runs without crashing. Which debugging strategy most directly lets you watch the loop's variable change on each pass?

How This Shows Up on the Create Performance Task

The Create Performance Task is completed individually under current AP rules, so the program and the written responses must be your own. Topic 1.4 is central to it. Your program must include a call to a student-developed procedure, and the written responses ask you to describe how that procedure works and how you know your program behaves correctly. That is a testing-and-debugging story.

Specifically, you should test your program with a variety of inputs (including edge cases such as empty input, the smallest and largest values, and invalid entries) and be ready to explain in writing how you identified and corrected errors, or how the program handles unexpected behavior. Reviewers reward responses that show you traced or tested the program rather than just claiming it works. When you describe your procedure and the two calls that produce different results, you are demonstrating exactly the testing thinking this lesson teaches. Keep clear comments in your code so your written explanation matches what the program actually does.

📈
MCQ Practice
6 questions • Exam difficulty and above • Predict before you peek
Question 1 of 6Classification
Decide whether the program runs before you look at the options.

A programmer writes a line of code but forgets to close a set of parentheses. When they try to run the program, it will not start at all and no output is produced. Which type of error is this?

Incorrect. A logic error lets the program run to completion and produce wrong output. This program never runs at all.
Incorrect. A run-time error happens while the program is running. This program never starts.
Correct. Unmatched parentheses violate the rules of the language, so the program cannot run. That is a syntax error.
Incorrect. An overflow error involves a number too large for its allotted space, not a punctuation mistake that prevents the program from starting.
Question 2 of 6Spot the error
Trace the loop by hand and predict what prints before reading the choices.

The procedure below is intended to return the sum of the numbers in the list. Study it, then choose the best description of the error.

PROCEDURE sumList(nums)
{
  total <- 0
  FOR EACH n IN nums
  {
    total <- n
  }
  RETURN total
}
Incorrect. The pseudocode is valid and would run; nothing here violates the language rules.
Correct. Writing total <- n overwrites the running total each pass, so the procedure returns the final element instead of the sum. The fix is total <- total + n. The program runs but is wrong, which is a logic error.
Incorrect. The procedure runs fine on a non-empty list; it simply returns the wrong number. It does not crash.
Incorrect. Nothing here concerns a value too large for its space; the flaw is in how total is updated.
Question 3 of 6Testing
Predict which single input is most likely to expose a hidden bug.

A student wrote a program that computes the average score of a list of quiz grades and only tested it once, with the list [80, 90, 100], getting a reasonable result. Which additional test case is most likely to reveal a hidden error in the program?

Incorrect. Another ordinary list mostly repeats a case that already passed and is unlikely to expose a boundary bug.
Incorrect. Re-running the identical input gives no new information about the program's behavior.
Correct. An empty list is a boundary case that can trigger a division by zero or other failure the typical inputs never reach. Edge cases expose the most bugs.
Incorrect. Reordering the same values usually yields the same average and does not probe a boundary condition.
Question 4 of 6I and II only style
Judge each statement true or false before matching to an option.

Consider these statements about errors in a program:

  • I. A syntax error prevents a program from running at all.
  • II. A logic error can be present in a program that runs to completion and produces output.
  • III. A run-time error is always detected before the program begins executing.
Incorrect. Statement II is also true; a running program can still contain a logic error.
Correct. I and II are true. III is false: a run-time error, by definition, occurs while the program is running, not before it starts.
Incorrect. Statement III is false, so any option that includes III cannot be correct.
Incorrect. Statement III is false. A run-time error surfaces during execution, not before it begins.
Question 5 of 6Debugging strategy
Predict the most efficient way to locate the bug first.

A long program produces an incorrect final result but does not crash. A programmer wants to find which section is responsible without rewriting everything. Which approach best isolates the faulty section?

Incorrect. The same input on unchanged code produces the same wrong result and reveals nothing new.
Correct. Commenting out sections and printing intermediate values narrows down exactly where a correct value turns into a wrong one, isolating the faulty section.
Incorrect. Adding features increases complexity and makes the existing bug harder, not easier, to find.
Incorrect. Blaming the language is almost never the cause and abandons the systematic debugging that would locate the real error.
Question 6 of 6Application
Predict which error type matches a value that is too big to store.

A program repeatedly doubles a whole number stored in a fixed amount of memory. After many doublings the value suddenly becomes negative or wildly incorrect, even though the code and inputs are valid. Which type of error best explains this behavior?

Incorrect. The code runs correctly for many steps, so it is valid syntax; the problem appears only after the value grows large.
Incorrect. The doubling itself is intended; the failure is about representing the resulting value, not about choosing the wrong operation.
Correct. When a number exceeds the fixed space allotted to store it, the representation breaks and can wrap to a wrong or negative value. That is an overflow error.
Incorrect. The program keeps running and produces a value; it just produces a corrupted one. Nothing here reports a crash.
🎮 Lesson Game
Bug Squasher
Classify each error and choose the testing move that would catch it.

🐛 Bug Squasher

AP CSP 1.4 • Identify and correct errors: syntax, logic, runtime, and overflow.

How to play: read the buggy code, first CLASSIFY the error type, then pick the FIX. Beat the timer and build a streak!

Round
1/10
Score
0
Streak
0

Frequently Asked Questions

A syntax error breaks the rules of the language, so the program will not run at all. A logic error lets the program run to completion but produces incorrect or unintended output. The quick test: if it will not run, suspect syntax; if it runs but the answer is wrong, suspect logic.
A run-time error occurs while the program is executing, after it has started. Common causes are dividing by zero or trying to use a value that does not exist, such as an item beyond the end of a list. The program begins normally and then fails at the moment it hits the bad operation.
Because most bugs hide at the boundaries. A program often works for typical inputs but breaks on an empty list, a zero, a maximum value, or the first and last elements of a range. Testing typical, boundary, and invalid inputs together is far more likely to expose errors than running one friendly input.
Hand-tracing the code line by line, adding display or print statements to inspect intermediate values, testing sections of the program in isolation, commenting out code to narrow down the problem, using varied test cases, and reading error messages. Documentation and a second reviewer also make errors easier to find.
The Create Task requires a call to a student-developed procedure, and the written responses ask how you know your program works. You demonstrate that by testing with varied inputs, including edge cases, and describing how you identified and corrected errors or handled the program's behavior. The task is completed individually.
📦
AP CSP Teacher SuperpackSlides, lesson plans, unit tests for all 5 Big Ideas, $249
Get the Superpack →
🏫
For teachers

Errors and testing are where students most often confuse vocabulary, so drill the runs-but-wrong versus will-not-run distinction relentlessly. Give buggy code and have students classify the error, propose a test case that would catch it, and name a debugging strategy to locate it. The Superpack includes a bug-classification sorting deck, an edge-case testing worksheet, and hand-tracing practice with answer keys. 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.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]