← 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
booleanis onlytrueorfalse; 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 ascount != 0 &&must come first. - De Morgan: and .
=assigns and==compares; for Strings use.equals(), because==compares references.
if, else if and else
- An
ifruns its block only when the condition istrue; the optionalelseblock runs in every other case. - Without braces, an
ifcontrols only the very next statement — always write the braces anyway. - An
else ifchain 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
elsebinds to the nearest unmatchedif, regardless of indentation. - The ternary
condition ? valueIfTrue : valueIfFalseis an expression that produces one of two values.
switch, Validation and Choosing a Structure
switchcompares one selector against fixedcaseconstants;defaulthandles everything unmatched.breakexits 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,Stringor an enum — neverdouble,float,longorboolean. - Use
switchfor a list of fixed values andelse iffor 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 && bis!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
ifstatements whereelse ifwas meant, so two or more branches can run for the same value. (if, else if and else) - Assuming an
elseattaches to the outerifbecause of the indentation. It attaches to the nearest unmatchedif. (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 checkinghasNextInt(), 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.
- Boolean Expressions and Comparison1Which line correctly tests whether the String
answerholds the text yes? - Boolean Expressions and Comparison2Which condition is equivalent to
!(score >= 50 && attempts < 3)? - Boolean Expressions and Comparison3How many rows does a complete truth table for three boolean variables have?
- Boolean Expressions and Comparison4Rewrite
!(raining || cold)without the outer NOT. - Boolean Expressions and Comparison5Given
String a = "cat";andString c = new String("cat");, what doesa == cproduce? - Boolean Expressions and Comparison6Which expression correctly tests that
markis between 50 and 79 inclusive? - if, else if and else7With
mark = 92, what does a chain that testsmark >= 50, thenmark >= 60, thenmark >= 80print? - if, else if and else8A student writes
if (score > 100)on one line, then two indented println lines, with no braces. What happens? - if, else if and else9In
if (a) if (b) X(); else Y();, which if does the else belong to? - if, else if and else10What is wrong with
if (total > 100);followed by a block that prints a discount? - if, else if and else11How many branches of a single if / else if / else chain execute for one run?
- if, else if and else12Rewrite
if (n > 0) { sign = 1; } else { sign = -1; }as a single ternary assignment. - switch, Validation and Choosing a Structure13What happens when a matched case has no
breakat the end of its statements? - switch, Validation and Choosing a Structure14In 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?
- switch, Validation and Choosing a Structure15What is the role of
defaultin a switch? - switch, Validation and Choosing a Structure16What does
System.out.println(5.0 / 0);print? - switch, Validation and Choosing a Structure17Match each part of a switch statement to its job.selectorcasebreakdefault
- switch, Validation and Choosing a Structure18Why 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…