Test 9 - Free Java SE 25 certification Mock test - Exam Code 1Z0-831-Oracle certified Java SE 25 Professional
Java 25 has finalised flexible constructor bodies and Oracle list them explicitly in the 1Z0-831 objectives. The 1Z0-831 free mock test goes into considerable depth on constructors and initialisation, giving you the opportunity to practice with the old rules as well as the new ones so you can answer with confidence whichever of the two the question happens to use.
The old rule was that you had to have a call to super() or this() as the very first statement in a constructor. Flexible constructor bodies have relaxed that. You can now have code prior to the explicit constructor call, as long as it doesn't reference the instance that's being constructed. You'll get a series of questions to test your ability to work out what you can do in that early phase (e.g. argument validation, value computation, initialisation of the current class's own fields) and what you can't do (e.g. reading this, calling instance methods). The questions will explain why this helps to prevent a superclass constructor from calling an overridden method which would see the instance in an inconsistent state.
Beyond that headline feature you'll be testing out the full constructor toolkit. The items covered are default constructors and when the compiler stops providing one, chained constructors using this, and the compile error that results when you have two constructors calling each other in a loop. You'll also see the effect of a missing no-argument superclass constructor on a subclass (which will fail to compile).
Initialisers get equal attention. Static initialisers run once, instance initialisers run every time an object is constructed and both of them run in the order in which they appear (together with the field declarations). Mixed snippets will ask you to print out the exact sequence of messages that would be produced when a subclass object is instantiated, that is to say the parent static block, the child static block, the parent instance block, the parent constructor, the child instance block and the child constructor.
Exceptions thrown during initialisation do appear. An exception which escapes a static block results in an ExceptionInInitializerError, whereas one which escapes an instance initialiser propagates to the caller. Final fields must be definitely assigned and you'll be looking at a number of programs to that effect.
Every explanation goes through the order of execution line by line. Do the test slowly the first time, then go through it again at speed. Watch out for polymorphism in constructors. If the parent constructor calls an overridden method the child's fields will have their default values and that timing trap is what produces the rather surprising output that these questions are all about. Initialisation questions are all about patience and having a clear mental model and that's exactly what this one does.
