Verified for the 2025 AP Computer Science Principles exam•Citation:
Errors! Gotta love them. Errors happen all the time when we're programming. The AP test will ask you to identify errors and you'll have to face them when making your Create task. Coding errors are also known as bugs, and they happen to everyone who's ever typed a line of code in their lives. Debugging is part of the coding process, so don't get discouraged if you come across some in your code.
In this guide, we'll be discussing common errors, how to detect errors and how to fix them. We'll also be looking at how this topic will be tested on the AP Exam.
Here's a list of common errors to get you started on your bug-hunting.
Syntax Errors: A syntax error occurs when the spelling and/or punctuation rules of the programming language aren't followed. For example, forgetting to close a set of parentheses or spelling a variable wrong, could cause your entire program to crash. A syntax error could also be failing to indent properly. This dependence on perfectly correct syntax can be one of the more frustrating things about coding.
Fortunately, many code editors will point out syntax errors, making them one of the easier errors to solve.
Logic Errors: A mistake in a program's base logic that causes unexpected behavior. Besides syntax errors or errors caused by the limits of the device you're running your code on, this category covers most other errors.
Run-Time Errors: An error that occurs when the program is running. You'll be able to start your program if you have a run-time error, but something will go wrong when you're trying to use it.
Overflow Errors: An error that occurs when a computer tries to handle a number that's outside of its defined range of values. Usually, this means that your computing device is trying to handle a number too big for it to process.
If you're working with a coding language and an error crashes your program, there will often be an error message displayed. In the example for a syntax error above, the error message started with SyntaxError, making it clear what the error was.
However, error messages are not always so helpful. It's important to have many different methods to find and fix errors. Furthermore, bugs are tricky things, and it can be hard for even professional programmers to find and catch them all. (This is why a lot of software updates contain bug fixes. People are constantly finding and destroying bugs in their programs.)
(Source: GIPHY from Reddit)So now that we know what the common errors are, how do we fix them? Here are some effective ways to identify, find, and correct errors. Not every method will be suited to every bug, and it's perfectly alright to mix and match them depending on what you need.
Testing is the process of making sure your code works. It sounds simple, but it's really hard, even for professional programmers. In this section, we'll be discussing common challenges people face when testing, and good practices to make testing easier and more thorough.
Here are some challenges you might face as you're testing your code:
Good testing procedures include testing with many different test cases.
There's a lot more that could be said about testing, but this is a pretty good start! Now, let's take a look at an example of error-fixing on the AP test.
Question courtesy of the AP CSP CED, updated for 2021.
Image source: AP CED, page 184Give this problem a try!
The answer is C: Moving the statement in line 5 so that it appears between lines 2 and 3.
First, let's take a look at the program's purpose: returning the number of times the value val appears in a list. For example, if val equaled 5 and 5 showed up twice in the list imputed, the program would return 2.
** If you're having trouble on a code question, try rewriting the code to the side. This can help you see the code lines in a different light. Just make sure to pay attention to details such as spacing when you rewrite.**
Rewritten without the brackets, the code looks like this:
PROCEDURE countNumOccurences(myList, val)
FOR EACH item IN myList count ← 0 IF(item = val) count ← count + 1 RETURN(count)
Let's make a test list [5, 2, 3, 5] and set val equal to 5 to see what the error might be. The program should return a value of 2.
Starting from the beginning, we set the count to 0. The first number in the list equals the value of val, so count now equals 1.
Because we're in a loop (which we'll talk more about in Big Idea 3), you're going to go back through the loop for the second item in the list, which is 2. We set count to zero again, and...
Wait, that isn't supposed to happen! The error in this code is that every time the loop resets, count also resets to zero.
How would we fix that? By taking count ← 0 outside of the loop. We need it to be before the loop starts, so we have the count variable to work with, but it can't be inside the loop itself.
The loop begins in line 3, so putting count ← 0 between lines 2 and 3 works perfectly.
That's a lot to take in, isn't it? Props to you for making it this far!
In our next Big Idea Guide, we'll cover data: how computers use and process it!