โ”Interview Questions

  1. 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.

  1. 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.

  1. 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.

  1. Can a constructor call static methods in Java?

Yes, constructors can call static methods because static methods don't require an instance.

  1. Is super() required to be the first statement in a constructor?

Yes, super() or this() must be the first executable statement in a constructor.

  1. Can variables be initialized using constructor parameters in Java?

Yes, constructor parameters can be used to initialize member variables via assignment.

  1. 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.

  1. 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.

  1. What causes an infinite recursion constructor error?

Calling this() or super() within their own constructor invocation causes infinite recursion.

  1. Are multiple constructors with different arguments considered method overloading?

Yes, overloaded constructors with different signatures are a form of constructor overloading.

  1. Can constructors be declared private in Java?

Yes, private constructors are used to implement singleton classes to prevent direct instantiation.

  1. Can a subclass constructor change accessibility of overridden superclass constructor?

No, overriding constructors in subclasses cannot have broader accessibility than superclass constructors.

  1. 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.

  1. 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.

  1. Can a subclass constructor invoke private superclass constructors?

No, private superclass constructors cannot be accessed or invoked from subclass constructors.

  1. Can constructor parameters be final in Java?

Yes, constructor parameters can be declared final to prevent modifications after assignment.

  1. Is super.method() allowed within constructors?

Yes, super.method() can be used in a constructor to invoke superclass methods.

  1. Can constructors be synchronized in Java?

No. Constructors cannot be declared synchronized because they don't have a return type.

  1. Can a constructor directly return a value in Java?

No, constructors cannot return values directly. A factory method would be needed instead.

  1. 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