Modern Java syntax: interview questions & scenarios
These are the modern Java syntax questions I ask when I want to know whether someone uses newer Java carefully, not fashionably. Say your answer out loud first, then open the model answer, and use the lessons if you need a reset: Pattern matching for switch, Compact source files and instance main methods, Designing immutable objects, Records in Java and Sealed classes and interfaces.
Pattern matching and sealed types
4 questionsThese start with the syntax everyone quotes, then move into the compile-time guarantees that matter in backend code.
How would you refactor this instanceof chain in Java 21?
Tests pattern switch, sealed exhaustiveness, guards
Show answerHide answer
Model answer
Use a sealed hierarchy and a pattern switch expression when the set of domain variants is closed.
sealed interface Payment permits CardPayment, UpiPayment, WalletPayment { }
record CardPayment(long amountPaise, boolean international) implements Payment { }
record UpiPayment(long amountPaise) implements Payment { }
record WalletPayment(long amountPaise) implements Payment { }
static long fee(Payment p) {
return switch (p) {
case CardPayment c when c.international() -> c.amountPaise() * 3 / 100;
case CardPayment c -> c.amountPaise() * 2 / 100;
case UpiPayment u -> 0;
case WalletPayment w -> w.amountPaise() / 100;
};
}
The win is not fewer lines. The win is exhaustiveness: add a permitted subtype and the switch stops compiling until you handle it.
A sealed payment hierarchy turns a new permitted subtype into a compile-time gap in every exhaustive switch that forgot it
How I’d say it in the interview
"I'd use a sealed Payment type and a pattern switch with no default. Guards go before the unguarded case. Adding a new payment type should break the build, not reach an else throw in production."
Signals depth
Knowing that default can hide the missing subtype, and that this is best when the hierarchy is closed and owned by your code.
Red flags
Replacing three branches with a Map<Class<?>, Function> and losing compile-time checking.
Follow-ups interviewers ask next
What if the types come from a third-party library you cannot seal?
Adapt them once at the boundary into your own sealed type. Keep any
instanceofchecks there, with tests, and use exhaustive switches inside your domain.
What does case null do in a pattern switch?
Tests null handling, switch selector semantics
Show answerHide answer
Model answer
case null handles a null selector explicitly. Without it, a pattern switch over a null reference throws NullPointerException before type cases run.
static String label(Payment p) {
return switch (p) {
case null -> "missing";
case CardPayment c -> "card";
case UpiPayment u -> "upi";
case WalletPayment w -> "wallet";
};
}
Use case null when null is part of the boundary contract. If null means invalid data, reject it earlier or let the switch fail loudly.
How I’d say it in the interview
"Pattern switch does not silently treat null as no match. I either write case null deliberately or I let the NPE tell me a caller broke the contract."
Signals depth
Mentioning that case null, default -> ... is possible, but often too broad for domain code.
Red flags
Saying it behaves like instanceof, where null gives false.
Follow-ups interviewers ask next
What if the selector type is sealed and all subtypes are handled, but null is possible?
The switch is exhaustive for non-null values, not for null unless you add
case null. Null handling is a separate decision.
Why can this switch fail to compile because of dominance?
Tests dominance, guarded patterns, ordering
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
What is MatchException, and when might you see it?
Tests MatchException, separate compilation, sealed evolution
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
Records, record patterns and immutability
4 questionsRecords remove boilerplate, but the interview usually checks whether you understand shallow immutability.
Are records immutable?
Tests records, shallow immutability, defensive copies
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
Your immutable cache key started missing lookups after a deploy. What would you check?
Tests mutable keys, hashCode stability, defensive copies
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
What is the difference between List.copyOf and Collections.unmodifiableList?
Tests immutable snapshots, unmodifiable views
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
How do record patterns help inside a switch?
Tests record patterns, deconstruction, pattern switch
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
Compact source files and instance main methods
3 questionsThis group is about version accuracy. A correct answer says what is preview in Java 21 and what is final later.
Is public static void main(String[] args) still required?
Tests main method, instance main, version status
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
What is the launcher order when several main methods exist?
Tests launch protocol, main selection
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
Where would you use compact source files, and where would you avoid them?
Tests compact source files, production judgement
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
Production scenarios and trade-offs
5 questionsThese are the "what if it breaks at 2 a.m." versions of the same topics.
A new payment subtype shipped and one endpoint started throwing from a default branch. What went wrong?
Tests sealed switches, default branches, release safety
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
A service exposes an immutable OrderSnapshot, but clients can still change its items. Walk me through it.
Tests defensive copies, accessors, arrays and collections
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
Your Java 21 build fails on a compact source file copied from a Java 25 tutorial. Why?
Tests preview features, source versions, IO helper
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
A pattern switch over events is readable, but a teammate nested three record patterns in one case. What do you do?
Tests readability, record patterns, domain methods
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
How do final fields make immutable objects safer across threads?
Tests final field semantics, safe publication, this escape
Model answer, how to say it, 1 follow-up: unlock with the Java Pass
Premium question set
Keep reading with the Java Pass
The rest of this question set is part of Java One-Stop Interview Prep: in-depth explanations, diagrams, model answers and how to say them in the interview.
From ₹199 · one-time payment · No auto-renewal