AP · Computer Science Principles · Collaboration and Program Design

Finding and Fixing Errors

12 min readUpdated December 1, 2026FreeAP Computer Science Principles, Creative Development

Programs fail in four main ways: a syntax error breaks the language's rules, a runtime error stops the program while it runs, a logic error gives a wrong result, and an overflow error comes from a number too large for the space set aside for it. You find errors by testing with chosen inputs, especially boundary values, and locate them by hand-tracing, adding extra output and rerunning test cases.

Quick check

  1. Why does building a program in small, tested pieces make errors easier to find?Show answerHide answer

    A new error is most likely in the newest piece, because everything before it already passed its tests.

  2. In the exam's pseudocode the first list element is at index 1. What happens if code asks for aList[0]?Show answerHide answer

    There is no element at index 0, so it is an error that stops the program.

Know these already? Jump to the practice questions

What you'll be able to do

  • Identify syntax, runtime, logic and overflow errors from a description or code.
  • Choose test inputs that include boundary values.
  • Find the line that causes a logic error by hand-tracing.
  • Predict the extra output added for debugging.

Key terms

Syntax error

A mistake that breaks the rules of the programming language, such as a missing brace or a misspelled keyword, so the code cannot run as written.

In plain English
The code is not in a form the language accepts.
Don't write
"A syntax error means the answer is wrong."

Runtime error

An error that happens while the program is running and stops it, such as asking for a list index that does not exist.

In plain English
The program starts, then crashes.
Don't write
"A runtime error means the program runs slowly."

Logic error

A mistake in the algorithm that lets the program run but makes it give wrong or unexpected results.

In plain English
It runs, but the answer is wrong.
Don't write
"A logic error stops the program."

Overflow error

An error that happens when a program tries to handle a number outside the range that its fixed number of bits can represent.

In plain English
The number is too big for its space.
Don't write
"Overflow means a list has too many elements."

How do you tell the four errors apart?

Ask these questions in order:

QuestionIf yes
Does the code break the language's rules?Syntax error
Does the program stop partway with an error?Runtime error
Is a number too large for the bits that hold it?Overflow error
Does it run to the end with a wrong result?Logic error

