Constructor in Java Interview Questions and Answers for freshers

100 Java Constructor Interview Questions for Freshers
  1. What is a constructor in Java?

    • Answer: A constructor is a special method in Java that is automatically called when an object of a class is created. It's used to initialize the object's instance variables (fields). The constructor has the same name as the class and doesn't have a return type, not even void.
  2. What is the purpose of a constructor?

    • Answer: The primary purpose is to initialize the object's state. This ensures that the object is in a valid and consistent state when it's first created. It avoids the need to manually set each field after object creation.
  3. How many constructors can a class have?

    • Answer: A class can have multiple constructors, a concept known as constructor overloading. Each constructor must have a unique signature (different number or types of parameters).
  4. Explain constructor overloading.

    • Answer: Constructor overloading is the ability to define multiple constructors within a single class. These constructors must differ in their parameter lists (number or types of parameters). The compiler distinguishes between them based on the arguments provided during object creation.
  5. What is a default constructor?

    • Answer: If you don't explicitly define any constructors in a class, Java provides a default constructor automatically. This default constructor is a no-argument constructor (takes no parameters) that initializes instance variables to their default values (0 for numbers, false for boolean, null for objects).
  6. When is a default constructor not created?

    • Answer: The Java compiler will *not* create a default constructor if you explicitly define any constructor in your class, even if that constructor has parameters.
  7. Explain the difference between a constructor and a method.

    • Answer: A constructor is automatically called when an object is created; a method is explicitly called. Constructors have the same name as the class; methods have different names. Constructors do not have a return type; methods have a return type (even if it's void).
  8. What is a parameterized constructor?

    • Answer: A parameterized constructor is a constructor that takes one or more parameters. These parameters are used to initialize the object's instance variables with values provided at the time of object creation.
  9. Can a constructor be private?

    • Answer: Yes, a constructor can be private. This prevents the creation of objects of that class from outside the class itself. It's often used for utility classes or classes representing singletons.
  10. What is a copy constructor?

    • Answer: A copy constructor is a constructor that creates a new object as a copy of an existing object. It typically takes an object of the same class as a parameter.
  11. How do you create a copy constructor in Java?

    • Answer: You create a copy constructor by defining a constructor that takes an object of the same class as a parameter and copies the values of the instance variables from the parameter object to the newly created object. Shallow vs. deep copying is an important consideration here.
  12. Explain the difference between shallow copy and deep copy.

    • Answer: A shallow copy creates a new object but copies only the references to the objects within the original object. A deep copy creates a new object and recursively copies all the objects within the original object, creating entirely new copies of nested objects.
  13. What is a static constructor?

    • Answer: Java doesn't have static constructors in the same way some other languages do. Static blocks are used to perform initialization tasks related to the class itself, not to individual objects.
  14. Can a constructor call another constructor?

    • Answer: Yes, using "this()" keyword. This is called constructor chaining. The call to `this()` must be the first statement in the constructor.
  15. Explain constructor chaining.

    • Answer: Constructor chaining is when one constructor calls another constructor within the same class, typically to reuse initialization logic. It improves code reusability and reduces redundancy.
  16. What is the `this` keyword in a constructor?

    • Answer: Inside a constructor, `this` refers to the current object being constructed. It's used to access instance variables and call other constructors within the same class.
  17. What is the `super` keyword in a constructor?

    • Answer: In a subclass constructor, `super()` is used to call a constructor of the superclass. It's used to initialize inherited members from the parent class.
  18. What happens if you don't call `super()` explicitly in a subclass constructor?

    • Answer: If you don't explicitly call `super()`, the compiler implicitly adds a call to the no-argument constructor of the superclass. If the superclass doesn't have a no-argument constructor, you'll get a compile-time error.
  19. Write a Java program demonstrating constructor overloading.

    • Answer: ```java public class MyClass { int x; int y; MyClass() { x = 0; y = 0; } MyClass(int a) { x = a; y = 0; } MyClass(int a, int b) { x = a; y = b; } public static void main(String[] args) { MyClass obj1 = new MyClass(); MyClass obj2 = new MyClass(5); MyClass obj3 = new MyClass(5, 10); } } ```
  20. Write a Java program demonstrating constructor chaining.

    • Answer: ```java public class MyClass { int x; int y; MyClass() { this(0, 0); // Calls the parameterized constructor } MyClass(int x, int y) { this.x = x; this.y = y; } public static void main(String[] args) { MyClass obj = new MyClass(); System.out.println(obj.x + ", " + obj.y); // Output: 0, 0 } } ```

Thank you for reading our blog post on 'Constructor in Java Interview Questions and Answers for freshers'.We hope you found it informative and useful.Stay tuned for more insightful content!