Search Tutorials


Java Constructors MCQ - Multiple Choice Questions And Answers | JavaInUse

Java Constructors MCQ - Multiple Choice Questions And Answers

Q. What is a constructor in Java?

A. A method that returns a value
B. A special type of method that is used to initialize objects
C. A method to perform mathematical calculations
D. A method that is used to print output

Q. Which keyword is used to declare a constructor in Java?

A. void
B. class
C. this
D. new

Q. Can a constructor have a return type in Java?

A. Yes
B. No

Q. Which of the following is true about parameterized constructors in Java?

A. They have no parameters
B. They can have only one parameter
C. They can have multiple parameters
D. They can only be used for static methods

Q. Which of the following is not a valid constructor declaration?

A. public MyClass()
B. private void MyClass()
C. protected MyClass(int x)
D. public MyClass(int x, int y)

Q. Can a constructor call another constructor of the same class in Java?

A. Yes
B. No

Q. What happens if a class does not have a constructor in Java?

A. The class cannot be compiled
B. The compiler automatically provides a default constructor
C. The class cannot be instantiated
D. The class can only have static methods

Q. What is the purpose of a parameterized constructor in Java?

A. To initialize the class variables to default values
B. To create multiple instances of the same class
C. To initialize an object with specific values
D. To restrict access to the class

Q. Can a constructor have access modifiers in Java?

A. Yes
B. No

Q. Which keyword is used to call the parent class constructor in a subclass constructor in Java?

A. this
B. class
C. super
D. new

Q. Which of the following is the correct way to define a parameterized constructor in Java?

A.
class MyClass {
    int num;
    
    public MyClass() {
        num = 0;
    }
}
B.
class MyClass {
    int num;
    
    public MyClass(int n) {
        num = n;
    }
}
C.
class MyClass {
    int num;
    
    public MyClass {
        num = 0;
    }
}
D.
class MyClass {
    int num;
    
    public void MyClass() {
        num = 0;
    }
}

Q. What will be the output of the following code snippet?

public class Example {
    int x;
    String y;
    
    public Example() {
        x = 10;
        y = "Hello";
    }
    
    public static void main(String[] args) {
        Example ex = new Example();
        System.out.println("x: " + ex.x);
        System.out.println("y: " + ex.y);
    }
}
A.
x: 0
y: null
B.
x: 10
y: Hello
C.
x: 0
y: Hello
D.
x: 10
y: null





Q. Which of the following statements about constructors in Java is true?

A. Constructors can be inherited by subclasses.
B. Constructors can have a return type.
C. A class can have more than one constructor with the same parameter types.
D. Constructors can be made final to prevent subclassing.

Q. How can you invoke one constructor from another constructor in the same class?

A. Using the this() keyword.
B. Using the super() keyword.
C. Using the invoke() keyword.
D. Using the new() keyword.

Q. Consider the following code snippet. What will be the output when this code is executed?

class MyClass {
    int num;
    
    public MyClass(int n) {
        num = n;
    }
    
    public void display() {
        System.out.println("Number: " + num);
    }
}

public class Main {
    public static void main(String[] args) {
        MyClass obj = new MyClass(5);
        obj.display();
    }
}
A. Number: 0
B. Number: 5
C. Compilation Error
D. Runtime Error

Q. Which of the following statements is true about default constructors in Java?

A. Default constructors are automatically added by the compiler if no other constructors are defined.
B. Default constructors must have a return type.
C. Default constructors can have parameters.
D. Default constructors can be inherited by subclasses.

Q. Which of the following is true about constructors in Java?

A. Constructors can be final.
B. Constructors can be static.
C. Constructors can have a return type.
D. Constructors can be overloaded.

Q. What will be the output of the following code snippet?

public class Example {
    int x;
    
    public Example() {
        this(10);
    }
    
    public Example(int x) {
        this.x = x;
    }
    
    public static void main(String[] args) {
        Example ex = new Example();
        System.out.println(ex.x);
    }
}
A. 0
B. 10
C. Compilation Error
D. Runtime Error

Q. Which of the following is true about constructor chaining in Java?

A. Constructor chaining refers to multiple constructors calling each other in a loop.
B. Constructor chaining is not allowed in Java.
C. Constructor chaining can only be achieved using the super() keyword.
D. Constructor chaining allows one constructor to call another constructor in the same class.

Q. What is the purpose of a constructor in Java?

A. To initialize the instance variables of a class.
B. To define methods that operate on the instance variables.
C. To provide an entry point for the execution of a Java program.
D. To control the access level of a class.