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

   next 30
 Q31. Does Constructor returns the reference of the newly created object ?
Ans. No, New operator actually returns the reference of newly created object. Constructor's purpose is to initialize the default state of the object by setting the initial values of the elements and hence returns nothing.

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

   Like         Discuss         Correct / Improve     Constructor


 Q32. Why doesn't Java even allow void return types for Constructors ?
Ans. Because having a void return type with the Constructor name will make it as a method name. As methods with the Class names are perfectly legit in Java, It needs some way to recognize the constructor and having none return type helps identify that.

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

   Like         Discuss         Correct / Improve     constructors

Try 1 Question(s) Test


 Q33. Is a method name same as Class name permissible in Java ?
Ans. Yes, But the method should have some return type as otherwise it will be treated as a constructor.

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

   Like         Discuss         Correct / Improve     constructor  methods  method names

Try 1 Question(s) Test


 Q34. What is the difference between constructor and instance initialization blocks ?Core Java
Ans. Constructor has the same name as class name whereas instance initialization block just have a body without any name or visibility type.

instance initialization blocks are useful if we want to have some code run regardless of which constructor is used or if we want to do some instance initialization for anonymous classes.

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

   Like         Discuss         Correct / Improve     instance initialization blocks  constructor  constructor vs instance initialization blocks     Asked in 2 Companies

Try 1 Question(s) Test


 Q35. Why do we use a copy constructor ?
Ans. Copy Constructor is used for creating a duplicate of another object. Duplicate object will have the same state of the copied object but will have independent values in different memory locations. Copy Constructor can be useful to create duplicates of immutable objects as the Original cannot be tampered. Moreover It can be useful if base copies are required for individual requests in threading.

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

   Like         Discuss         Correct / Improve     copy constructor  clone  cloning object   immutable  immutability  immutability


 Q36. Have you ever felt the need of keeping the constructor private ?Design
Ans. Yes, When either we don't want an object to be created ( class having all static elements and hence not required ) or object to be created using a static method or static block. One such example could be a situation when we would like app to load objects at start up and we would like to restrict new object creation by any request.

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

   Like         Discuss         Correct / Improve     private constructor  constructor


 Q37. Is it mandatory to have a default constructor ?Core Java
Ans. No.

Default constructor is provided by compiler if no constructor is provided ( argument or no argument ) by us.

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

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

Try 1 Question(s) Test


 Q38. Why Java doesn't provide a default constructor if we define an argument constructor ?Core Java
Ans. Java provides a default constructor ( no argument ) if no constructor is provided by the programmer. Java makes a check during pre compilation if there is any constructor provided by the programmer. If any constructor is provided, Java understands that programmer has provided all the mechanism for initialization and construction of the object and may not even intend to create objects by calling a no argument constructor. For example - One may like to have objects created with self defined state otherwise restrict its creation.

We make the constructor private if we want to make a class singleton as we would like to restrict creation of new objects using new operator and default constructor. Similar could be the situation wherein we would like to construct objects in a particular manner only.

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

   Like         Discuss         Correct / Improve     default constructor      expert


 Q39. Is it a good Design practice to call methods from a constructor ?Design
Ans. Constructor's objective is to initialize member elements. If we don't deviate from that and don't have methods with logic in them, I don't see an issue with constructor's calling other methods assuming the methods are doing nothing but initialization. There are situations wherein we classify the member elements based on their type and hence having different initialization methods for different element types will give a better abstracted way to initialize them.

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

   Like         Discuss         Correct / Improve     design constructor


 Q40. Is it a good design practice to have programming constructs like if and loops in constructor ?Design
Ans. I would avoid that. If we need to initialize member elements differently on the basis of some condition, I would prefer having overloaded constructors. I don't see a need to have a loop for initializing member elements unless the count of elements is huge and they all need to be initialized with common value.

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

   Like         Discuss         Correct / Improve     design constructor  for loop


 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


previous 30   next 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: