Scrivio Interview library

Sealed types and pattern matching

22 questions in 8 subtopics.

Sealed classes and interfaces

ConceptFoundations6 min read
Explain sealed classes and interfaces: syntax, what permitted subclasses must declare, and where they must live.
DesignIntermediate7 min read
When would you choose a sealed interface over an enum, over an open interface, or over an abstract class with a private constructor?
LeadershipAdvanced7 min read
A shared library exposes a sealed interface PaymentEvent with five record subtypes. Consumers write exhaustive switches. A sixth subtype is needed. How do you handle the release?
DesignPrincipal6 min read
Sealed hierarchies give compile-time guarantees inside one codebase but become coordination points when shared between teams. How would you decide, as an organization-wide rule, where sealed types may appear in public APIs?

Pattern matching for instanceof

CodingFoundations6 min read
Rewrite using pattern matching for instanceof and explain the scope of the binding variable:
if (obj instanceof String) {
String s = (String) obj;
if (s.length() > 3) return s.toUpperCase();
}
CodingIntermediate6 min read
Simplify this equals method using instanceof patterns and explain why it is still correct for null.
public boolean equals(Object o) {
if (!(o instanceof Point)) return false;
Point p = (Point) o;
return x == p.x && y == p.y;
}
LeadershipPrincipal6 min read
Pattern matching features arrived over several releases (instanceof patterns, switch patterns, record patterns, unnamed patterns, primitive patterns still in preview in Java 25). How would you pace adoption across many teams on different JDK versions?

Switch pattern matching

CodingFoundations6 min read
Write a switch that returns a description for an Object: "null" for null, the length for a String longer than 10, "short string" otherwise, "int" for Integer, and "other" for anything else.
ConceptIntermediate7 min read
Explain dominance rules in pattern switches and why case CharSequence cs -> ...; case String s -> ... does not compile.
DesignAdvanced7 min read
Your team is replacing a Visitor pattern over an AST with sealed interfaces and pattern switches. What do you gain and lose, and how do you decide per module?

Record patterns and deconstruction

CodingFoundations6 min read
Given record Point(int x, int y) and record Line(Point a, Point b), write a method that returns true for vertical lines using record patterns.
ConceptIntermediate6 min read
Record patterns deconstruct using accessor methods. What happens if a record overrides an accessor with side effects or a defensive copy, and why does that matter for pattern matching code?
ScenarioAdvanced6 min read
A team uses nested record patterns heavily in a message-routing service. Performance profiling and readability reviews raise concerns. What do you look for?

Exhaustiveness checking

InternalsIntermediate6 min read
How does the compiler check exhaustiveness for switches over sealed types, and what happens at run time when a new permitted subclass appears without recompiling the switch?
LeadershipPrincipal7 min read
Exhaustiveness checking moves some errors from run time to compile time, but couples every switch to the full set of variants. How do you set org-wide guidance on using exhaustive switches versus default branches?

Unnamed variables and patterns

CodingFoundations6 min read
Use unnamed variables and patterns (Java 22) to simplify each case: an unused catch parameter, an unused lambda parameter, and a record pattern where only one component matters.
ConceptIntermediate5 min read
Explain how unnamed patterns help in switch cases over sealed types, for example case Circle _, Square _ -> ..., and why that was not allowed with named bindings.

Primitive patterns

ConceptIntermediate6 min read
What is the status of primitive types in patterns, instanceof and switch in Java 25, and what does if (value instanceof byte b) mean for an int value?
LeadershipAdvanced6 min read
A team asks to enable --enable-preview in production builds to use primitive patterns. What questions do you ask and what is your decision rule?

Algebraic data types in Java

DesignIntermediate6 min read
Model a payment result as an algebraic data type in Java 25 with success, declined with a reason, and failed with an exception, and show how callers consume it.
DesignAdvanced6 min read
Your organization is adopting data-oriented programming with sealed interfaces and records for domain modelling. Where does it fit with object-oriented design and Spring-based services, and where does it not?
ConceptPrincipal6 min read
Java resisted algebraic data types and pattern matching for 25 years, then delivered them in stages from Java 14 to 21. Explain why it was done in that order (records, sealed, patterns) and what that tells you about betting on future language features.