Skip to content
Study Hall
← Unit 3: If Statements

ICS3U · Unit 3 test review

If Statements: Test Review

Everything for the Unit 3 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

Boolean Expressions and Comparison

  • A boolean is only true or false; Java will not accept a number or a String where a condition is required.
  • The six relational operators are ==, !=, <, >, <=, >=, and each produces a boolean.
  • && needs both sides true, || needs at least one, ! flips. Precedence runs !, then &&, then ||.
  • Short-circuit: && skips its right side when the left is false, so a guard such as count != 0 && must come first.
  • De Morgan: and .
  • = assigns and == compares; for Strings use .equals(), because == compares references.

if, else if and else

  • An if runs its block only when the condition is true; the optional else block runs in every other case.
  • Without braces, an if controls only the very next statement — always write the braces anyway.
  • An else if chain stops at the first true condition, so overlapping ranges must go from most demanding to least.
  • Exactly one branch of an if / else if / else chain ever runs, and the rest are skipped without being tested.
  • An else binds to the nearest unmatched if, regardless of indentation.
  • The ternary condition ? valueIfTrue : valueIfFalse is an expression that produces one of two values.

switch, Validation and Choosing a Structure

  • switch compares one selector against fixed case constants; default handles everything unmatched.
  • break exits the switch. Without it, execution falls through into the statements of the cases below.
  • Stacked bare labels such as case 4: case 6: are deliberate fall-through and share one block.
  • A switch selector may be int, short, byte, char, String or an enum — never double, float, long or boolean.
  • Use switch for a list of fixed values and else if for ranges, decimals or multi-variable conditions.
  • Integer division by zero throws ArithmeticException; double division by zero gives Infinity or NaN silently.

Where marks get lost

  • Comparing Strings with ==. It tests whether they are the same object, and it accidentally works on literals just often enough to hide the bug. (Boolean Expressions and Comparison)
  • Writing if (done = true) on a boolean. It compiles, assigns true, and then always takes the branch. (Boolean Expressions and Comparison)
  • Writing if (0 < mark < 50) as in mathematics. Java evaluates left to right, and the boolean from the first comparison cannot be compared with 50. (Boolean Expressions and Comparison)
  • Negating only half of a condition: the opposite of a && b is !a || !b, not !a && !b. (Boolean Expressions and Comparison)
  • Putting the guard second, as in total / count > 5 && count != 0. Short-circuit only helps when the protective test comes first. (Boolean Expressions and Comparison)
  • Ordering an else if chain from the least demanding condition upward, so the higher branches can never be reached. (if, else if and else)
  • Leaving out braces, then adding a second line to the branch that quietly runs every time. (if, else if and else)
  • Putting a semicolon straight after the condition, as in if (x > 5); — the branch becomes an empty statement and the block below always runs. (if, else if and else)
  • Using separate if statements where else if was meant, so two or more branches can run for the same value. (if, else if and else)
  • Assuming an else attaches to the outer if because of the indentation. It attaches to the nearest unmatched if. (if, else if and else)
  • Forgetting break, so a matched case runs on into every case below it and prints several answers. (switch, Validation and Choosing a Structure)
  • Trying to switch on a range, such as case mark >= 80:. Case labels must be single constant values. (switch, Validation and Choosing a Structure)
  • Switching on a double, which Java rejects because exact equality on decimals cannot be trusted. (switch, Validation and Choosing a Structure)
  • Leaving out default, so an unexpected value silently does nothing at all and the bug stays hidden. (switch, Validation and Choosing a Structure)
  • Reading a number with nextInt() before checking hasNextInt(), so bad input crashes instead of being reported. (switch, Validation and Choosing a Structure)

Practice test

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

  1. Boolean Expressions and Comparison
    1
    Which line correctly tests whether the String answer holds the text yes?
  2. Boolean Expressions and Comparison
    2
    Which condition is equivalent to !(score >= 50 && attempts < 3)?
  3. Boolean Expressions and Comparison
    3
    How many rows does a complete truth table for three boolean variables have?
  4. Boolean Expressions and Comparison
    4
    Rewrite !(raining || cold) without the outer NOT.
  5. Boolean Expressions and Comparison
    5
    Given String a = "cat"; and String c = new String("cat");, what does a == c produce?
  6. Boolean Expressions and Comparison
    6
    Which expression correctly tests that mark is between 50 and 79 inclusive?
  7. if, else if and else
    7
    With mark = 92, what does a chain that tests mark >= 50, then mark >= 60, then mark >= 80 print?
  8. if, else if and else
    8
    A student writes if (score > 100) on one line, then two indented println lines, with no braces. What happens?
  9. if, else if and else
    9
    In if (a) if (b) X(); else Y();, which if does the else belong to?
  10. if, else if and else
    10
    What is wrong with if (total > 100); followed by a block that prints a discount?
  11. if, else if and else
    11
    How many branches of a single if / else if / else chain execute for one run?
  12. if, else if and else
    12
    Rewrite if (n > 0) { sign = 1; } else { sign = -1; } as a single ternary assignment.
  13. switch, Validation and Choosing a Structure
    13
    What happens when a matched case has no break at the end of its statements?
  14. switch, Validation and Choosing a Structure
    14
    In a switch with cases 1, 2, 3 and default, all printing their own word and none having a break, what is printed when the selector is 3?
  15. switch, Validation and Choosing a Structure
    15
    What is the role of default in a switch?
  16. switch, Validation and Choosing a Structure
    16
    What does System.out.println(5.0 / 0); print?
  17. switch, Validation and Choosing a Structure
    17
    Match each part of a switch statement to its job.
    selector
    case
    break
    default
  18. switch, Validation and Choosing a Structure
    18
    Why should a validation message say what would be acceptable, not just that something is wrong?

Endless drill

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

Endless drillnew numbers every time

Generating a problem…

Keep going