❔Interview Questions
Can constructor have a return type in Java?
No, constructors do not have a return type, not even void. Their purpose is to initialize object state.
What is the difference between this() and super() in constructors?
this() calls another overloaded constructor in the same class. super() calls the parent class constructor.
Can the this keyword be used to refer to instance variables from static methods?
No. The this keyword can only be used in instance methods and constructors, not static methods.
Can a constructor call static methods in Java?
Yes, constructors can call static methods because static methods don't require an instance.
Is super() required to be the first statement in a constructor?
Yes, super() or this() must be the first executable statement in a constructor.
Can variables be initialized using constructor parameters in Java?
Yes, constructor parameters can be used to initialize member variables via assignment.
Does every constructor in Java need to explicitly call super()?
No, if missing the compiler will insert an implicit no-arg super() call as the first statement.
Can a subclass constructor exist without matching constructor arguments in parent class?
Yes, subclass constructors do not have to match parent constructors. super() can use default.
What causes an infinite recursion constructor error?
Calling this() or super() within their own constructor invocation causes infinite recursion.
Are multiple constructors with different arguments considered method overloading?
Yes, overloaded constructors with different signatures are a form of constructor overloading.
Can constructors be declared private in Java?
Yes, private constructors are used to implement singleton classes to prevent direct instantiation.
Can a subclass constructor change accessibility of overridden superclass constructor?
No, overriding constructors in subclasses cannot have broader accessibility than superclass constructors.
What is the output of print(this) statement inside a constructor?
It prints the object hashcode since this refers to the current instance being constructed.
Does using this() call prevent use of super() in a constructor?
No, super() and this() can be used in the same constructor provided super() appears first.
Can a subclass constructor invoke private superclass constructors?
No, private superclass constructors cannot be accessed or invoked from subclass constructors.
Can constructor parameters be final in Java?
Yes, constructor parameters can be declared final to prevent modifications after assignment.
Is super.method() allowed within constructors?
Yes, super.method() can be used in a constructor to invoke superclass methods.
Can constructors be synchronized in Java?
No. Constructors cannot be declared synchronized because they don't have a return type.
Can a constructor directly return a value in Java?
No, constructors cannot return values directly. A factory method would be needed instead.
Can the this keyword be used to call constructors within other constructors?
Yes, this() invokes an overloaded constructor while this refers to the current instance.
Last updated