What is a program, how does Java run it, and why does the order of statements matter?
Topic 1.1 Why Programming? Why Java?: explain how a program is a sequence of statements executed in order, how Java source is compiled and run, and how to display output and handle simple runtime behavior.
A focused answer to AP CSA Topic 1.1, covering what a program is, the structure of a Java class with a main method, sequential execution, System.out output, and compile-time versus run-time errors, with a fully traced worked program.
Reviewed by: AI editorial process; not yet individually human-reviewed
Have a quick question? Jump to the Q&A page
Jump to a section
What this topic is asking
The College Board (Topic 1.1) wants you to know that a program is an ordered sequence of statements that the computer carries out one at a time, that Java programs live inside a class with a main method, that you display output with System.out.print and System.out.println, and that mistakes show up either when the program is compiled or while it runs. This is the foundation every later topic assumes.
A program is a sequence of statements
Because execution is sequential, reordering two statements can change the output. The computer does exactly what each line says, in order, with no memory of intent. This is why tracing a program - stepping through each statement and noting what happens - is the single most useful skill in AP CSA.
The structure of a Java program
Every Java program is written inside a class, and the program starts running in the main method. The standard skeleton is:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
public class HelloWorlddeclares a class namedHelloWorld. The file is usually namedHelloWorld.java.public static void main(String[] args)is the method where execution begins. You will memorize this header exactly.- Statements inside the method end with a semicolon (
;). - Braces
{ }group statements into blocks. Every opening brace needs a matching closing brace.
Displaying output
So the segment below prints score = 7 on one line:
System.out.print("score = ");
System.out.println(7);
Compile-time and run-time
Java is a compiled language: the compiler translates your source code into bytecode before the program runs. This creates two distinct moments where things can go wrong.
A compile-time error is caught before the program runs at all. The compiler refuses to produce a runnable program. Typical causes are a missing semicolon, a misspelled keyword, an unmatched brace, or using a variable that was never declared. Nothing executes, so there is no output - just a compiler message pointing at the line.
A run-time error happens while the program is executing, after it compiled successfully. The program starts, runs some statements, and then hits something it cannot do, such as dividing an integer by zero or accessing a position that does not exist. Java responds by throwing an exception, which stops the program and prints a stack trace unless the exception is handled. The key difference for the exam: compile-time errors mean nothing ran; run-time errors mean it ran until the bad statement.
There is also a third, sneakier category the AP exam loves: a logic error. The program compiles and runs with no error message, but produces the wrong answer because the instructions, though valid, do not express what you intended. Logic errors are found by tracing and testing, not by the compiler.
Try this
Q1. State where execution of a Java program begins. [1 point]
- Cue. In the
mainmethod, specificallypublic static void main(String[] args).
Q2. A program compiles but prints the wrong total. Identify the category of error and how you would find it. [2 points]
- Cue. A logic error. The compiler cannot catch it because the code is valid; you find it by tracing the code and testing with known inputs.
Exam-style practice questions
Practice questions written in the style of College Board exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.
AP 2022 (style)1 marksMultiple choice. Consider the following code segment.
```java
System.out.print("AP");
System.out.println("CSA");
System.out.print("Java");
```
What is printed when the segment runs?
(A) `AP CSA Java`
(B) `APCSA` then `Java` on a new line
(C) `AP` `CSA` `Java` each on its own line
(D) `APCSAJava` all on one line
(E) Nothing, because the code does not compile
Show worked answer →
The answer is (B).
print writes text without moving to a new line, so System.out.print("AP") and System.out.println("CSA") produce APCSA. The println then advances to a new line, so the final System.out.print("Java") writes Java on the next line. There are no spaces because none were written. The code compiles, so (E) is wrong; (A) invents spaces; (C) invents line breaks between every token; (D) ignores the line break created by println.
Markers reward knowing that print stays on the line while println ends it.
AP 2019 (style)4 marksFree response (code writing). Write a complete Java class named `Greeting` with a `main` method that prints exactly the following three lines:
```
Welcome
to
AP CSA
```
Your class must compile and run.
Show worked answer →
A 4-point code-writing question testing class structure and output.
public class Greeting {
public static void main(String[] args) {
System.out.println("Welcome");
System.out.println("to");
System.out.println("AP CSA");
}
}
Point 1: a correct class header public class Greeting. Point 2: a correct main header public static void main(String[] args). Point 3: three println calls producing the three required lines. Point 4: exact strings, including the space in AP CSA and correct capitalization. Using print instead of println would put the words on one line and lose marks. The braces must match.
Related dot points
- Topic 1.2 Variables and Data Types: identify the primitive types int, double and boolean, and declare, initialise and use variables of those types.
A focused answer to AP CSA Topic 1.2, covering the difference between primitive and reference types, the three exam primitives int, double and boolean, declaring and initialising variables, valid identifiers, and constants, with a traced worked example.
- Topic 1.3 Expressions and Assignment Statements: evaluate arithmetic expressions using operator precedence, integer division and the modulo operator, and assign results to variables.
A focused answer to AP CSA Topic 1.3, covering arithmetic operators, operator precedence, integer division truncation, the modulo operator, and how assignment statements store results, with a fully traced worked evaluation.
- Topic 2.1 Objects: Instances of Classes: explain the relationship between a class and its objects, and describe an object as an instance of a class with state and behavior.
A focused answer to AP CSA Topic 2.1, covering the class-object relationship, what it means for an object to be an instance, the difference between attributes (state) and methods (behavior), and reference versus primitive variables, with a worked example.
- Topic 2.2 Creating and Storing Objects (Instantiation): use the new keyword to call a constructor and instantiate an object, choosing the correct constructor by its parameters, and store the result in a reference variable.
A focused answer to AP CSA Topic 2.2, covering the new keyword, constructors and how they initialise an object, choosing a constructor by its parameter list, matching argument types, storing the reference, and the meaning of null, with a worked example.
- Topic 2.3 Calling a Void Method: call a non-static void method on an object using dot notation, and explain that a void method performs an action but returns no value.
A focused answer to AP CSA Topic 2.3, covering dot notation for calling methods on objects, what void means, why a void call cannot be used in an expression, the difference between a method signature and a call, and method side effects, with a worked example.
Sources & how we know this
- AP Computer Science A Course and Exam Description — College Board (2025)