AP® Computer Science A review sheet from Aim for Five (aimforfive.com/csa/units/1/1-1)
Unit 1 · Topic 1.1
1.1 Introduction to Algorithms, Programming, and Compilers
An algorithm is a set of steps for solving a problem, and a Java program is an algorithm written so a computer can follow it. This topic covers how code gets from your editor to a running program, and the three kinds of errors you'll need to recognize all year.
Key terms
- algorithm
- sequencing
- compiler
- syntax error
- logic error
- run-time error and exception
Algorithms and sequencing
An algorithm is a step-by-step process for finishing a task or solving a problem. You follow algorithms all the time: a recipe, the directions to a friend's house, the steps for logging in to a website.
You can describe an algorithm in plain sentences, as a numbered list, or as a diagram such as a flowchart, where boxes are steps and arrows show what comes next. Writing the steps out before you code is one of the best ways to plan.
Sequencing means the steps happen one at a time, in a set order. Order matters. "Pour the cereal, then add milk" works; "add milk, then pour the cereal" gives you a mess in a different spot. In code, swapping two lines can change the result in the same way.
From code to a running program
You can type Java in any plain text editor, but most programmers use an IDE (integrated development environment). An IDE is one app that lets you write, compile and run your code, and it usually highlights mistakes as you type.
Java source code has to be compiled before it runs. The compiler reads your whole program and checks that it follows Java's rules. If it finds errors, it reports them and the program can't run until you fix them. If there are none, the program runs, starting at the main method.
Here is a complete Java program. Every statement ends in a semicolon, and the curly braces mark where the class and the method begin and end.
public class Hello
{
public static void main(String[] args)
{
System.out.println("Hello, AP!");
}
}
Running it prints Hello, AP! and then moves to a new line.
Three kinds of errors
The exam expects you to name the kind of error a piece of code has, so learn these carefully.
| Error | What it is | How you find it | Example |
|---|---|---|---|
| Syntax error | The code breaks Java's rules | The compiler reports it, and the program can't run | A missing semicolon or an unmatched brace |
| Logic error | The program runs but does the wrong thing | Testing with data whose correct answer you know | Writing a + b / 2 when you meant (a + b) / 2 |
| Run-time error | Something goes wrong while the program is running | The program usually stops early (it crashes) | Dividing an int by zero |
Exceptions
An exception is one type of run-time error. It happens when the program hits an unexpected situation the compiler couldn't catch ahead of time. The exception interrupts the normal flow of the program, and unless something handles it, the program stops and prints the exception's name.
You'll meet several exceptions this year: ArithmeticException (dividing an int by zero), NullPointerException (calling a method on null), StringIndexOutOfBoundsException and ArrayIndexOutOfBoundsException (using an index that doesn't exist). You only need to know when they happen, not how to handle them.
Testing finds logic errors
The compiler can't tell you whether your program gives the right answer. That's what testing is for: run the program on inputs where you already know the correct output, and compare. Pick ordinary inputs and edge cases, like 0, 1, an empty string, or the largest allowed value.
Worked examples
Try each one yourself first, then open the solution.
- Example 1
Naming the error
Each code segment below is supposed to do something sensible. Name the kind of error in each one. Segment 1:
int total = 5 System.out.println(total);Segment 2 (meant to print the average ofa,bandc):int a = 3; int b = 6; int c = 9; int avg = a + b + c / 3; System.out.println(avg);Segment 3:int students = 28; int groups = 0; int perGroup = students / groups; System.out.println(perGroup);Show the solutionHide the solution
- Step 1: Segment 1: the first statement has no semicolon. That breaks Java's rules, so the compiler rejects the program before it can run. This is a syntax error.
- Step 2: Segment 2: it compiles and runs, but division happens before addition, so it computes
3 + 6 + (9 / 3), which is3 + 6 + 3, or 12. The real average of 3, 6 and 9 is 6. A wrong answer from a program that runs is a logic error. The fix is(a + b + c) / 3. - Step 3: Segment 3: it compiles, but when the third line runs it divides the
int28 by theint0. Java throws anArithmeticExceptionand the program stops, so nothing is printed. This is a run-time error, and specifically an exception.
Answer: Segment 1 is a syntax error (missing semicolon), Segment 2 is a logic error (it prints 12 instead of 6), and Segment 3 is a run-time error: it throws an
ArithmeticException.
Common mistakes
- Calling every mistake a "syntax error". If the program compiles and runs but gives the wrong answer, it's a logic error; if it crashes while running, it's a run-time error.
- Thinking the compiler catches everything. It only catches rule-breaking code. Dividing by a variable that happens to be 0 compiles fine and fails only when it runs.
- Forgetting that order matters. Two correct statements in the wrong order can produce a logic error.
On the exam
- Recent change: the exam is now fully digital in Bluebook. You type your free-response code instead of writing it by hand, but nothing compiles or runs it, so finding your own logic errors is up to you. Readers ignore small slips, like a missing semicolon, when your meaning is clear. Multiple-choice questions now have 4 choices (A to D).
- Multiple-choice questions often show a code segment and ask what happens: does it fail to compile, throw an exception, or run and print something? Check for syntax problems first, then trace it to look for run-time errors.
- When a question says a method "does not work as intended", it's describing a logic error. Find an input where the output is wrong.
Connected topics
Videos
Check yourself
4 questions on 1.1 Introduction to Algorithms, Programming, and Compilers. Pick an answer to see if you got it, and why.
A student's program is supposed to print the average of three test scores. It compiles and runs without stopping, but for the scores 80, 90 and 100 it prints 203.33333333333334 instead of 90.0. Which kind of error does the program have?
A programmer leaves the semicolon off the end of a statement. When is this error detected?
Consider the following two code segments.// Segment 1
int x = 5;
x = x * 2;
x = x + 3;
System.out.println(x);and// Segment 2
int x = 5;
x = x + 3;
x = x * 2;
System.out.println(x);Which of the following best describes the output of the two segments?
Consider the following code segment.int total = 45;
int count = 0;
System.out.println("Start");
System.out.println(total / count);What is printed as a result of executing the code segment?
0 of 4 answered