UNIT 1: USING OBJECTS AND METHODS · TOPIC 1.1
1.1 Introduction to Algorithms, Programming, and Compilers
The vocabulary you need before the first line of Java: what a compiler does, what a syntax error is, and why some errors only appear when the program runs.
What you need to know
- An algorithm is a finite set of instructions that accomplishes a task. A program expresses an algorithm in a programming language so a computer can run it.
- Java source code is written in
.javafiles and compiled into bytecode. The compiler translates and checks the code before it can run. - A syntax error (compile-time error) is a violation of Java's grammar — the compiler refuses to produce a program. A run-time error (exception) happens while the program executes, such as dividing an int by zero. A logic error compiles and runs but produces the wrong result.
- Sequence, selection, and iteration are the three control structures every algorithm is built from.
- A Java program's entry point is
public static void main(String[] args). Statements end with semicolons; blocks are enclosed in braces{ }. - Comments (
//single line,/* */block) are ignored by the compiler and exist for people.
Worked example
public class Hello
{
public static void main(String[] args)
{
System.out.println("Hello, AP CSA");
int x = 10 / 0; // compiles fine, then ArithmeticException at run time
}
}
The missing-semicolon version of line 5 would be a compile-time error — nothing runs. As written, it compiles, prints the greeting, then throws a run-time exception on line 6. The exam wants you to classify each.
Exam tip: If a question shows code and asks "what happens," check syntax first (missing semicolon, mismatched braces, undeclared variable = won't compile), then look for run-time traps (division by zero, null, bad index), then trace for logic.
Going deeper
The nuance, edge cases, and connections that turn a 3 into a 5.
- Java is compiled to bytecode, then run by the Java Virtual Machine. That two-stage design is why the same
.classfile runs on any operating system — and why the exam separates compile-time from run-time. - The compiler checks syntax (grammar) and types (is that an int where an int is expected?). It cannot check logic. A program that compiles cleanly can still be completely wrong.
- Exceptions are Java's run-time errors. The ones the exam names:
ArithmeticException(int division by zero),ArrayIndexOutOfBoundsException,IndexOutOfBoundsException(ArrayList),NullPointerException,StringIndexOutOfBoundsException,ConcurrentModificationException. You won't write exception handling; you'll identify which one a code segment throws. - The main method header is exact:
public static void main(String[] args). You don't need to explain each word yet, but you should recognize it as the entry point. - Statements end with
;. Blocks use{ }. Indentation is for humans only — Java ignores it. Case matters:Countandcountare different variables. - An algorithm exists independent of language. The same algorithm in AP CSP pseudocode and in Java is the same algorithm. The exam sometimes describes an algorithm in English and asks which Java implements it.
Mistakes that cost points
- Saying a program with a syntax error "produces wrong output." It produces no output — it never runs. Wrong output means it compiled and ran.
- Saying a crash is a logic error. A crash (exception) is a run-time error. Logic errors finish silently with the wrong answer.
- Expecting the compiler to catch division by zero. It doesn't; the values aren't known until run time.
Practice questions
Written in the style of the real exam. Try each one before revealing the answer.
Q1 Which of the following is a compile-time error in Java?
Show answer
Answer: C. A missing semicolon violates syntax, so the compiler rejects it. The other three compile and fail at run time.
Q2 A program compiles and runs to completion but prints an incorrect total because the loop stops one iteration early. This is best described as which type of error?
Show answer
Answer: C. Runs fine, wrong answer — that's a logic error.
Key vocabulary
- Compiler
- translates Java source code into a form the computer can run, reporting syntax errors
- Compile-time error
- an error the compiler detects; the program cannot run
- Run-time error
- an error that occurs during execution, such as an exception
- Logic error
- code that runs but produces incorrect results