Lesson 1.1: Introduction to Algorithms, Programming, and Compilers | AP CSA

Unit 1 · Lesson 1.1 · Conceptual

Lesson 1.1: Introduction to Algorithms, Programming, and Compilers

Reading time: 12-15 min · Practice: 6 exercises · Mastery: applied scenario

What you’ll learn in this lesson

  • 1.1.A. Represent everyday patterns and processes as algorithms using written language or diagrams, and explain why sequencing matters.
  • 1.1.B. Explain the role of an IDE, the compilation step, and what kinds of errors the compiler can and cannot catch.
  • 1.1.C. Identify and distinguish syntax errors, logic errors, run-time errors, and exceptions, including which are caught by the compiler and which only show up later.

This lesson is part of the AP CSA Course and Exam Description (effective May 2027).

AP exam weight: Conceptual lessons appear in the MCQ section, expect 1-3 questions per lesson topic on the live exam, often as scenario or definition stems. Lesson 1.1 vocabulary (syntax error, logic error, run-time error, exception) shows up across all four units.

What is an algorithm, and what does a computer do with it?

Every time you follow a recipe, give someone directions to your house, or work through long division, you are running an algorithm. You start with an input, you follow a fixed sequence of steps, and you end up with a result. Computer science takes that same everyday idea and asks a tighter question: what is the precise, unambiguous list of steps that solves this problem, and how do we hand those steps to a machine that has zero common sense?

Definition

An algorithm is a step-by-step process for completing a task or solving a problem. Algorithms can be represented in written language, in diagrams (like flowcharts), or in a programming language. Sequencing is the part of an algorithm that defines the order in which steps are performed, one step at a time.

AP CSA is, at its core, a course about writing algorithms in Java. Before any code is involved, though, the algorithm has to exist as a clear, ordered plan. The CED is explicit about this: students should be able to represent patterns and algorithms found in everyday life, not just type Java. The reason is practical. Programmers who can’t describe their plan in plain English usually can’t describe it in Java either. They end up debugging a mental model that was never coherent to begin with.

Sequencing: one step at a time, in a specific order

Sequencing sounds obvious until you try to break it. Consider making a peanut butter sandwich. “Spread the peanut butter, take out the bread, open the jar” lists all the right ingredients but produces a mess. The same is true in code. System.out.println(name); String name = "Tanner"; uses the right pieces but compiles only if reordered. Computers execute statements one at a time, in the order written, with no ability to look ahead or guess what you meant.

Example

Algorithm for “log in to a website”: (1) open the login page, (2) type the username, (3) type the password, (4) click submit, (5) wait for the server response, (6) display the home page only if the response was successful. Swap steps 3 and 4 and the password is never entered. Swap steps 4 and 5 and the user clicks submit before there’s anything to submit. The steps haven’t changed; only the sequence has, and the sequence is what makes it work.

From algorithm to running program: writing, compiling, executing

Once you have an algorithm, you write it in a programming language. Java code can be written in any text editor, but most programmers use an integrated development environment (IDE), a single application that lets you write the code, compile it, and run it without leaving the window. Common AP CSA classroom IDEs include IntelliJ IDEA, Eclipse, BlueJ, and Replit. The CED treats the choice of IDE as classroom-dependent and not tested directly, what is tested is what the IDE does for you.

The key step is compilation. A compiler is a program that translates your Java source code into a form the machine can run, and along the way it checks the code for certain kinds of errors. If the compiler finds an error, your program does not run at all. You must fix the error first. This is one of the most important ideas in this lesson: the compiler is a gatekeeper for one class of mistakes, but it is blind to others.

Common misconception

“If my code compiles, it works.” This is wrong, and the AP exam tests it directly. The compiler only catches errors related to the rules of the Java language (missing semicolons, undeclared variables, type mismatches). A program can compile cleanly, run end-to-end, and still produce the wrong answer because the logic, the algorithm you wrote, is flawed. Successful compilation means “Java accepts this as valid Java,” not “this solves your problem.”

The four error types you must distinguish

