Java Control Flow Quiz: If, Switch and Loops Practice Test
Twelve questions and two programs on Java control flow: if and else, switch statements and expressions, loops, break, continue, labels and loop patterns.
- Course: Java study plan
- Module: Control flow
- Kind: Checkpoint — cleared at 70%
- Reading time: 22 min
- Runtime: OpenJDK 18
Checkpoint — Control flow is the checkpoint that closes the Control flow module: a graded quiz and whole-program exercises, passed at 70%.
Instructions
This checkpoint covers if/else, switch in its classic, arrow and expression forms, the four loops, break/continue/labels/return, and the standard loop patterns.
How it works. Twelve questions and two programs; 70% on the questions and both programs accepted clears the module. Retake as often as you like.
Before you start, make sure you can answer:
- Which
ifa danglingelsebelongs to. - What fall-through is and which
switchform has it. - What a switch expression must be, and the keyword that yields a value from a block.
- The difference between
whileanddo-while; how many timesi <= nruns. - What
breakdoes inside aswitchinside a loop. - How
allandanyloops are seeded and why.
The programs are pattern exercises: a grid search that needs the right exit, and a classifier that wants a switch expression.
Common questions
Which if does a dangling else belong to in Java?
The nearest unmatched if. Java ignores indentation, so in if (a) if (b) x(); else y(); the else belongs to if (b). Braces around every body remove the ambiguity.
What must a switch expression be, and how does a block give its value?
A switch expression must be exhaustive: it needs a default for an int or String selector, or a case for every constant of an enum. A branch that is a block gives its value with yield, not return.
How many times does for (int i = 0; i <= n; i++) run?
n + 1 times, once for every value of i from 0 to n inclusive. The half-open form i < n runs exactly n times, which is why it is the standard for indexing arrays.
Exercises
Grid search
Read integers R, C and target, then an R × C grid of integers (row by row). Scanning row-major (row 0 left to right, then row 1, …), print found <row> <col> for the first cell equal to target, or not found. Leave both loops as soon as the cell is found — with a labelled break, or by putting the loops in a method that returns.
Example: 2 3 7 then 1 2 3 / 7 8 7 → found 1 0.
Opening hours
A shop is open Monday–Friday from 9 to 17 (an hour h counts as open when 9 ≤ h < 17), Saturday from 10 to 14, and closed on Sunday. Read an integer n, then n lines each with a day name (any letter case) and an hour (0–23). Print open, closed, or unknown day for a name that is not a weekday.
Use a switch expression on the lower-cased day name; use blocks with yield where a branch needs an if.
Example: monday 9 → open; Saturday 14 → closed; funday 12 → unknown day.
Roman numerals, both ways
Read an integer n and n tokens. A token made of digits (1–3999) is converted to a Roman numeral; a token made of the letters IVXLCDM is converted from Roman to a number (subtractive pairs like IV and XC apply). Print <token> -> <result>.
Example: 4 then 1994 MCMXCIV 3999 IV →
1994 -> MCMXCIV
MCMXCIV -> 1994
3999 -> MMMCMXCIX
IV -> 4In this module: Control flow
← Loop patterns every program is made of · Defining and calling methods →