# AP® Computer Science A 1.1 Introduction to Algorithms, Programming, and Compilers

Source: https://lockincs.com/ap-csa/unit-1/1-1-introduction-to-algorithms-programming-and-compilers/  
Last reviewed against the College Board Course and Exam Description (CED): 2026-11-12  
CED codes: 1.1.A, 1.1.A.1, 1.1.A.2, 1.1.B, 1.1.B.1, 1.1.B.2, 1.1.C, 1.1.C.1, 1.1.C.2, 1.1.C.3, 1.1.C.4

An algorithm is a step-by-step process for completing a task or solving a problem, and sequencing means its steps run in a set order, one at a time. A program is an algorithm written in Java: the compiler checks it for some errors before it can run. Programs can have syntax errors, logic errors and run-time errors such as exceptions.

## What you will learn

- 1.1.A Represent patterns and algorithms found in everyday life using written language or diagrams.
- 1.1.B Explain the code compilation and execution process.
- 1.1.C Identify types of programming errors.

## 1\. Algorithms and sequencing

An **algorithm** is a step-by-step process for completing a task or solving a problem. You can represent an algorithm in written language (plain words) or as a diagram, for example a flowchart 1.1.A.1.

Here is an everyday algorithm in written language:

1.  Boil water.
2.  Put a tea bag in a cup.
3.  Pour in the water.
4.  Wait 3 minutes.
5.  Remove the bag.

As a flowchart, each step is a box, and arrows join the boxes in this order: boil water, then tea bag in cup, then pour, then wait, then remove the bag.

**Sequencing** defines the order in which the steps happen. Steps are completed one at a time 1.1.A.2. Change the order and you may change the result: swap steps 1 and 3 and you get cold, wet tea leaves.

A Java program is an algorithm too. The statements in `main` run in sequence, top to bottom, one at a time:

```
int score1 = 80;
int score2 = 90;
int score3 = 85;
int total = score1 + score2 + score3;
System.out.println("Total: " + total);
System.out.println("Average: " + total / 3);
```

Output:

```
Total: 255
Average: 85
```

**Watch out:** the total must be computed before the average. If the average line came first, `total` would not exist yet and the code would not compile.

## 2\. Writing, compiling and running code

Code can be written in any text editor. However, programmers often use an **integrated development environment (IDE)**, because it gives tools to write, compile and run code in one place 1.1.B.1. Without an IDE, you run the compiler and then the program yourself.

A **compiler** checks your code for some errors. Errors the compiler can detect must be fixed before the program can run 1.1.B.2.

The path from code to a working program has four stages. Each type of error shows up at a different stage:

| Stage | What happens | Error that shows up here |
| --- | --- | --- |
| 1\. Write code | You type the program in a text editor or IDE. |  |
| 2\. Compile | The compiler checks some errors. | Syntax error: rules broken; fix it, then compile again. |
| 3\. Run | The statements execute. | Run-time error: for example an exception; the program stops abnormally. |
| 4\. Test | You try known inputs and compare with the expected outcome. | Logic error: the program runs, but gives a wrong result. |

Here the programmer forgot a semicolon:

```
// does not compile
public class Lives
{
    public static void main(String[] args)
    {
        int lives = 3
        System.out.println(lives);
    }
}
```

Real view: the compiler stops and reports the file, the line number (6) and the problem. Nothing runs.

```
Lives.java:6: error: ';' expected
        int lives = 3
                     ^
1 error
```

## 3\. The three types of programming error

| Error | What it is | Who or what finds it | Example |
| --- | --- | --- | --- |
| Syntax error | The rules of Java are not followed 1.1.C.1. | The compiler | A missing `;` or `)` |
| Logic error | The algorithm or program is wrong, so it behaves incorrectly or unexpectedly 1.1.C.2. | Testing with specific data and comparing to the expected outcome | Area computed as `length + width` |
| Run-time error | Happens while the program runs (during execution); usually makes it stop abnormally 1.1.C.3. | Shows up during execution | Dividing an `int` by `0` |

A logic error compiles and runs without complaint. This program should print the area of a 5 by 3 rectangle, which is 15:

```
int length = 5;
int width = 3;
int area = length + width;
System.out.println("Area: " + area);
```

Output:

```
Area: 8
```

The output is wrong because the formula is wrong. Testing with data where you already know the answer (5 and 3 give 15) reveals the error. The fix is `length * width`.

