Constructors Interview Questions

    Question 1JAVA OOPS - Default Constructor

    What does Java provide if no constructor is defined in a class?

    class Person {}

    Question 2JAVA OOPS - Parameterized Constructor

    Which statement correctly creates a parameterized constructor object?

    class Car { String model; Car(String m) { model = m; } }

    Question 3JAVA OOPS - Constructor Overloading

    What will be printed?

    class Demo { Demo() { System.out.println("Default"); } Demo(int x) { System.out.println("Param"); } } public class Main { public static void main(String[] args) { Demo d1 = new Demo(); Demo d2 = new Demo(10); } }

    Question 4JAVA OOPS - Constructor Without `void`

    Which is true about constructors?

    Question 5JAVA OOPS - `this()` Keyword in Constructor

    What will the following code print?

    class Test { Test() { System.out.println("Default"); } Test(int x) { this(); System.out.println(x); } } public class Main { public static void main(String[] args) { Test t = new Test(5); } }

    Question 6JAVA OOPS - Constructor Chaining

    Which concept is demonstrated here?

    class Demo { Demo() { System.out.println("A"); } Demo(int x) { this(); System.out.println("B"); } }

    Question 7JAVA OOPS - Object Initialization

    Which is true about objects in Java?

    Question 8JAVA OOPS - Private Constructor

    What is true about a private constructor?

    class Demo { private Demo() {} }

    Question 9JAVA OOPS - Constructor in Inheritance

    What will be printed?

    class A { A() { System.out.println("A"); } } class B extends A { B() { System.out.println("B"); } } public class Main { public static void main(String[] args) { B obj = new B(); } }

    Question 10JAVA OOPS - No-argument Constructor with Parameters

    Will this code compile?

    class Test { Test(int x) {} } public class Main { public static void main(String[] args) { Test t = new Test(); } }