Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

1.

If Class Has Explicit Constructor , Will It Has Default Constructor?

Answer»

No. COMPILER PLACES DEFAULT CONSTRUCTOR only if there is no EXPLICIT constructor.

No. compiler places default constructor only if there is no explicit constructor.

2.

When Developer Must Provide Constructor Explicitly?

Answer»

If we want do execute some logic at the TIME of OBJECT creation, that logic MAY be object initialization logic or some other useful logic.

If we want do execute some logic at the time of object creation, that logic may be object initialization logic or some other useful logic.

3.

When Compiler Provides Default Constructor?

Answer»

Only if there is no EXPLICIT CONSTRUCTOR DEFINED by DEVELOPER.

Only if there is no explicit constructor defined by developer.

4.

What Is Default Accessibility Modifier Of Default Constructor?

Answer»

It is ASSIGNED from its CLASS.

It is assigned from its class.

5.

Why Compiler Given Constructor Is Called As Default Constructor?

Answer»

Because it obtain all its default properties from its class.

They are:

  1. Its accessibility MODIFIER is same as its class accessibility modifier 
  2. Its NAME is same as class name.
  3. Its does not have parameters and LOGIC.

Because it obtain all its default properties from its class.

They are:

6.

Is Constructor Definition Is Mandatory In Class?

Answer»

No, it is OPTIONAL . If we do not DEFINE a CONSTRUCTOR COMPILER will define a DEFAULT constructor.

No, it is optional . If we do not define a constructor compiler will define a default constructor.

7.

Can We Declare Constructor As Private?

Answer»
  1. Yes we can declare constructor as PRIVATE.
  2. All four ACCESS modifiers are ALLOWED to constructor.
  3. We should declare constructor as private for not to ALLOW user to create object from outside of our class.
  4. BASICALLY we will declare private constructor in Singleton design pattern.
  5. Read more at Can we create private constructor in java

8.

Why Constructor Name Is Same As Class Name?

Answer»
  • Every class OBJECT is created using the same NEW keyword , so it must have information about the class to which it must CREATE object .
  • For this REASON constructor name should be same as class name.

9.

Why Return Type Is Not Allowed For Constructor?

Answer»

As there is a possibility to DEFINE a METHOD with same CLASS name , RETURN type is not allowed to constructor to DIFFERENTIATE constructor block from method block.

As there is a possibility to define a method with same class name , return type is not allowed to constructor to differentiate constructor block from method block.

10.

How Compiler And Jvm Can Differentiate Constructor And Method Invocations Of Both Have Same Class Name?

Answer»

By USING new keyword, if new keyword is USED in calling then CONSTRUCTOR is EXECUTED else method is executed.

By using new keyword, if new keyword is used in calling then constructor is executed else method is executed.

11.

How Compiler And Jvm Can Differentiate Constructor And Method Definitions Of Both Have Same Class Name?

Answer»

By USING return TYPE , if there is a return type it is considered as a METHOD else it is considered as CONSTRUCTOR.

By using return type , if there is a return type it is considered as a method else it is considered as constructor.

12.

If We Place Return Type In Constructor Prototype Will It Leads To Error?

Answer»

No, because COMPILER and JVM CONSIDERS it as a METHOD.

No, because compiler and JVM considers it as a method.

13.

Can We Define A Method With Same Name Of Class?

Answer»

Yes, it is allowed to DEFINE a METHOD with same class name. No compile time error and no runtime error is raised, but it is not RECOMMENDED as PER coding standards.

Yes, it is allowed to define a method with same class name. No compile time error and no runtime error is raised, but it is not recommended as per coding standards.

14.

What Are The Rules In Defining A Constructor?

Answer»
  • Constructor name should be same as class name.
  • It should not contain RETURN type.

It should not contain Non Access Modifiers:

  • final ,static, abstract, synchronized
  • In it logic return statement with value is not allowed.

It can have all four accessibility modifiers:

It can have throws CLAUSE:

  • we can throw exception from constructor.
  • It can have logic, as part of logic it can have all java legal statement EXCEPT return statement with value.
  • We can not place return in constructor.

It should not contain Non Access Modifiers:

It can have all four accessibility modifiers:

It can have throws clause:

15.

Define Constructor?

Answer»
  • CONSTRUCTOR is a SPECIAL method given in OOP LANGUAGE for creating and initializing OBJECT.
  • In java , constructor ROLE is only initializing object , and new keyword role is crating object.