Examples in pseudocode:

  • IF(x > 3 { … } is missing a closing parenthesis: a syntax error.
  • DISPLAY(scores[4]) when scores has 3 elements: an index above LENGTH(scores) is an error that stops the program (Learn this), so it is a runtime error.
  • count ← 1 where count ← count + 1 was meant: the program runs but miscounts, a logic error.
  • A program that stores whole numbers from 0 upward in 8 bits can hold only 0 to 255, so 255 + 1 cannot fit: an overflow error. The note on Overflow, Round-Off and Analog Data covers why.

Check yourself

A program runs to the end but shows an average of 0 for every class. Which type of error is it?
Show answerHide answer

A logic error: it runs without stopping, but the result is wrong.

How do you choose test inputs?

A test case is an input together with the output you expect, decided before you run the code. Pick three kinds of input:

  • typical values, the ones users will enter most;
  • boundary values: values at a condition's limit and just either side of it, so for IF(score ≥ 90) you test 89, 90 and 91;
  • unusual values: an empty list, a one-element list, 0, a negative number.

Boundaries matter because mixing up > and ≥, or a loop that runs once too many, changes the result only at the edge. A program can pass every typical test and still fail at 90.

Passing tests does not prove a program correct; it shows that it worked for those inputs. That is why the choice of inputs matters.

Check yourself

For IF(age < 13), which three ages test the boundary?
Show answerHide answer

12, 13 and 14: just below, at and just above the limit.

How do you find and fix a logic error?

Use three tools together:

  • Hand-trace: make a trace table with one column per variable and one row per step or loop pass, and compare each row with what should happen.
  • Extra output: add temporary DISPLAY statements that show variables while the program runs, then remove them once the bug is fixed.
  • Test cases: after each fix, rerun every test, not just the one that failed, because a fix can break a case that used to pass.

Change one thing at a time, so you know which change worked.

Key idea

The first trace row where a variable differs from what you expected is where to look for the bug.

Check yourself

Why rerun all the test cases after a fix, not only the failing one?
Show answerHide answer

A fix can break a case that passed before, and only rerunning every test shows that.

Worked example 1: Name the error

Question: A program should display the highest of a list of quiz scores. It runs without stopping, but for [7, 9, 4] it displays 4. Name the type of error.

What tells you the method

A description of behavior, so ask the four questions in order.

  1. Step 1: Does it break the language's rules, or stop partway?

    No: it runs to the end.

    Why

    That rules out syntax and runtime errors.

  2. Step 2: Is a number too large for its bits?

    No: 4, 7 and 9 are small.

    Why

    That rules out overflow.

  3. Step 3: Is the result wrong?

    Yes: the highest score is 9, not 4.

    Why

    Running to the end with a wrong result is a logic error.

Check it

Displaying 4, the last value, suggests the code stores each score instead of comparing them: a flaw in the algorithm, not in the language.

Answer

Logic error

Worked example 2: Trace to find the line

Question: findMax should return the largest value in any non-empty list. It passed the test [3, 8, 5]. Trace findMax([-4, -2, -7]) and give the number of the line that causes the error.

1: PROCEDURE findMax(nums)
2: {
3:    best ← 0
4:    FOR EACH n IN nums
5:    {
6:       IF(n > best)
7:       {
8:          best ← n
9:       }
10:   }
11:   RETURN(best)
12: }

What tells you the method

A test that should fail and a request for a line number, so trace the failing input and find where the values go wrong.

  1. Step 1: Trace the failing input.

    Passnn > bestbest
    start0
    1-4false0
    2-2false0
    3-7false0
    Why

    The procedure returns 0, but the largest value is -2.

  2. Step 2: Find the cause.

    best starts at 0, which is larger than every element, so no element replaces it. Line 3 should be best ← nums[1].

    Why

    Starting from a real element means best is always a value in the list.

Check it

With best ← nums[1], best starts at -4, becomes -2 on pass 2, and stays -2; and [3, 8, 5] still returns 8.

Answer

Line 3

Worked example 3: Choose tests that expose a boundary bug

Question: ticketPrice(age) should return 5 for ages under 12, 7 for ages 65 and over, and 12 for everyone else. Which set of test ages reveals the error in this procedure?

PROCEDURE ticketPrice(age)
{
   IF(age < 12)
   {
      RETURN(5)
   }
   IF(age > 65)
   {
      RETURN(7)
   }
   RETURN(12)
}
  • A) 5, 30, 70
  • B) 11, 12, 64
  • C) 10, 40, 80
  • D) 12, 64, 65

What tells you the method

"Which tests reveal the error" means find the boundary the code gets wrong, then the set that includes it.

  1. Step 1: Compare the code with the rule at each limit.

    age < 12 matches "under 12". But age > 65 excludes 65, which the rule says pays 7.

    Why

    The bug can show only at age 65.

  2. Step 2: Check which set includes 65.

    Only D. For 65, the code skips both IFs and returns 12 instead of 7.

    Why

    Every other age in every set gets the right price, so A, B and C all pass.

Check it

D's other ages: 12 returns 12 and 64 returns 12, both correct, so 65 alone exposes the bug.

Answer

D

Your turn

Question: This code should display the sum of nums. A student added the DISPLAY inside the loop to debug it. What is displayed?

nums ← [4, 6, 3]
total ← 0
FOR EACH n IN nums
{
   total ← n
   DISPLAY(total)
}
DISPLAY(total)
  1. Step 1: Pass 1.

    n is 4, so total ← n sets total to 4, and 4 is displayed.

  2. Step 2: Your go: trace passes 2 and 3, then the last line.

Show the full solutionHide the solution

Step 2. Pass 2 sets total to 6 and displays 6; pass 3 sets it to 3 and displays 3. After the loop, DISPLAY(total) shows 3 again. The extra output shows total never grows, so the fix is total ← total + n.

Answer: 4 6 3 3

Traps

Trap

If a program runs without stopping, it has no errors.

It seems right because syntax and runtime errors announce themselves. But a logic error runs to the end and gives a wrong answer, so only tests with known expected outputs reveal it.

A program finishes and displays 15, but the correct answer is 16. Which type of error?