The CED is unusually specific here. Topic 1.1.C names four error categories, and the AP exam tests whether you can place a given mistake into the right bucket. The distinctions matter because they tell you when an error will surface: at compile time, at run time, or only when you happen to test with the right input.

  • A syntax error is a violation of the rules of the Java language. Missing semicolon, misspelled keyword, unclosed brace. The compiler catches every syntax error. Your program does not run.
  • A logic error is a mistake in the algorithm itself. The code compiles and runs but produces wrong output. You only catch logic errors by testing with specific inputs and comparing actual output to expected output.
  • A run-time error is a problem that occurs during execution. The compiler accepted the code, but something goes wrong while the program is running, usually causing it to terminate abnormally.
  • An exception is a specific type of run-time error: an unexpected condition that the compiler couldn’t predict, which interrupts the normal flow of execution. ArithmeticException from dividing by zero and NullPointerException from calling a method on null are the two you’ll see most often in Unit 1.

Key insight

The compiler catches syntax errors only. Logic, run-time, and exception errors all slip past the compiler. They require running the program (and often testing with specific inputs) to surface. This is why writing test cases is part of programming, not an optional extra.

Key vocabulary

Term Definition
Algorithm A step-by-step process for completing a task or solving a problem, representable in written language, diagrams, or code.
Sequencing The order in which steps in a process are completed; steps run one at a time.
IDE Integrated Development Environment, an application that bundles tools to write, compile, and run code in one place.
Compiler A program that translates source code into a runnable form and checks for certain language-rule errors before the program runs.
Syntax error A mistake that violates the rules of the programming language; detected by the compiler. The program does not run.
Logic error A mistake in the algorithm that causes incorrect or unexpected behavior; detected only by testing with specific inputs.
Run-time error A mistake that occurs during program execution, typically causing the program to terminate abnormally.
Exception A type of run-time error caused by an unexpected condition the compiler did not detect; interrupts the normal flow of execution.
▶ Java Code Editor
Try It Yourself
Write real Java, hit Run, and your code executes on a live compiler. Output is checked automatically.

Tier 2 Practice Check your understanding

Six questions covering the lesson concepts. On tougher questions, try predicting your answer before reading the options, it’s the best way to catch trap distractors. Mix of formats: MCQ, matching, classification, and a vocabulary cloze.

MCQ · Multi-select stem
1. A student is writing an algorithm to make oatmeal. Consider the following statements about algorithms and sequencing.
  1. An algorithm can be represented in written language, in a diagram, or in code, and the representation does not change whether the algorithm is correct.
  2. Reordering two steps in an algorithm always produces a different result.
  3. An algorithm only counts as “an algorithm” once it is written in a programming language; a recipe on paper does not qualify.
