Java 25 features: Java 25 practice questions
Java 25 features is 12% of the free Java 25 mock (6 of 50 questions). Pick an answer to see right away whether it is correct, with the explanation and the documentation section it comes from.
J.IX · Java 25 practice question
Which explicit constructor invocation rule applies to a non-canonical constructor of a record class in Java 25?
Check answer and explanation
Correct answer: It must contain an alternate constructor invocation this(...) and not super(...)
Explanation
A non-canonical record constructor must delegate via an alternate constructor invocation this(...) and must not contain a superclass constructor invocation super(...).
Common trap
Records implicitly extend java.lang.Record and do not allow constructors to invoke super(...) directly. Forbidding explicit constructor invocations applies to canonical record constructors, not non-canonical ones.
Source: JEP 513: Flexible Constructor Bodies — Description
Checked against the cited text on 2026-11-25.
J.IX · Java 25 practice question
A framework binds a request ID to a ScopedValue using run and then invokes application code. When does that binding expire?
Check answer and explanation
Correct answer: As soon as the run method call completes
Explanation
A scoped value passed to run remains bound only for the lifetime of that run call, so the binding ends automatically as soon as the run method call completes.
Common trap
Developers used to ThreadLocal expect bindings to persist for the entire thread lifetime unless explicitly cleared, but scoped values are strictly bounded to the run call. Reading a scoped value with get() does not consume or unbind it; any number of methods invoked during run can read it.
Source: JEP 506: Scoped Values — Description
Checked against the cited text on 2026-11-25.
J.IX · Java 25 practice question
What happens when a developer compiles this Java 25 record?
public record Point(int x, int y) {
public Point(int x, int y) {
super();
this.x = x;
this.y = y;
}
public static void main(String[] args) {
System.out.println(new Point(1, 2));
}
}
Check answer and explanation
Correct answer: Compilation fails because a canonical record constructor must not contain an explicit constructor invocation
Explanation
Even with flexible constructor bodies in Java 25, canonical record constructors remain restricted and must not contain an explicit constructor invocation such as super().
Common trap
Normal classes allow explicit super() calls, which tempts developers to assume canonical record constructors do too. Moving super() after field assignments would still fail because canonical record constructors forbid any explicit constructor invocation.
Source: JEP 513: Flexible Constructor Bodies — Description
Checked against the cited text on 2026-11-25.
J.IX · Java 25 practice question
Inside a constructor epilogue (after super()), which form of the return statement is permitted in Java 25?
Check answer and explanation
Correct answer: A bare return with no expression
Explanation
In a constructor epilogue, only a bare return with no expression is permitted; returning any expression is disallowed.
Common trap
Even though a constructor produces an instance of the class, returning this or any other expression is a compile-time error. Only the constructor prologue forbids return statements; the epilogue allows a bare return.
Source: JEP 513: Flexible Constructor Bodies — Description
Checked against the cited text on 2026-11-25.
J.IX · Java 25 practice question
How does Java 25 change the default imports provided automatically by the JShell tool?
Check answer and explanation
Correct answer: JShell automatically imports module java.base instead of an ad-hoc list of ten packages
Explanation
In Java 25, JShell replaces its previous ad-hoc list of ten on-demand package imports by automatically importing module java.base.
Common trap
JShell continues to provide automatic imports for convenience; it simply switches from ten specific packages to module java.base. Only module java.base is automatically imported, not every module on the module path.
Source: JEP 511: Module Import Declarations — Description
Checked against the cited text on 2026-11-25.
J.IX · Java 25 practice question
When does the JVM invoke the main method of the initial class or interface?
Check answer and explanation
Correct answer: After completion of the initialization for the initial class or interface
Explanation
JLS SE 25 §12.1.4 specifies that the main method is invoked only after completion of the initialization for the initial class or interface.
Common trap
Static initializers run as part of class initialization and must finish before main begins. Linking and initialization complete before main is invoked; they do not run concurrently with main.
Source: JLS SE 25 §12.1.4. Invoke a main Method
Checked against the cited text on 2026-11-25.
J.IX · Java 25 practice question
A developer launches a Java 25 subclass that declares no main method of its own, but its superclass declares a package-accessible no-arg void main() method and no String[] main method. What does the launcher do?
Check answer and explanation
Correct answer: It chooses and invokes the inherited no-arg main method
Explanation
Because the class neither declares nor inherits a main method with a String[] parameter, the launcher falls back to the inherited no-arg main method and invokes it.
Common trap
The launcher explicitly checks for both declared and inherited main methods. Both String[] and no-parameter main methods can be inherited and chosen by the launcher.
Source: JEP 512: Compact Source Files and Instance Main Methods — Description
Checked against the cited text on 2026-11-25.
J.IX · Java 25 practice question
A developer runs the java launcher on a compiled class that declares only void main(int count) and inherits no other main methods. What happens?
Check answer and explanation
Correct answer: The launcher reports an error and terminates
Explanation
Because the class neither declares nor inherits a main method with a String[] parameter or with no parameters, the launcher reports an error and terminates.
Common trap
The launcher never supplies default primitive arguments to overloaded main methods. Launching a class without a valid main method is an error rather than a silent no-op.
Source: JEP 512: Compact Source Files and Instance Main Methods — Description
Checked against the cited text on 2026-11-25.
J.IX · Java 25 practice question
A developer uses a module import declaration to import an aggregator module. What characterizes an aggregator module?
Check answer and explanation
Correct answer: It exports no packages itself, but exports the packages exported by the modules it requires
Explanation
An aggregator module does not export any packages of its own, but it exports the packages that are exported by the modules it requires.
Common trap
Aggregator modules operate through module requires directives rather than merging classes into an unnamed package. An aggregator module only exposes packages that are already exported by the modules it requires, not encapsulated internal packages.
Source: JEP 511: Module Import Declarations — Description
Checked against the cited text on 2026-11-25.
J.IX · Java 25 practice question
A web framework binds a ScopedValue in serve, calls user code, and the user code calls back into the framework's readKey method on the same thread. What does readKey observe?
Check answer and explanation
Correct answer: The value bound by serve earlier in the thread
Explanation
Scoped values provide one-way data sharing down the call stack, so readKey observes the value bound by serve earlier in the thread even when invoked through intermediate user code.
Common trap
Intermediate user stack frames do not clear or isolate the binding; any method called directly or indirectly from run sees the bound value. The run method provides one-way sharing from caller to callee rather than two-way mutation back to the caller.
Source: JEP 506: Scoped Values — Description
Checked against the cited text on 2026-11-25.
J.IX · Java 25 practice question
In a web framework that dedicates a thread to each request, application code creates its own virtual threads to run subtasks. How are those virtual threads related to the request-handling thread?
Check answer and explanation
Correct answer: They are child threads of the request-handling thread
Explanation
Virtual threads created by user code running on the request-handling thread are child threads of that request-handling thread.
Common trap
Even though virtual threads are lightweight, they still have a parent-child relationship with the thread that creates them. Creating a virtual thread does not replace or invert the relationship with the request-handling thread.
Source: JEP 506: Scoped Values — Description
Checked against the cited text on 2026-11-25.
J.IX · Java 25 practice question
A developer writes a compact source file containing only a helper method and a field, with no main method. What happens at compile time in Java 25?
Check answer and explanation
Correct answer: A compile-time error is reported because a compact source file must have a launchable main method
Explanation
The implicitly declared class of a compact source file must have a launchable main method; if it does not, the compiler reports a compile-time error.
Common trap
Normal top-level classes do not require a main method, so developers may expect a compact source file to compile as a library class. The compiler synthesizes a default no-parameter constructor for a compact source file, not a main method.
Source: JEP 512: Compact Source Files and Instance Main Methods — Description
Checked against the cited text on 2026-11-25.