**Watch out:** “it compiles” does not mean “it is correct”.

## 4\. Exceptions

An **exception** is a type of run-time error. It happens because of an unexpected problem that the compiler did not detect, and it interrupts the normal flow of the program’s execution 1.1.C.4. In this course, an exception stops the program, so the statements after it do not run.

```
int total = 12;
int count = 0;
System.out.println("Start");
System.out.println(total / count);
System.out.println("End");
```

Output:

```
Start
Exception in thread "main" java.lang.ArithmeticException: / by zero
```

The code compiles, because the compiler does not know that `count` will be 0. The program prints `Start`, then stops at the division. `End` is never printed.

Unit 1 names three exceptions:

-   `ArithmeticException`: an integer divided by the integer zero (topic 1.3).
-   `NullPointerException`: a method called on `null` (topic 1.14).
-   `StringIndexOutOfBoundsException`: an index outside a `String` (topic 1.15).

> **Background (not tested): what the compiler actually makes.** The Java compiler turns your `.java` file into bytecode (a `.class` file). The Java Virtual Machine (JVM) then runs the bytecode, which is why the same Java program runs on different operating systems. Every program starts running at its main method: `public static void main(String[] args)`. Programmers use these words often; the exam does not test them.

## 5\. Unit 1 on the AP® exam

-   The College Board weights Unit 1 at 15 to 25 percent of the multiple-choice section. That section has 42 questions in 90 minutes and is 55 percent of the exam score.
-   Unit 1 skills also appear in the free-response section (4 questions, 90 minutes, 45 percent). Question 1, Methods and Control Structures, has a part (3 points) that requires calling `String` methods, and the sample free-response Questions 1, 3 and 4 in the official Course and Exam Description (CED) all use Unit 1 objectives.
-   The unit develops 9 skills: 1.A (design), 2.A, 2.B, 2.C (develop code), 3.A, 3.C, 3.D (analyze code) and 4.A, 4.B (document code).
-   Topic 1.15 (String Manipulation) is the largest topic in the unit.
-   You get the Java Quick Reference on the exam. Learn the `Math` and `String` methods on it now (topics 1.11 and 1.15).
-   Tags like 1.3.C on these pages point to the College Board learning objective each note covers. Background boxes help you understand a topic; their wording is not tested.

**Official practice:** do the CED sample multiple-choice Questions 1 to 4 (Exam Information section of the AP® Computer Science A CED, effective Fall 2025; Questions 1 and 2 are on page 149, Questions 3 and 4 on page 150, answer key on page 170). Questions 5 to 20 test later units. Sample free-response Question 1, Methods and Control Structures (pages 161 to 163): part (b) uses Unit 1 `String` methods, and part (a) needs Unit 2 loops, so try the full question after Unit 2. The same goes for the 2026 Free-Response Question 1 (released exam, pages 4 to 6) on the AP® Central past exam questions page. Score yourself with the College Board’s own scoring guidelines.

> **Background (not tested): words you will hear.** Programmers use these words every day. They are useful, but they are not the official course wording, so on the exam look for the course term.
> 
> | You may hear | Course term or meaning |
> | --- | --- |
> | static method | class method (1.10) |
> | field, property | attribute (1.7); Unit 3 adds the term instance variable |
> | pass by value | call by value (1.9) |
> | actual parameter / formal parameter | argument / parameter (1.9) |
> | build, make an object | instantiation: create an object with `new` (1.13) |
> | type casting, type conversion | casting (1.5) |
> | bytecode, JVM, main method | not in the course framework; see section 4 above |
> | operand; unary / binary operator | the value an operator works on; an operator with one / two operands (1.3) |
> | crash; bug | run-time error; bug means any error, often a logic error (1.1) |
> | string joining | concatenation (1.15) |
> | object address, pointer | object reference (1.12) |

## Common mix-ups

- Not true: If the code compiles, the program is correct. The compiler only finds some errors. A logic error compiles and runs but gives a wrong result. Only testing with specific data reveals it.
- Not true: You need an IDE to write Java. Code can be written in any text editor. An integrated development environment (IDE) is popular because it puts the tools to write, compile and run code in one place.
- Not true: A program with a syntax error starts running and stops at the bad line. A program with a syntax error never runs. The compiler reports the error, and you must fix it before the program can run.
- Not true: The order of the steps does not matter as long as all the steps are there. Sequencing matters. Steps run one at a time in a set order, and changing the order can change the result.