Which of the statements are true?
A. I only
B. I and II
C. I only is fully correct; II and III are false
D. II and III
C. Only statement I is true: the CED explicitly says algorithms can be represented in written language, diagrams, or code, and correctness is independent of representation. Statement II overreaches, reordering two steps can change the result, but if the two steps are independent (e.g., “put on left sock” then “put on right sock”) the result is identical. Statement III is the most common student mistake: a paper recipe and a flowchart are perfectly valid algorithm representations, nothing about the definition requires code.
MCQ · Spot the misconception
2. Which of the following statements about compilers and errors is NOT true?
This stem uses NOT. You’re looking for the option that is wrong. Before reading the choices, predict what a compiler can and cannot catch. Then read all four.
A. A program that fails to compile will not run at all until the errors are fixed.
B. Syntax errors are violations of the rules of the programming language and are detected by the compiler.
C. A program can compile successfully and still produce incorrect output for some inputs.
D. If a program compiles successfully, then it will never throw a run-time exception during execution.
D. Option D is the false statement, successful compilation means only that the code follows Java’s language rules, not that it is safe at run time. A compiled program can still throw exceptions like ArithmeticException (division by zero) or NullPointerException the moment it tries an operation the compiler couldn’t predict. A, B, and C are all accurate: compile failures block execution, syntax errors are language-rule violations the compiler catches, and clean compilation says nothing about logical correctness.
Matching
3. Match each error type on the left to its precise definition on the right.
Syntax error
Logic error
Run-time error
Exception
An error that occurs during program execution, typically causing the program to terminate abnormally.
A violation of the rules of the programming language; detected by the compiler before the program runs.
A type of run-time error from an unexpected condition the compiler did not detect, interrupting normal flow.
A mistake in the algorithm itself; the program runs but produces incorrect or unexpected results.
Syntax errors live entirely at compile time. Logic errors run cleanly but give wrong answers (think: off-by-one bug in a loop). Run-time errors halt execution mid-program. Exceptions are the specific subset of run-time errors that occur because of unexpected conditions like dividing by zero, every exception is a run-time error, but not every run-time error is an exception.
Classify
4. For each scenario below, classify the error as a syntax error, a logic error, or a run-time error.
A student forgets the semicolon at the end of int x = 5 and the program will not compile.
A program that computes the average of three test scores returns the sum instead because the student forgot to divide by 3.
A program crashes with ArithmeticException when the user enters 0 as the divisor.
A student types Sytem.out.println with a misspelled class name and the IDE underlines it in red before running.
A loop intended to print numbers 1 through 10 prints 0 through 9 because the student started the counter at the wrong value.
A program that reads a file crashes partway through with NullPointerException the first time it encounters an empty line.
Syntax error
Logic error
Run-time error
The two syntax errors (missing semicolon, misspelled System) are caught by the compiler, so the program never runs. The two logic errors compile and run but produce wrong output: in both cases Java did exactly what was written, which wasn’t what the student meant. The two run-time errors (division by zero, null pointer) compile cleanly and execute until they hit a condition the compiler couldn’t predict.
Cloze · Vocabulary in context
5. Fill each blank with the correct term from the bank below.
A student writes a Java program in their , saves the file, and clicks Run. The reports no errors, so the program starts executing. After thirty seconds it terminates abnormally with a stack trace; this is a . The student fixes the crash, runs the program again, and this time it finishes cleanly but produces the wrong answer for several inputs. This second problem is a .
IDE compiler run-time error logic error syntax error algorithm
The IDE is where code is written and managed. The compiler is the gatekeeper before execution; if it reports no errors, syntax is not the problem. A program that compiles but terminates abnormally during execution is hitting a run-time error. A program that compiles, runs to completion, and produces wrong output has a logic error. The algorithm itself is flawed.
MCQ · Applied scenario
6. A student writes a Java program that calculates a tip on a restaurant bill. The program compiles without any errors and runs to completion every time, but the tip amount is consistently 10 times larger than it should be. Which of the following best classifies this mistake?
A. A logic error, because the program runs without crashing but produces an incorrect result.
B. A syntax error, because the program is using the wrong math operator and the compiler should have caught it.
C. A run-time error, because the wrong value appears during execution rather than at compile time.
D. An exception, because the output deviates from what was expected.
A. The defining feature here is that the program compiles, runs to completion, and produces incorrect output, and that is the textbook signature of a logic error. B is wrong because the compiler does not check whether math is “correct” for your purposes; multiplying by 10 instead of 0.1 is valid Java. C is the most attractive trap: a run-time error specifically means execution is interrupted (the program terminates abnormally), which is not what happened. D is wrong because an exception is a specific kind of run-time error, not a generic label for any incorrect output.
Tier 3 · AP Mastery Challenge

The After-School Tutoring Scheduler

Mr. Ramirez asks two of his students, Sara and Jordan, to write a Java program that prints the names of every student signed up for after-school tutoring on a given day. The program reads names from a text file (one name per line) and prints them in the order they appear. Sara writes the program in IntelliJ, clicks Run, and IntelliJ refuses to start the program; it underlines the second-to-last line in red. Sara fixes the underline and clicks Run again; this time the program starts, prints six names, and then terminates abnormally with a message about NullPointerException. Jordan looks at the same file and notices that the file has an empty line in the middle, which is where Sara’s program crashed. Jordan rewrites the program so it skips empty lines. It now runs to completion. But Mr. Ramirez points out that one of the printed names is wrong: the program is printing every name twice.

Part A: Identify Sara’s first error

When IntelliJ refused to start Sara’s program and underlined a line in red, what kind of error was Sara hitting?
Predict first: what is the only error type that prevents a program from running at all? Then read the options.
A. A logic error, because the program is producing the wrong result.
B. A syntax error, because the compiler refused to compile the code and flagged a specific line.
C. A run-time error, because the IDE detected it before execution.
D. An exception, because the IDE handled it before the program ran.
B. The defining feature is that the program never ran. The IDE underlined a line in red and refused to compile, which is what happens with a syntax error. A is wrong because logic errors only show up after the program runs and produces wrong output. C is wrong because run-time errors happen during execution, not before it starts. D is wrong because exceptions are a specific kind of run-time error, again something that surfaces while the program is executing.