A logic error.

Trap

Testing a few typical values is enough.

It seems right because typical values are what users enter. But errors such as > written for ≥ show only at the boundary, so test the limit and the values on either side.

Which single age best tests IF(age ≥ 18)?

18, the limit itself.

What's examined

On the AP® Computer Science Principles exam, this belongs to Big Idea 1: Creative Development. Multiple-choice questions describe a program's behavior and ask which type of error it has, which test inputs would reveal a bug, which line causes it, or what code with extra output displays. Testing your own Create program with planned inputs uses the same method.

Practice questions

  1. Recall Which code segment contains a syntax error?
  • A) IF(x > 3 { DISPLAY(x) }
  • B) IF(x > 3) { DISPLAY(x) }
  • C) IF(x ≥ 3) { DISPLAY(x + 1) }
  • D) IF(x < 3) { DISPLAY(3) }
Show answer

Answer: A. The condition is missing its closing parenthesis.

Why the others are wrong:

  • B is written correctly.
  • C uses ≥, which is a valid relational operator.
  • D is valid; displaying a constant is allowed.
  1. Recall scores ← [8, 6, 9, 7] runs, then DISPLAY(scores[5]). Name the type of error.
Show answer

Answer: Runtime error

Marking points:

  • scores has 4 elements, so index 5 is above LENGTH(scores).
  • The code is valid as written, but the program stops when it reaches that line.
  1. Apply Which set of inputs best tests the boundary of IF(score ≥ 90)?
  • A) 50, 75, 100
  • B) 90, 95, 100
  • C) 89, 90, 91
  • D) 0, 90, 180
Show answer

Answer: C. It tests just below, at and just above the limit.

Why the others are wrong:

  • A never tests 90, so score > 90 written for score ≥ 90 would pass all three.
  • B has nothing below 90, so a condition that is always true would pass.
  • D has nothing just below 90, so score ≥ 89 written for score ≥ 90 would pass all three.
  1. Apply countNeg should return how many values in nums are negative. Give the number of the line that stops it working.
1: PROCEDURE countNeg(nums)
2: {
3:    count ← 0
4:    FOR EACH n IN nums
5:    {
6:       IF(n < 0)
7:       {
8:          count ← 1
9:       }
10:   }
11:   RETURN(count)
12: }
Show answer

Answer: Line 8

Marking points:

  • For [-2, 5, -1], line 8 sets count to 1 twice, so the procedure returns 1 instead of 2.
  • The fix is count ← count + 1.
  1. Exam-style This code should display the product of vals. A student added DISPLAY inside the loop to debug it. What is displayed?
vals ← [2, 3, 4]
product ← 0
FOR EACH v IN vals
{
   product ← product * v
   DISPLAY(product)
}
Show answer

Answer: 0 0 0

Marking points:

  • product starts at 0, and 0 times any number is 0, so every pass displays 0.
  • The extra output points to the starting value: it should be product ← 1.
  1. Exam-style A game stores the score as a whole number in 8 bits, so it can hold 0 to 255. When the score is 255 and the player earns 1 point, the score shows 0. Which type of error is this?
  • A) Syntax error
  • B) Overflow error
  • C) Logic error
  • D) Runtime error
Show answer

Answer: B. 256 is outside the range 8 bits can represent.

Why the others are wrong:

  • A: the code runs, so it follows the language's rules.
  • C: the adding is correct; the result does not fit in its space.
  • D: the program keeps running rather than stopping.

Key points

Recall, then reveal

Say the main points from memory first, then check them.

Show key pointsHide key points
  • Syntax: breaks the rules. Runtime: stops partway. Logic: runs with a wrong result. Overflow: a number too big for its bits.
  • A test case pairs an input with the expected output, decided before running.
  • Test at a boundary and just either side of it, plus empty and unusual inputs.
  • Trace until a row differs from what you expected; that is where the bug is.
  • Temporary DISPLAY statements show how values change; remove them after the fix.

Keep learning

Explore AP Computer Science Principles tutoring →

View the full Computer Science Principles syllabus →

More in this unit

Creative Development

All Collaboration and Program Design notes

Part of Novark's AP Computer Science Principles notes