Java Constructors MCQ - Multiple Choice Questions And Answers
Q. What is a constructor in Java?
A. A method that returns a valueB. 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. voidB. class
C. this
D. new
Q. Can a constructor have a return type in Java?
A. YesB. No
Q. Which of the following is true about parameterized constructors in Java?
A. They have no parametersB. 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. YesB. No
Q. What happens if a class does not have a constructor in Java?
A. The class cannot be compiledB. 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 valuesB. 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. YesB. No
Q. Which keyword is used to call the parent class constructor in a subclass constructor in Java?
A. thisB. 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: nullB.
x: 10 y: HelloC.
x: 0 y: HelloD.
x: 10 y: null