Part B: Identify Sara’s second error

After Sara fixed her first error, the program ran, printed six names, and then crashed with NullPointerException. Which classification best describes this second error?
Predict first: the compiler accepted this code, but execution stopped mid-program. Which category fits?
A. An exception, which is a type of run-time error caused by an unexpected condition the compiler could not detect.
B. A logic error, because the program ran for a while and then stopped.
C. A syntax error, because the compiler should have caught it before execution.
D. A compilation warning, because the IDE permitted the program to start but then halted it.
A. NullPointerException is a specific type of run-time error caused by an unexpected condition (a null value being used where an object was expected) that the compiler could not detect in advance. Every exception is a run-time error. B is wrong because a logic error means wrong output, not a crash. C is wrong because the compiler does not detect this kind of problem; the code is syntactically valid. D is wrong because Java does not have a notion of a “compilation warning” that halts execution mid-program.

Part C: Structured response

In three to five sentences, explain to Mr. Ramirez why Jordan’s final program still has a problem, even though it now compiles and runs to completion without crashing. Identify what type of error this is, and explain how Jordan would have to track it down, since the compiler and the run-time will not flag it for him.

Scoring rubric

Correctly identifies the “every name printed twice” bug as a logic error. Full 1 pt
Explains that the program compiles and runs to completion, so neither the compiler nor the run-time flags it. Full 1 pt
States that the only way to find it is by testing with specific inputs and comparing actual output to expected output. Partial 0.5 pt
Calls the bug a syntax error, run-time error, or exception. Zero 0 pt

Sample full-credit response

Jordan’s program is hitting a logic error. The code compiles, so it is not a syntax error, and the program runs to completion without crashing, so it is not a run-time error or exception. The bug is in the algorithm itself, somewhere in the code Jordan is processing each name twice (perhaps an extra loop or a duplicate print statement). Because neither the compiler nor the Java run-time can know what output Mr. Ramirez expected, no automatic tool will flag this. Jordan has to test the program with a known input file, compare the printed list to the names actually in the file, and trace through his code line by line to find where the duplication happens.

Common AP exam traps

The exam predictably tests these same misconceptions year after year. Recognize them now and you’ll catch them in May.

Trap 1: “If it compiles, it works”

Successful compilation means the code follows Java’s rules, nothing more. A compiled program can still throw exceptions at run time or produce completely wrong output. The compiler is one layer of error detection, not the only layer.

Trap 2: Confusing exception with run-time error

Students often treat “exception” and “run-time error” as synonyms. An exception is a specific kind of run-time error caused by an unexpected condition the compiler did not detect. All exceptions are run-time errors, but not all run-time errors are exceptions.

Trap 3: Logic errors classified as something else

If a program compiles, runs to completion, and produces wrong output, it is still a logic error, even when the wrong output seems “weird” or “unexpected.” The exam will phrase the scenario in ways that tempt you to call it an exception. Don’t.

Trap 4: “An algorithm has to be in code”

The CED explicitly lists written language and diagrams as valid algorithm representations. A flowchart, a numbered list of steps, and a Java method are all algorithms; the form of representation has nothing to do with whether the algorithm is correct.

Tier 4 · Beyond AP (Optional)

How real compilers find more than just syntax errors

Modern compilers and IDEs go well beyond the bare minimum the AP exam describes. Java’s compiler also performs type checking (catching mismatches like assigning a String to an int) and definite assignment analysis (refusing to compile if a local variable might be used before it’s assigned). IntelliJ and Eclipse add static analysis on top of that, flagging suspicious patterns, unused variables, methods that always return the same value, possible null dereferences, before you even hit Run. Some of these warnings catch genuine logic errors before they ever execute. None of this is on the AP exam, but it’s worth knowing that the line between “compile-time” and “run-time” is blurrier in professional tools than the CED’s clean four-category framework suggests.

This material extends beyond the AP CSA curriculum. Skip it if you’re focused on exam prep.

Deep dive: more on this topic

Get the AP CSA daily practice question by email

Free, daily, takes 90 seconds. Built by an AP CSA teacher with a 54.5% 5-rate.

Start daily practice →

Get in Touch

Whether you're a student, parent, or teacher — I'd love to hear from you.

Just want free AP CS resources?

Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]