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

   
 Q41. Can a constructor have a private access specifier ?Core Java
Ans. Yes, declaring a constructor private means disabling the creation of object that way using new operator.

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

   Like         Discuss         Correct / Improve     constructor  private constructor


 Q42. What is the use of constructors in Java ?Core Java
Ans. Constructors are used to initialize the state of an object. If there are no constructor , objects won't have any initialized state and hence it's elements may contain garbage values. If the memory were used as is, the behavior of the object would also be unpredictable.

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

   Like         Discuss         Correct / Improve     constructor     Asked in 5 Companies


 Q43. What are the trade offs between public constructor and static final method ?Core Java
Ans. Public constructor is simple and easy as it's the default way of object creation. So there are no additional coding overheads as compiler provides the default constructor if none is provided by coder.

With static final methods, it facilitates loose coupling by segregating the responsibility of object creation to a separate method. Validation can be done on the constructor arguments before calling it. Moreover if any adaption on the arguments is required that can achieved easily with factory method.On the flip side, there is coding overhead and additional method call.

  Sample Code for constructor

  Sample Code for factory

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

   Like         Discuss         Correct / Improve     constructor  factory design pattern  factory method


 Q44. Does all methods need to have a return type ? Is it applicable to constructors too ?Core Java
Ans. All methods are not expected to return something but Yes, all methods are expected to have a return type. If a method returns nothing, it can be declared with the return type void.

Constructors are not expected to have any return types , not even void.

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

   Like         Discuss         Correct / Improve     return type   method  constructor


 Q45. What is the difference between the 2 methods in Java ?

Class BuggyBread {

BuggyBread(){
}
void BuggyBread(){
}
}
Core Java
Ans. BuggyBread method without any return type is the constructor which get's called upon object creation whereas BuggyBread method with return type of void is just another method that needs to be called explicitly for it's invocation.

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

   Like         Discuss         Correct / Improve     method  return type  constructor


 Q46. Is this constructor overloading ?

What is the difference between the 2 methods in Java ?

Class BuggyBread {

BuggyBread(){
}
void BuggyBread(int x){
}
}
Core Java
Ans. No, first method is a constructor whereas the second method is just a normal method. There is no way a constructor can be called explicitly and hence all explicit calls to BuggyBread() would result in compilation error.

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

   Like         Discuss         Correct / Improve     constructor  overloading


 Q47. Can we call a constructor explicitly , just like a method ?Core Java
Ans. No we cannot call a constructor like that.

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

   Like         Discuss         Correct / Improve     constructor


 Q48. Can we call a constructor for an object twice ?Core Java
 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     constructor


 Q49. What will be the output upon executing main method

public class Graph {
   private static Multimap<Integer,Integer> adjacentDirectedNodesMap = ArrayListMultimap.create();
   
   Graph(){
      adjacentDirectedNodesMap.put(1, 2);
      adjacentDirectedNodesMap.put(1, 3);
      adjacentDirectedNodesMap.put(1, 4);
      adjacentDirectedNodesMap.put(3, 5);
      adjacentDirectedNodesMap.put(4, 5);
   }
   
   public static void main(String[] args){
      ArrayList visited = new ArrayList();
      
      Integer startNode = 1;
      
      for(Integer adjacentNodes: adjacentDirectedNodesMap.get(startNode)){
            System.out.println(adjacentNodes);
      }
   }   
}
Core Java
Ans. Nothing as we haven't yet created an instance and hence adjacentDirectedNodesMap won't be initialized with any value and hence is empty.

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

   Like         Discuss         Correct / Improve     constructor


 Q50. How do you know if a method is a constructor ?Core Java
Ans. Is the method name same as class Name - Yes

Does the method have any return type ( even void ) - No

It's a constructor

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

   Like         Discuss         Correct / Improve     constructor


 Q51. Can we instantiate the object of derived class if parent constructor is private ?Core Java
Ans. No

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

   Like         Discuss         Correct / Improve     constructor  private constructor


 Q52. How can we make objects if the constructor is private ? Core Java
 This question was recently asked at 'Nagravision'.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     constructor     Asked in 1 Companies


 Q53. Can we declare constructor inside an interface ? Why ?Core Java
 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     interface   constructor


 Q54. Will it print the message in Base Class constructor if we execute main method of Main class ? why ?

