Course contents

Modern Java syntax: interview questions & scenarios

Premium16 questions · 16 what-if follow-ups

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 questions

These start with the syntax everyone quotes, then move into the compile-time guarantees that matter in backend code.

Question 1. CodingWarm-upAsked very often

How would you refactor this instanceof chain in Java 21?

Tests pattern switch, sealed exhaustiveness, guards

Show answerHide answer1 follow-up

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.

Fig. 1

A sealed payment hierarchy turns a new permitted subtype into a compile-time gap in every exhaustive switch that forgot 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

  1. 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 instanceof checks there, with tests, and use exhaustive switches inside your domain.

Question 2. ConceptWarm-upAsked very often

What does case null do in a pattern switch?

Tests null handling, switch selector semantics

Show answerHide answer1 follow-up

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

  1. 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.

Question 3. What does it print?CoreCommonly asked

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

Question 4. DebuggingSeniorSometimes asked

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 questions

Records remove boilerplate, but the interview usually checks whether you understand shallow immutability.

Question 5. ConceptWarm-upAsked very often

Are records immutable?

Tests records, shallow immutability, defensive copies

Model answer, how to say it, 1 follow-up: unlock with the Java Pass

Question 6. ScenarioCoreCommonly asked

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

Question 7. ConceptCoreCommonly asked

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

Question 8. ConceptCoreCommonly asked

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 questions

This group is about version accuracy. A correct answer says what is preview in Java 21 and what is final later.

Question 9. ConceptWarm-upCommonly asked

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

Question 10. ConceptCoreSometimes asked

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

Question 11. DesignCoreCommonly asked

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 questions

These are the "what if it breaks at 2 a.m." versions of the same topics.

Question 12. ScenarioSeniorCommonly asked

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

Question 13. DebuggingSeniorCommonly asked

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

Question 14. DebuggingCoreSometimes asked

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

Question 15. DesignSeniorSometimes asked

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

Question 16. ConceptSeniorCommonly asked

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

Lessons in Modern Java syntax