Core Java - Interview Questions and Answers for 'Constructor' | Search Interview Question - javasearch.buggybread.com
Javasearch.buggybread.com

Search Interview Questions


 More than 3000 questions in repository.
 There are more than 900 unanswered questions.
Click here and help us by providing the answer.
 Have a video suggestion.
Click Correct / Improve and please let us know.
Label / Company      Label / Company / Text

   



Interview Questions and Answers - Order By Newest

   
 Q61. Can constructors be final ?Core Java
Ans. No. They are never inherited and therefore are not subject to hiding or overriding.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     constructor  final  final constructor     Asked in 1 Companies


 Q62. Can a constructor be declared static ? Why ?Core Java
Ans. No.

When we declare a method static, it means that "this belongs to class as whole and not particular instance". The whole purpose of constructor is to initialize a object and hence there is no sense having static constructor.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     constructor  static  static constructor     Asked in 1 Companies


 Q63. Does abstract class have public constructor? Core Java
Ans. Yes, an abstract class can have a constructor in Java.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     abstract class  public constructor     Asked in 1 Companies


 Q64. Can we have more than two constructors in a class ?Core Java
Ans. Yes, through constructor overloading

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     constructor overloading


 Q65. What is the role of "?" in TypeScript constructors ?TypeScript
 This question is still unanswered. Can you please provide an answer.


 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     Typescript constructor


 Q66. What is a default constructor ?Core Java
a. Constructor without parameters declared by user
b. Constructor provided by Java if no constructor is declared
c. Constructor with empty body
d. All of the above

Ans.b. Constructor provided by Java if no constructor is declared

 Q67. How can we create objects if we make the constructor private ?Core Java
a. We can't create objects if constructor is private
b. We can only create objects if we follow singleton pattern
c. We can only create one object
d. We can create new object through static method or static block

Ans.d. We can create new object through static method or static block

 Q68. With the following code, Which is a valid way to initialize ?
public class BuggyBread {

   private String element1;

   private String element2;

   private BuggyBread(String element1, String element2){
      this.element1 = element1;
      this.element2 = element2;
   }

   public static class Builder {
   
      private String element1;

      private String element2;

      Builder(BuggyBread buggybread){
         element1 = buggybread.element1;
         element2 = buggybread.element2;
      }

      Builder withElement1(String element1){
         this.element1 = element1;
         return this;
      }

      Builder withElement2(String element2){
         this.element2 = element2;
         return this;
      }

      BuggyBread build(){
         BuggyBread buggybread = new BuggyBread(element1,element2);
         return buggybread;
      }
   }
}
Core Java
a. BuggyBread buggybread = new BuggyBread();
b. BuggyBread buggybread = new BuggyBread("element1","element2");
c. BuggyBread.Builder builder = new BuggyBread.Builder();
d. BuggyBread.Builder builder = new BuggyBread.Builder("element1","element2");

Ans.d. BuggyBread.Builder builder = new BuggyBread.Builder("element1","element2");

 Q69. What will be the output of following code ?

public class BuggyBread {
   
   private int x;
   private int y;
   
   BuggyBread(int x,int y){};
   
   public static void main(String[] args){
      BuggyBread buggybread = new BuggyBread();
      System.out.println(buggybread.x);
   }
}
Core Java
a. 0
b. null
c. compilation error due to uninitialized element
d. compilation error due to constructor

Ans.d. compilation error due to constructor

 Q70. What will be the output of following code ?

public class BuggyBread {

   private int x;
   private Integer y;

   private BuggyBread(int x,int y){};

   public static void main(String[] args){
      BuggyBread buggybread = new BuggyBread(1,2);
      System.out.println(buggybread.x);
   }
}
Core Java
a. compilation error due to private constructor
b. compilation error due to uninitialized elements
c. 0 null
d. 0 0

Ans.c. 0 null

 Q71. What is the problem with this code ?

public class Car extends Vehicle{   
   int x;
   
   Car(int y){
      x = 5;
   }
   
   Car(){
      this(5);
      super();
   }
}
Core Java
a. We cannot overload constructors
b. Constructors should have type
c. we should have called super() before this(5)
d. We cannot have both super() and this() in a constructor

Ans.d. We cannot have both super() and this() in a constructor

 Q72. What is the problem with this code ?

public class Car extends Vehicle{
   int x;

   Car(int y){
      x = 5;
   }

   Car(){
      super();
      this.x = 5;
   }
}
Core Java
a. We cannot overload constructors
b. We cannot call super in overloaded constructor
c. we cannot have this and super in constructor
d. There is no problem

Ans.d. There is no problem

 Q73. The following code is an example of

public class Car extends Vehicle{
   int x;

   Car(int y){
      x = 5;
   }

   Car(){
      this(5);
   }
}
Core Java
a. Constructor Overloading
b. Constructor Chaining
c. Both Constructor Overloading and Chaining
d. None of above

Ans.c. Both Constructor Overloading and Chaining

 Q74. What is the problem with following code ?

public class Car extends Vehicle{
   int x;

   void Car(int y){
      x = 5;
   }

   void Car(){
      this(5);
   }
}
Core Java
a. We cannot use this() within normal method
b. We cannot chain methods
c. We cannot overload constructors
d. We cannot use return type void with overloaded methods

Ans.a. We cannot use this() within normal method

 Q75. Default Constructor is provided by Java , if ..Core Java
a. No constructor is provided by coder
b. No argument constructor is not provided by Coder
c. No Constructor is provided by coder initializing fields to null and primitive default values
d. No argument constructor is provided by coder

Ans.a. No constructor is provided by coder

 Q76. Default Constructor is provided by Java ... Core Java
a. To Reserve Memory
b. To provide at least one instance method
c. To Make it look good
d. To initialize the object state

Ans.d. To initialize the object state

previous 30   

Help us and Others Improve. Please let us know the questions asked in any of your previous interview.

Any input from you will be highly appreciated and It will unlock the application for 10 more requests.

Company Name:
Questions Asked: