Java Constructors

Last Updated :
Discuss
Comments

Question 1

Which of the following is true about Java constructors?

  • A

    Constructors have a return type of void.

  • B

    Constructors must always be public.

  • C

    A constructor is automatically called when an object is created.

  • D

    Constructors can only be defined explicitly.


Question 2

What happens if you do not define a constructor in a Java class?

  • A

    The program will not compile.

  • B

    The compiler provides a default constructor.

  • C

    The class cannot be instantiated.

  • D

    The constructor from another class will be used.


Question 3

Can a Java constructor be private?


  • A

    No, it must be public.

  • B

    Yes, but the class cannot be instantiated outside the class.

  • C

    Yes, and it can be accessed from anywhere.

  • D

    No, Java does not allow private constructors.

Question 4

How can one constructor call another constructor within the same class?

  • A

    Using super()

  • B

    Using this()

  • C

    Using new()

  • D

    Constructors cannot call each other


Question 5

What will be the output of this program?

Java
class Demo {
    int num;
    Demo() {
        this(100);
        System.out.println("Default Constructor");
    }
    Demo(int n) {
        num = n;
        System.out.println("Parameterized Constructor");
    }
}
public class Main {
    public static void main(String[] args) {
        Demo obj = new Demo();
    }
}


  • A

    Default Constructor

  • B

    Parameterized Constructor

  • C

    Parameterized Constructor
    Default Constructor

  • D

    Compilation Error


Question 6

Can a constructor be final in Java?


  • A

    Yes, if you override it.

  • B

    No, because constructors are not inherited.

  • C

    Yes, but only in abstract classes.

  • D

    Yes, but only for utility classes.

Question 7

What will be the output of the following code?

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


  • A

    Base Constructor
    Derived Constructor

  • B

    Derived Constructor
    Base Constructor


  • C

    Compilation Error

  • D

    Runtime Error

Question 8

What will be the output of this program?

Java
class A {
    private A() {
        System.out.println("Private Constructor");
    }
    public static void createInstance() {
        new A();
    }
}
public class Main {
    public static void main(String[] args) {
        A.createInstance();
    }
}


  • A

    Compilation Error

  • B

    Private Constructor

  • C

    Runtime Error


  • D

    No Output

Question 9

What is the purpose of a constructor?


  • A

    Destroy objects

  • B

    Allocate memory

  • C

    Initialize objects

  • D

    Call main() method

There are 9 questions to complete.

Take a part in the ongoing discussion