public class BaseClass {
BaseClass(){
System.out.println("Hello I am in Base Class Constructor");
}
}

public class DerivedClass extends BaseClass{
}

public class Main {

public static void main(String[] args) {
    DerivedClass derivedClass = new DerivedClass();
}
}
Core Java
Ans. Yes.

When the Derived class constructor is initialized , a no argument super will be called intrinsically.

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

   Like         Discuss         Correct / Improve     constructor


 Q55. What will the following print upon executing main method of Main class ?

public class BaseClass {
BaseClass(){
System.out.println("Hello I am in Base Class Constructor");
}
}

public class DerivedClass extends BaseClass{
DerivedClass(){
System.out.println("Hello I am in Derived Class Constructor");
}
}

public class Main {

public static void main(String[] args) {
    DerivedClass derivedClass = new DerivedClass();
}
}
Core Java
Ans. Hello I am in Base Class Constructor
Hello I am in Derived Class Constructor

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

   Like         Discuss         Correct / Improve     constructor


 Q56. What will the following code print upon executing main method of Main class ?

public class BaseClass {
BaseClass(){
System.out.println("Hello I am in Base Class Constructor");
}
}

public class DerivedClass extends BaseClass{
DerivedClass(){
System.out.println("Hello I am in Derived Class Constructor");

super();
}
}

public class Main {

public static void main(String[] args) {
    DerivedClass derivedClass = new DerivedClass();
}
}

Core Java
Ans. There will be compilation error within constructor of Derived Class as "super must be the first statement in constructor body".

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

   Like         Discuss         Correct / Improve     constructor


 Q57. What will the following code print upon executing main method of Main class ?

public class BaseClass {
BaseClass(int x){
System.out.println("Hello I am in Base Class Constructor");
}
}

public class DerivedClass extends BaseClass{
DerivedClass(){
System.out.println("Hello I am in Derived Class Constructor");
}
}

public class Main {

public static void main(String[] args) {
    DerivedClass derivedClass = new DerivedClass();
}
}
Core Java
Ans. Compilation error as there is no default constructor available in BaseClass.

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

   Like         Discuss         Correct / Improve     constructor


 Q58. What will the following code print upon executing main method of Main class ?

public class BaseClass {
BaseClass(int x){
System.out.println("Hello I am in Base Class Constructor");
}
}

public class DerivedClass extends BaseClass{
DerivedClass(){
super(1);
System.out.println("Hello I am in Derived Class Constructor");
}
}

public class Main {

public static void main(String[] args) {
    DerivedClass derivedClass = new DerivedClass();
}
}
Core Java
Ans. Hello I am in Base Class Constructor
Hello I am in Derived Class Constructor

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

   Like         Discuss         Correct / Improve     constructor


 Q59. What will the following code print upon executing main method of Main class

public class BaseClass {

BaseClass(){
this(2);
System.out.println("Hello I am in Base Class Constructor");
}

BaseClass(int i){
System.out.println("Hello I am in Base Class int argument Constructor");
}
}

public class DerivedClass extends BaseClass{
DerivedClass(){
super(1);
System.out.println("Hello I am in Derived Class Constructor");
}
}

public class Main {

public static void main(String[] args) {
DerivedClass derivedClass = new DerivedClass();
}
}
Core Java
Ans. Hello I am in Base Class int argument Constructor
Hello I am in Base Class Constructor
Hello I am in Derived Class Constructor

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

   Like         Discuss         Correct / Improve     constructor


 Q60. What will the following code print upon executing main method of Main class

public class BaseClass {

BaseClass(){
this(2);
System.out.println("Hello I am in Base Class Constructor");
}

BaseClass(int i){
System.out.println("Hello I am in Base Class int argument Constructor");
}
}

public class DerivedClass extends BaseClass{
DerivedClass(){
System.out.println("Hello I am in Derived Class Constructor");
}
}

public class Main {

public static void main(String[] args) {
    DerivedClass derivedClass = new DerivedClass();
}
}
Core Java
Ans. Hello I am in Base Class int argument Constructor
Hello I am in Base Class Constructor
Hello I am in Derived Class Constructor

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

   Like         Discuss         Correct / Improve     constructor


 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

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: