Skip to content
Study Hall
← Unit 2: Programming Basics

ICS3U · Unit 2 test review

Programming Basics: Test Review

Everything for the Unit 2 test on one page: the key facts from all 3 lessons, the mistakes that cost the most marks, and a practice test that points you back to the lessons you need.

Key facts

Your First Java Program

  • Every Java program lives in a class, and execution starts at public static void main(String[] args).
  • The file name must exactly match the public class name, including capitals: public class Hello goes in Hello.java.
  • Braces { } group statements into blocks; a semicolon ; ends one statement. Braces take no semicolon.
  • javac Hello.java compiles to Hello.class; java Hello runs it — no file extension on the run command.
  • println prints then moves to a new line; print prints and leaves the cursor in place.
  • Three error types: syntax (caught at compile time), runtime (crashes mid-run), logic (runs fine, answer is wrong).

Variables, Primitive Types and Casting

  • The eight primitives are byte, short, int, long, float, double, char, boolean. String is a class, not a primitive.
  • int holds up to ; going past it wraps silently to the most negative int.
  • If both operands of / are integers, Java truncates: 5 / 2 is 2. Make one side a double to get 2.5.
  • % gives the remainder: 17 % 5 is 2, and n % 2 == 0 tests for even.
  • Widening (byte → short → int → long → float → double) is automatic; narrowing needs a cast such as (int).
  • A cast truncates toward zero — (int) 9.99 is 9 — and final constants are written in UPPER_SNAKE_CASE.

Operators, Expressions and Input

  • Precedence: brackets, then unary, then * / % left to right, then + - left to right, then assignment.
  • % sits at the same level as * and /, so 10 % 4 * 2 is (10 % 4) * 2 = 4.
  • If either operand of + is a String, + joins: "total: " + 1 + 2 gives total: 12, so bracket the arithmetic.
  • x += 3 means x = x + 3; x++ uses the old value then increments, ++x increments then uses the new one.
  • Math.round returns a long; to round to two decimals use Math.round(v * 100) / 100.0 with a double divisor.
  • After nextInt() or nextDouble(), call a bare nextLine() before reading a line of text.

Where marks get lost

  • Saving a file under a name that does not match the public class name — Java rejects it before it looks at anything else. (Your First Java Program)
  • Typing java Hello.java instead of java Hello when running, or forgetting to compile first so the .class file is stale or missing. (Your First Java Program)
  • Leaving off a semicolon, or adding one after a closing brace. Statements end in ;; blocks do not. (Your First Java Program)
  • Writing System.out.println with a lowercase s or a misspelled printn — Java is case sensitive and will not guess. (Your First Java Program)
  • Assuming that because a program printed something, it worked. Only a test with a known correct answer tells you that. (Your First Java Program)
  • Expecting 5 / 2 to be 2.5. With two ints Java truncates to 2 — and never warns you, because nothing illegal happened. (Variables, Primitive Types and Casting)
  • Casting too late: (double) (a / b) does the integer division first, so the decimal is already gone by the time the cast runs. (Variables, Primitive Types and Casting)
  • Assuming a cast rounds. (int) 9.99 is 9, not 10. Use Math.round when you want rounding. (Variables, Primitive Types and Casting)
  • Storing a value that overflows an int — anything past 2 147 483 647 wraps to a negative number silently. Use long. (Variables, Primitive Types and Casting)
  • Using == to compare Strings because String looks like a primitive. It is a class, so .equals() is the correct tool. (Variables, Primitive Types and Casting)
  • Expecting "total: " + 1 + 2 to print total: 3. Evaluation is left to right, so it prints total: 12 unless you bracket the sum. (Operators, Expressions and Input)
  • Forgetting the bare nextLine() after nextInt(), so the next text prompt is skipped and an empty String is stored. (Operators, Expressions and Input)
  • Writing Math.round(v * 100) / 100 with an int divisor, which truncates and throws away the decimals you just rounded to. (Operators, Expressions and Input)
  • Assuming % binds more loosely than *. They share a level and run left to right, so 10 % 4 * 2 is 4, not 10 % 8. (Operators, Expressions and Input)
  • Forgetting import java.util.Scanner; above the class, or building a new Scanner every time input is read instead of reusing one. (Operators, Expressions and Input)

Practice test

18 questions from across the unit. Each one shows the lesson it comes from.

  1. Your First Java Program
    1
    Which of these is the correct signature for the method where a Java program starts?
  2. Your First Java Program
    2
    What appears on screen after System.out.print("Ja"); System.out.print("va"); runs?
  3. Your First Java Program
    3
    A program compiles, runs to the end, and prints an average of 45 when the correct average is 72. This is best classified as:
  4. Your First Java Program
    4
    Put the steps of getting a Java program running into order.
    1. 1Run java ClassName to launch the bytecode
    2. 2A ClassName.class file of bytecode is produced
    3. 3Fix any syntax errors the compiler reports and recompile
    4. 4Write the source code and save it as ClassName.java
    5. 5Run javac ClassName.java to compile it
  5. Your First Java Program
    5
    What file does javac Hello.java produce?
  6. Your First Java Program
    6
    Which of these would produce a runtime error rather than a compile error?
  7. Variables, Primitive Types and Casting
    7
    What value does 7 / 2 produce when both operands are ints?
  8. Variables, Primitive Types and Casting
    8
    What does (double) 7 / 2 evaluate to?
  9. Variables, Primitive Types and Casting
    9
    Which statement correctly declares a constant for Ontario's 13% HST rate?
  10. Variables, Primitive Types and Casting
    10
    What does System.out.println(5 / 2.0); print?
  11. Variables, Primitive Types and Casting
    11
    Put these primitive types in order of widening, from narrowest to widest.
    1. 1float
    2. 2long
    3. 3double
    4. 4byte
    5. 5short
    6. 6int
  12. Variables, Primitive Types and Casting
    12
    Rewrite int avg = total / count; so the average keeps its decimal places in a double named avg.
  13. Operators, Expressions and Input
    13
    What does 3 + 4 * 2 evaluate to?
  14. Operators, Expressions and Input
    14
    What does 10 % 4 * 2 evaluate to?
  15. Operators, Expressions and Input
    15
    int x = 5; then x += 3; then x *= 2;. What is x?
  16. Operators, Expressions and Input
    16
    What does Math.round(2.5) return?
  17. Operators, Expressions and Input
    17
    A program calls sc.nextInt() and then sc.nextLine() to read a name, but the name comes back empty. Why?
  18. Operators, Expressions and Input
    18
    Order these steps as Java evaluates 2 + 3 * 4 - 6 / 2.
    1. 1Evaluate 6 / 2, giving 3
    2. 2Subtract 3 from 14, giving 11
    3. 3Add 2 + 12, giving 14
    4. 4Evaluate 3 * 4, giving 12

Endless drill

Fresh numbers every time, for the calculation questions in this unit.

Endless drillnew numbers every time

Generating a problem…

Keep going