Short on time? This page is the recap. For explanations and worked examples, use the full study guide.
Recap
- An algorithm is a step-by-step process for completing a task or solving a problem. It can be written in everyday language or drawn as a diagram.
- Sequencing defines the order in which steps happen. Steps are completed one at a time, and changing the order can change the outcome.
- An integrated development environment (IDE) provides tools to write, compile and run code. Code can also be written in any text editor.
- The compiler checks code for some errors. Errors it detects must be fixed before the program can run.
- Every program in this course is written in Java, begins at
main, and (in Unit 1) runs its statements from top to bottom, with detours into methods.
Key relationships
| Idea | What to remember |
|---|---|
| Order of work | write, then compile, then run |
| Syntax error | breaks the rules of the language; detected by the compiler |
| Logic error | compiles and runs but behaves incorrectly or unexpectedly; detected by testing with specific data |
| Run-time error | occurs during execution; the program usually terminates abnormally |
| Exception | a run-time error from an unexpected problem the compiler did not detect; it interrupts normal flow |
| Integer division by zero | ArithmeticException at run time (for example 12 / 0) |
| Trace table | one row per statement; one column per variable; shows each value after each statement |
Assumptions and conventions
- Examples assume a class with a
mainmethod, as inpublic static void main(String[] args). - A compile error means the program does not run at all. A run-time error means it starts and then fails; any output printed before the failure still appears.
- “Test with specific data” means choosing inputs whose correct output you can work out independently.
- No calculator is allowed on the exam, so work every trace by hand.
Mistakes to avoid
- Saying “it compiled, so it is right”. Compiling does not prove the algorithm is correct.
- Confusing the three error kinds. Ask: could it be compiled? Did it stop while running? Did it finish with a wrong answer?
- Reordering statements carelessly. Moving a line changes the meaning, especially when two steps use the same variable.
- Calling every error message a syntax error. An exception message at run time is a run-time error.
- Using test data that hides a bug. Zero, or equal values, can make a faulty formula give the right answer by luck.
- Describing an algorithm as “the code”. An algorithm is the plan; the code is one way to write it.
Quick self-check
- Is a missing semicolon a syntax, logic or run-time error, and which tool finds it? (Syntax error; the compiler.)
- A program prints
Average: 120for the scores 70, 80 and 90 and does not crash. What kind of error is this? (A logic error; the correct average is 80, found by testing.) int n = 0; System.out.println(50 / n);compiles. What happens when it runs? (AnArithmeticExceptionstops the program: a run-time error.)- Trace:
int a = 2; int b = 5; a = b; b = a;What areaandb? (Both are 5, becauseawas overwritten before its old value was saved.)
Next: practice questions.