AP CSP
AP Computer Science Principles / All lessons / Big Idea 1: Creative Development
1Big Idea 1: Creative Development
AP CSP topic 1.4

Identifying and Correcting Errors

Errors are a normal part of programming. A syntax error breaks the rules of the language, so the program will not run. A logic error lets the program run but produces the wrong result. A run-time error happens while the program runs, like dividing by zero, and an overflow error happens when a number is too large to store. Programmers find errors by testing with chosen inputs, including edge cases, tracing code by hand, and adding output statements to check values.

Picture itSyntax errorDISPLAY("hi"Missing ) so it will not runLogic erroravg ← a + b / 2Runs, but the answer is wrongRun-time errorx ← 10 / 0Crashes while running
Three kinds of errors. A syntax error stops the program from running; a logic error runs but gives the wrong answer; a run-time error crashes while running.

Identifying and Correcting Errors vocabulary

  • Syntax error: A mistake in the rules of the language, such as a missing bracket, that stops the program from running.
  • Logic error: A mistake in the algorithm that makes the program behave incorrectly or give a wrong answer.
  • Run-time error: An error that happens while the program is running, often stopping it unexpectedly.
  • Edge case: An input at the extreme end of what is allowed, such as 0, an empty list, or the largest value.

AP-style practice questions

Try each one, then open the answer. To save your progress, take this quiz in the interactive course.

Skill: Code Analysis
The procedure below should return the average of three numbers.

PROCEDURE average(a, b, c)
{
   RETURN (a + b + c / 3)
}

1. average(3, 6, 9) returns 12 instead of 6. Which of the following best describes the error?

  1. (A) A syntax error, because the procedure will not run
  2. (B) A logic error, because the division happens before the addition
  3. (C) An overflow error, because the numbers are too large
  4. (D) A run-time error, because it divides by zero
Show answer

(B) A logic error, because the division happens before the addition The code runs but gives the wrong answer: c / 3 is computed first. That is a logic error.

Skill: Code Analysis

2. A program runs correctly for most inputs but crashes when a user enters 0 because it divides by that value. Which kind of error is this?

  1. (A) Syntax error
  2. (B) Run-time error
  3. (C) Lossless error
  4. (D) Rounding error
Show answer

(B) Run-time error Dividing by zero happens while the program is running, so it is a run-time error.

Skill: Computational Solution Design

3. A program should accept ages from 13 to 18. Which of the following sets of test inputs is best for finding errors?

  1. (A) 15, 16, 17
  2. (B) 12, 13, 18, 19
  3. (C) 14, 14, 14
  4. (D) 100, 200, 300
Show answer

(B) 12, 13, 18, 19 Testing values just inside and just outside the boundaries checks the edge cases.

Skill: Code Analysis

4. A programmer adds DISPLAY statements at several points in a program to see the value of a variable as it runs. What is the programmer most likely doing?

  1. (A) Compressing the program
  2. (B) Finding where a logic error happens
  3. (C) Encrypting the program
  4. (D) Adding a new feature
Show answer

(B) Finding where a logic error happens Displaying values while the program runs helps locate where results go wrong.

Open 1.4 in the interactive course