When does an instance initialiser block { … } run?
It runs together with field initialisers, in source order.
Constructors and the this Keyword · Exercise 1 of 4
Twenty multiple-choice questions on Java constructors and the this keyword: default constructors, overloading, this() chaining, initialisation order, private constructors, copy constructors and builders. Pick an option to check your answer instantly.
When does an instance initialiser block { … } run?
It runs together with field initialisers, in source order.
How many times does a static initialiser block run if 100 objects are created?
Static state belongs to the class, not to each object.
Are constructors inherited by subclasses?
Covered in detail in Inheritance.
A class has private final int id; with no initialiser. What must every constructor do?
Missing or double assignment is a compile error.
What is a copy constructor in Java?
Unlike C++, Java never generates one.
Which is an advantage of a static factory method over a constructor?
Integer.valueOf caches small values; List.of returns an internal immutable type.
When is the builder pattern the best choice?
It avoids the "telescoping constructor" problem.
Why is calling an overridable method from a constructor risky?
Make such methods private or final, or avoid the call.
If a constructor throws an exception, what does new return?
This is why constructor validation guarantees valid objects.
What happens if A() calls this(1) and A(int x) calls this()?
javac detects constructor cycles.
Timed online tests with instant scores and explanations.