Scrivio Interview library

Classes, objects and initialization

35 questions in 13 subtopics.

Constructors and constructor chaining

CodingFoundations5 min read
What does this print?
class A {
A() {
this(1);
System.out.print("A ");
}
A(int x) {
System.out.print("A" + x + " ");
}
}
class B extends A {
B() {
System.out.print("B ");
}
}
new B();
DesignIntermediate7 min read
A domain class has six constructors with different parameter combinations and callers mix them up. What alternatives do you propose and what are their trade-offs?
ConceptPrincipal7 min read
Java constructors always run the full superclass chain and, until Java 25, forced super(...) first. Explain why the language was designed that way, why it relaxed the rule now, and what that suggests about how to judge future language changes.

Initialization order

CodingFoundations5 min read
In what order do these run when new Child() is called for the first time?
class Parent {
static {
p("Ps");
}
{
p("Pi");
}
Parent() {
p("Pc");
}
}
class Child extends Parent {
static {
p("Cs");
}
{
p("Ci");
}
Child() {
p("Cc");
}
}
InternalsIntermediate7 min read
When exactly is a class initialized in the JVM, and why can a static final constant be read without triggering initialization?
DebuggingIntermediate7 min read
A subclass overrides a method that the superclass constructor calls. The override reads a field initialized in the subclass and gets null. Explain and fix.
DesignPrincipal7 min read
Static initialization and singletons have caused startup failures, test pollution and deadlocks across your organization's services for years. With Spring dependency injection, CDS and AOT caches available, what rules on static state and class initialization would you set org-wide?

Static and instance initializer blocks

ConceptFoundations6 min read
What are static and instance initializer blocks, when would you use each, and what happens if a static block throws an exception?
DebuggingIntermediate6 min read
Several services fail at startup with NoClassDefFoundError: Could not initialize class com.acme.Config, but the class is in the jar. What do you look for?
ScenarioAdvanced7 min read
Startup time of a large Spring application is dominated by many static initializers that build caches and parse resources. What fleet-wide approach do you take?

This and super

ConceptFoundations6 min read
Explain the uses of this and super in Java: in constructors, for fields, for methods and in inner classes. Can you call super.super.method()?
DebuggingIntermediate6 min read
An object is registered as a listener in its own constructor with bus.register(this). Another thread occasionally sees it with null fields. What is happening?

Nested, inner, local and anonymous classes

ConceptFoundations6 min read
Compare static nested classes, inner classes, local classes and anonymous classes. When would you choose each?
InternalsIntermediate6 min read
How do nested classes access each other's private members at the bytecode level, and what changed with nestmates in JDK 11?
DebuggingIntermediate7 min read
A heap dump shows many large Activity or Controller objects kept alive by small listener objects stored in a long-lived registry. The listeners are anonymous classes created inside instance methods. Explain the leak.
ConceptPrincipal7 min read
Inner classes were added in Java 1.1 mainly for event listeners, and lambdas in Java 8 replaced most anonymous classes. Explain how each change altered typical Java code, and what lasting problems inner classes still cause in older codebases.

Access modifiers and packages

ConceptFoundations6 min read
Explain public, protected, package-private and private. What exactly does protected allow that package-private does not?
DesignAdvanced6 min read
Your company's shared libraries expose many public classes in packages named impl or internal, and consumers depend on them, blocking refactoring. How do you fix this over time?
LeadershipPrincipal6 min read
Java's package-private visibility has proved too weak for large codebases, and many organizations never adopted JPMS modules. What strategy would you set for enforcing module and team boundaries in Java code over the next five years?

Final classes, methods and fields

ConceptFoundations6 min read
What does final mean on a class, a method, a field and a local variable? Does a final field make the referenced object immutable?
InternalsIntermediate6 min read
What visibility guarantee does the Java Memory Model give for final fields, and how can code break it?
LeadershipAdvanced6 min read
Should a shared library make its classes final by default? Argue both sides and state the policy you would adopt.

Static members

DebuggingFoundations7 min read
Two tests pass alone but fail when run together. Both use a class with static List<String> cache = new ArrayList<>();. What is the problem and how do you fix it?
ConceptIntermediate6 min read
Static methods cannot be overridden. What happens when a subclass declares a static method with the same signature, and how does that affect calls through a variable of the parent type?

Object creation and the new keyword

ConceptFoundations7 min read
What steps happen when new Order(id) executes, from the bytecode to a usable object?
ScenarioIntermediate6 min read
A teammate proposes object pooling for small, short-lived request DTOs to reduce GC. How do you respond?

Flexible constructor bodies

CodingIntermediate6 min read
Java 25 finalised flexible constructor bodies (JEP 513). Rewrite this using the feature and explain what you may and may not do before super(...):
class PositiveRange extends Range {
PositiveRange(long lo, long hi) {
super(lo, hi);
if (lo < 0) throw new IllegalArgumentException();
}
}
LeadershipAdvanced6 min read
Java 25 makes flexible constructor bodies final. How would you introduce the feature in a large codebase, and which existing patterns would you retire?

Main method and program entry

ConceptFoundations7 min read
What exactly must a classic Java main method look like for the java launcher to start it, and what happens with these variants: private static void main(String[] a), static int main(String[] a), public static void main(String... a)?
DebuggingIntermediate8 min read
A Spring Boot fat jar starts fine with java -jar app.jar, but an operations script that runs java -cp app.jar com.acme.Application fails with ClassNotFoundException for Spring classes. Another service never exits after main returns. Explain both problems.

Pass by value semantics

CodingFoundations6 min read
What does this print, and why?
static void change(StringBuilder sb, int n, List<String> list) {
sb.append("!");
sb = new StringBuilder("new");
n = 5;
list.add("x");
list = new ArrayList<>();
}
called with sb = new StringBuilder("hi"), n = 1, list = new ArrayList<>(List.of("a")), then printing sb, n and list.
ScenarioIntermediate7 min read
A service passes its internal List<OrderLine> to a pricing library, and later finds lines modified and reordered. Another class returns its internal Map from a getter. Explain the risk in terms of pass-by-value of references and how you design APIs to avoid it.

Compact source files and instance main methods

ConceptFoundations6 min read
Java 25 finalised compact source files and instance main methods (JEP 512). Show the smallest hello world program allowed now, and explain which class it belongs to, what is imported automatically, and how it is run.
InternalsIntermediate6 min read
With instance main methods allowed in Java 25, how does the launcher choose which main method to run if a class has several candidates, and how is an instance main invoked? What does this mean for existing code?
LeadershipAdvanced7 min read
Your company wants to use Java 25 compact source files for operational scripts that are now written in Bash and Python. As the architecture lead, would you allow it, and what guidelines would you set?