Skip to main content
United StatesComputer ScienceSyllabus dot point

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.

Generated by Claude Opus 4.89 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this topic is asking
  2. A program is a sequence of statements
  3. The structure of a Java program
  4. Displaying output
  5. Compile-time and run-time
  6. Try this

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 HelloWorld declares a class named HelloWorld. The file is usually named HelloWorld.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 main method, specifically public 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

Sources & how we know this