Classes, objects and initialization
35 questions in 13 subtopics.
Constructors and constructor chaining
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();
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
In what order do these run whennew 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");
}
}
Static and instance initializer blocks
What are static and instance initializer blocks, when would you use each, and what happens if a static block throws an exception?Several services fail at startup withNoClassDefFoundError: Could not initialize class com.acme.Config, but the class is in the jar. What do you look for?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
Explain the uses of this and super in Java: in constructors, for fields, for methods and in inner classes. Can you callsuper.super.method()?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
Compare static nested classes, inner classes, local classes and anonymous classes. When would you choose each?How do nested classes access each other's private members at the bytecode level, and what changed with nestmates in JDK 11?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.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
Explain public, protected, package-private and private. What exactly does protected allow that package-private does not?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?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
What does final mean on a class, a method, a field and a local variable? Does a final field make the referenced object immutable?What visibility guarantee does the Java Memory Model give for final fields, and how can code break it?Should a shared library make its classes final by default? Argue both sides and state the policy you would adopt.Static members
Two tests pass alone but fail when run together. Both use a class withstatic List<String> cache = new ArrayList<>();. What is the problem and how do you fix it?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
What steps happen whennew Order(id) executes, from the bytecode to a usable object?A teammate proposes object pooling for small, short-lived request DTOs to reduce GC. How do you respond?Flexible constructor bodies
Java 25 finalised flexible constructor bodies (JEP 513). Rewrite this using the feature and explain what you may and may not do beforesuper(...):class PositiveRange extends Range {
PositiveRange(long lo, long hi) {
super(lo, hi);
if (lo < 0) throw new IllegalArgumentException();
}
}
Main method and program entry
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 voidmain(String[] a), static int main(String[] a), public static void main(String... a)?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
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<>();
}
new StringBuilder("hi"), n = 1, list = new ArrayList<>(List.of("a")), then printing sb, n and list.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
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.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?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?No question matches these choices.