Pattern matching and switch: Java 25 practice questions
Pattern matching and switch is 12% of the free Java 25 mock (6 of 50 questions). Pick an answer to see right away whether it is correct, with the explanation and the documentation section it comes from.
J.VII · Java 25 practice question
According to the Java Language Specification, how is pattern matching classified relative to statement execution and expression evaluation?
Check answer and explanation
Correct answer: It is a distinct run-time process of testing a value against a pattern and initializing any declared pattern variables on a match
Explanation
Pattern matching is a distinct run-time process of testing a value against a pattern and initializing any declared pattern variables on a match, separate from statement execution and expression evaluation.
Common trap
While instanceof is a boolean expression that uses pattern matching, pattern matching itself is defined as distinct from expression evaluation. It is also a run-time process rather than a compile-time macro expansion.
Source: JLS SE 25 §14.30.2. Pattern Matching
Checked against the cited text on 2026-11-25.
J.VII · Java 25 practice question
During run-time evaluation of a switch expression, when does the JVM determine which switch label applies?
Check answer and explanation
Correct answer: Only after the selector expression has been evaluated and completed normally
Explanation
The JVM determines which switch label applies only after the selector expression has been evaluated and completed normally, because label matching depends on the selector's run-time value.
Common trap
Static types are checked at compile time, not used to choose a run-time label before the selector runs. Java evaluates the selector expression strictly before attempting to match any switch label, never in parallel.
Source: JLS SE 25 §15.28.2. Run-Time Evaluation of switch Expressions
Checked against the cited text on 2026-11-25.
J.VII · Java 25 practice question
What happens when a developer compiles this Java 25 code?
void main() {
String text = "42";
if (text instanceof Integer n) {
System.out.println(n);
}
}
Check answer and explanation
Correct answer: It fails to compile because the Integer type pattern is not applicable at type String
Explanation
It fails to compile because the Integer type pattern is not applicable at type String; no testing conversion exists between the unrelated final reference types String and Integer.
Common trap
Testing an Object reference against Integer compiles and evaluates to false for a String, which makes thinking it prints nothing tempting, but static type String cannot be converted to Integer. instanceof never throws ClassCastException.
Source: JLS SE 25 §14.30.3. Properties of Patterns
Checked against the cited text on 2026-11-25.
J.VII · Java 25 practice question
What happens when a developer compiles this Java 25 program?
void main() {
Object obj = 'a';
switch (obj) {
case Character c:
System.out.println(c);
case Integer i:
System.out.println(i);
break;
default:
break;
}
}
Check answer and explanation
Correct answer: It fails to compile because execution cannot fall through a case label that declares a pattern variable
Explanation
It fails to compile because execution cannot fall through a case label that declares a pattern variable, as i would not be initialized if obj were a Character.
Common trap
Traditional colon switch cases allow fall-through, which makes expecting it to compile and print null or throw NullPointerException tempting, but the compiler rejects fall-through into any case label that declares a pattern variable.
Source: JEP 441: Pattern Matching for switch — Description
Checked against the cited text on 2026-11-25.
J.VII · Java 25 practice question
What happens when a developer compiles this Java 25 program?
void main() {
Object obj = "hello";
switch (obj) {
case CharSequence cs -> System.out.println(cs.length());
case String s -> System.out.println(s);
default -> System.out.println("other");
}
}
Check answer and explanation
Correct answer: It fails to compile because the CharSequence pattern dominates the unreachable String pattern
Explanation
It fails to compile because the CharSequence pattern dominates the unreachable String pattern; every String is a CharSequence, so the String case label could never be selected.
Common trap
Developers may think switch simply picks the first matching branch or automatically prioritizes the most specific subtype like overloaded methods, but Java's pattern switch rejects dominated case labels at compile time.
Source: JEP 441: Pattern Matching for switch — Description
Checked against the cited text on 2026-11-25.
J.VII · Java 25 practice question
What happens when this Java 25 program is compiled and run?
int load() {
throw new IllegalStateException("boom");
}
void main() {
String res = switch (load()) {
case 1 -> "one";
default -> "other";
};
System.out.println(res);
}
Check answer and explanation
Correct answer: It throws IllegalStateException directly from evaluating the selector expression
Explanation
It throws IllegalStateException directly from evaluating the selector expression because abrupt completion of the selector causes the entire switch expression to complete abruptly for the same reason.
Common trap
The default branch only handles normal selector values that do not match other labels, not exceptions thrown by the selector. MatchException is used for unhandled exhaustiveness gaps at run time, not for exceptions thrown during selector evaluation.
Source: JLS SE 25 §15.28.2. Run-Time Evaluation of switch Expressions
Checked against the cited text on 2026-11-25.
J.VII · Java 25 practice question
What happens when a developer tries to compile this Java 25 switch?
void main() {
Object obj = 5;
switch (obj) {
case Character c, Integer i -> System.out.println("char or int");
default -> System.out.println("other");
}
}
Check answer and explanation
Correct answer: It fails to compile because multiple patterns cannot be combined in a single comma-separated case label
Explanation
It fails to compile because multiple patterns cannot be combined in a single comma-separated case label, since one of the pattern variables would remain uninitialized.
Common trap
Arrow switch rules allow comma-separated constant labels like case 1, 2 ->, which makes expecting case Character c, Integer i -> to compile tempting, but multiple patterns in one case label are prohibited. A syntax/compile error prevents the program from running to print other.
Source: JEP 441: Pattern Matching for switch — Description
Checked against the cited text on 2026-11-25.
J.VII · Java 25 practice question
A developer has a variable of type Box<Box<String>> and wants to match it with a nested record pattern using instanceof. How does the Java 25 compiler handle omitting type arguments on both the outer and inner Box patterns?
Check answer and explanation
Correct answer: The compiler infers the full parameterized types for both the outer and inner record patterns
Explanation
The compiler infers the full parameterized types for both the outer and inner record patterns, resolving Box(Box(var s)) to Box<Box<String>>(Box<String>(var s)).
Common trap
It is easy to assume type arguments must be written on the outer pattern like diamond syntax, but JEP 440 allows dropping them on both levels. The compiler does not degrade omitted record pattern type arguments to raw types with Object.
Source: JEP 440: Record Patterns — Description
Checked against the cited text on 2026-11-25.
J.VII · Java 25 practice question
What does this Java 25 program output when compiled and run?
record Box<T>(T value) {}
void main() {
Box<Box<String>> bbs = new Box<>(new Box<>("ok"));
if (bbs instanceof Box(Box(var s))) {
System.out.println("String " + s);
}
}
Check answer and explanation
Correct answer: String ok
Explanation
The program prints String ok because the compiler infers the type arguments for both the outer and inner Box record patterns as Box<Box<String>>(Box<String>(var s)).
Common trap
Omitting angle brackets on Box looks like a raw type, which makes expecting a compile-time error tempting, but record patterns support type argument inference from the target type. Local variable type inference with var is explicitly allowed in nested record patterns.
Source: JEP 440: Record Patterns — Description
Checked against the cited text on 2026-11-25.
J.VII · Java 25 practice question
What is the output when this Java 25 program is compiled and executed?
void main() {
Object obj = "hi";
switch (obj) {
case String s -> System.out.println("string:" + s);
case CharSequence cs -> System.out.println("seq:" + cs.length());
default -> System.out.println("other");
}
}
Check answer and explanation
Correct answer: string:hi
Explanation
The program prints string:hi because placing the more specific String pattern before CharSequence avoids dominance, and the first matching case label is selected.
Common trap
Printing seq:2 would only happen if the CharSequence branch ran instead of String, which does not occur since String appears first. Overlapping types are allowed in switch as long as the subtype pattern precedes the supertype pattern.
Source: JEP 441: Pattern Matching for switch — Description
Checked against the cited text on 2026-11-25.
J.VII · Java 25 practice question
What is the result of compiling this Java 25 code?
void main() {
Object obj = 10;
switch (obj) {
case Character c:
case Integer i:
System.out.println("matched");
break;
default:
break;
}
}
Check answer and explanation
Correct answer: It fails to compile because stacking multiple pattern case labels on one statement group is not permitted
Explanation
It fails to compile because stacking multiple pattern case labels on one statement group is not permitted, even if the pattern variables are unused in the body.
Common trap
Because neither c nor i is read in the body, it is tempting to think the compiler permits stacking them like constant labels, but the grammar and fall-through rules forbid multiple pattern labels on a single group. Autoboxing 10 to Integer is valid and would not cause it to print nothing.
Source: JEP 441: Pattern Matching for switch — Description
Checked against the cited text on 2026-11-25.
J.VII · Java 25 practice question
What is the result of compiling this Java 25 switch expression?
void main() {
int n = 2;
int res = switch (n) {
case 1:
yield 10;
default:
System.out.println("default");
};
System.out.println(res);
}
Check answer and explanation
Correct answer: It fails to compile because the last statement in the switch block can complete normally
Explanation
It fails to compile because the last statement in the switch block can complete normally without a yield statement or throw, which is a compile-time error in a switch expression.
Common trap
Unlike local variables or fields with default values, a switch expression cannot implicitly return 0 when a colon group reaches the end. This defect is caught at compile time rather than throwing IllegalStateException at run time.
Source: JLS SE 25 §15.28.1. The Switch Block of a switch Expression
Checked against the cited text on 2026-11-25.
J.VII · Java 25 practice question
A developer writes a switch expression over an enum with three constants and handles every constant explicitly. Why should the developer omit a default branch?
Check answer and explanation
Correct answer: Omitting default lets the compiler verify exhaustiveness if a new enum constant is added later
Explanation
Omitting default lets the compiler verify exhaustiveness if a new enum constant is added later, whereas a manual default branch masks missing cases and defeats compile-time exhaustiveness checking.
Common trap
Java 25 does allow a default branch in an enum switch expression even when all constants are covered, so claiming it causes a compile-time error is false. Bytecode optimization into tableswitch is unrelated to why manual default branches are discouraged for exhaustiveness.
Source: JEP 441: Pattern Matching for switch — Description
Checked against the cited text on 2026-11-25.