What is the return type of a constructor?
Adding void turns it into an ordinary method.
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.
What is the return type of a constructor?
Adding void turns it into an ordinary method.
What must a constructor's name be?
Including the same capitalisation.
When does the compiler provide a default (no-arg) constructor?
Writing any constructor removes it.
What does the compiler-generated default constructor do?
Field initialisers still run as usual.
What is constructor overloading?
The compiler picks one by the arguments passed to new.
On Java 21, where must a this(...) call appear?
And a constructor cannot contain both this(...) and super(...).
Inside an instance method or constructor, what does this refer to?
The object on which the method was called or which is being constructed.
Why do constructors often contain this.name = name;?
Without this, name = name assigns the parameter to itself.
Which modifier can be used on a constructor?
Access modifiers are allowed; static, final and abstract are not.
Which is a typical use of a private constructor?
Math and Collections have private constructors.
Timed online tests with instant scores and explanations.