## In 30 seconds

- An algorithm is a step-by-step process; you can write it in plain words or draw it as a diagram such as a flowchart.
- Sequencing: steps happen in a set order, one at a time.
- Any text editor works for code, but an IDE gives tools to write, compile and run in one place.
- The compiler checks for some errors, and those errors must be fixed before the program can run.
- Syntax errors break the language rules (compiler finds them), logic errors give wrong results (testing finds them), run-time errors happen during execution.
- An exception is a run-time error the compiler did not detect; it interrupts the normal flow of the program.

## Checkpoint: 4 practice questions

### Question 1

**Steps, in no particular order**

| Step | Action |
| --- | --- |
| I | Divide the total by 3. |
| II | Read the three scores. |
| III | Add the three scores. |
| IV | Display the result. |

An algorithm finds the average of three test scores. Its steps are listed in no particular order. In which order should the steps run?

- A. II, I, III, IV
- B. II, III, IV, I
- C. III, II, I, IV
- D. II, III, I, IV

**Answer: D.** Read the scores (II), add them (III), divide the total (I), then display (IV). Sequencing: one step at a time, in order. A divides before any total exists, B displays the result before it has been computed, and C adds scores that have not been read yet.

### Question 2

Which statement about compiling Java code is true?

- A. Errors detected by the compiler have to be corrected before the program will run.
- B. The compiler finds logic errors by comparing the output with the expected outcome.
- C. A program with a syntax error starts running, then stops at the line with the error.
- D. Code must be written in an IDE, because a text editor file cannot be compiled.

**Answer: A.** The compiler finds some errors, and those must be fixed before the program runs. B is wrong because logic errors are found by testing, not by the compiler. C is wrong because a program with a syntax error never starts. D is wrong because any text editor works; an integrated development environment (IDE) only puts the tools in one place.

### Question 3

**Three programs with a problem**

| Program | Problem |
| --- | --- |
| I | A program prints the area of a rectangle as length + width. It runs without stopping. |
| II | A statement is missing its closing parenthesis. |
| III | A program stops abnormally when it divides an int total by an int count of 0. |

Three programs have a problem. Which correctly classifies the errors?

- A. I syntax, II logic, III run-time
- B. I logic, II syntax, III run-time
- C. I logic, II run-time, III syntax
- D. I run-time, II syntax, III logic

**Answer: B.** Wrong formula that runs: logic. Missing parenthesis: syntax (the compiler finds it). Stops while running: run-time. A calls the wrong formula a syntax error, but it compiles. C and D mix up the missing parenthesis (found before running) and the division by zero (found during execution).

### Question 4

Consider the following code segment. Which of the following best describes what happens when the code segment is executed?

```java
int laps = 0;
System.out.println("Race start");
System.out.println(400 / laps);
System.out.println("Race end");
```

- A. Nothing is printed; the compiler reports the division by zero as a syntax error.
- B. `Race start`, `0` and `Race end` are printed, because int division by zero gives 0.
- C. `Race start` is printed, then an `ArithmeticException` stops the program.
- D. `Race start` and `Race end` are printed, and the compiler skips the division line.

**Answer: C.** The code compiles, because the compiler does not know that laps will be 0. When the division runs, an `ArithmeticException` (a run-time error) interrupts the normal flow, so `Race end` is never printed. A is wrong because the compiler does not detect this. B and D are wrong because Java never quietly replaces a division by zero with 0 or skips it.

## Questions students ask

**What is an algorithm in AP® Computer Science A?**

An algorithm is a step-by-step process for completing a task or solving a problem. It can be written in plain language or shown as a diagram, such as a flowchart.

**What is the difference between a syntax error and a logic error?**

A syntax error breaks the rules of Java, so the compiler finds it and the program cannot run. A logic error follows the rules, so the program runs, but the algorithm is wrong and the result is wrong. Testing finds logic errors.

**Is an exception a compile-time error?**

No. An exception is a type of run-time error. It happens while the program runs, because of a problem the compiler did not detect, and it interrupts the normal flow of execution.

**Do I need an IDE for the AP® exam?**

No. On the exam you write and read code on paper or screen without running it. An IDE is a helpful tool for practice because it lets you write, compile and run code in one place.

---

© LockInCS, licensed CC BY-NC-ND 4.0. AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse, this website.
