📚

All Subjects

 > 

⌨️ 

AP Comp Sci P

 > 

🕹

Big Idea 1: Creative Development

Identifying and Correcting Errors

4 min readnovember 16, 2020

minnachow

Minna Chow

caroline49234295

Caroline Koffke


AP Computer Science Principles ⌨️

Bookmarked 2.3k • 69 resources
See Units

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.

Common Errors

Here's a list of common errors to get you started on your bug-hunting!
Logic Errors: A mistake in a program that causes unexpected behavior. For example, a program outputs a result different from the expected value, like saying that three times three equals thirty-three.
Syntax Errors: A syntax error occurs when the rules of the programming language aren't followed. Any typos in your code, such as forgetting to close a parentheses or spelling a variable wrong, will cause an error. Fortunately, many code editors will point out syntax errors, making them one of the easier errors to solve.
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. Logic errors are examples of run-time errors.
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 computer's trying to handle a number too big for it.

Error Fixes

Here are some effective ways to identify, find and correct errors.
  • Testing different inputs, and testing at different points in your program's runtime. 
    • Good testing procedures include testing with valid and invalid data. You want to make sure your program works with valid data and knows what to do if invalid data is imputed.
    • You should also test boundary cases, or values on the edge of program limits. This is where errors are likely to happen. For example, if your program only accepts values less than 9, you would check 8 and 10 as boundary cases.
  • Hand tracing, or manually tracking what values your variables have as your program goes along. Many hand tracing methods use charts to organize these values.
  • Using visualizations, or visual representations of your code. Try plugging in some code into this visualization toolto see how it works!
    • Although they're helpful, you won't need to know how to use visualization tools for the AP test or to write your Create program.
  • Using debuggers, or programs designed specially to test for bugs
  • Adding extra print statements to your code to make sure that the "invisible" steps (the ones that aren't shown as output) are working properly.
  • https://firebasestorage.googleapis.com/v0/b/fiveable-92889.appspot.com/o/images%2F-rJBMa1ekgXxU.jpg?alt=media&token=6be711a5-b485-4d01-8c52-e9a5cd0d5f28

    Adding extra print statements is also a common way to test code. In this example, the numbers 5 and 7 are printed in order to make sure that the input system is working properly.

Let's take a look at an example of error-fixing on the AP test.

Error-Hunting Example

https://firebasestorage.googleapis.com/v0/b/fiveable-92889.appspot.com/o/images%2F-YlqDhGk89dk6.png?alt=media&token=d75fc2f6-bc13-465d-8a97-ada9b9fa421b

Image source: AP CED, page 184

Give this problem a try!

Answer

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.

Whew!

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!

Was this guide helpful?

Join us on Discord
Thousands of students are studying with us for the AP Computer Science Principles exam.
join now
Hours Logo
Studying with Hours = the ultimate focus mode
Start a free study session
🔍 Are you ready for college apps?
Take this quiz and find out!
Start Quiz
Browse Study Guides By Unit
🕹Big Idea 1: Creative Development
⚙️Big Idea 2: Data
📱Big Idea 3: Algorithms and Programming
🖥Big Idea 4: Computer Systems and Networks
⌨️Big Idea 5: Impact of Computing
✏️Blogs
📝Exam Prep
Join us on Discord
Thousands of students are studying with us for the AP Computer Science Principles exam.
join now
💪🏽 Are you ready for the AP CSP exam?
Take this quiz for a progress check on what you’ve learned this year and get a personalized study plan to grab that 5!
START QUIZ
Play this on HyperTyper
Practice your typing skills while reading Identifying and Correcting Errors
Start Game