JAVA Question Bank
Multiple-Choice Questions (With Single and Multi-correct Questions)
1. Select all the valid statements about constructors:
A) Constructors can be static.
B) Constructors cannot be inherited.
C) Constructors can have the same name as methods.
D) Constructors can be overloaded.
2. What will be the output of the code?
class Test {
Test() {
[Link]("Default Constructor");
}
Test(int x) {
[Link]("Parameterized Constructor");
}
}
public class Main {
public static void main(String[] args) {
Test t = new Test(5);
}
}
A) Default Constructor
B) Parameterized Constructor
C) Runtime Error
D) No output
3. Which of the following are true for constructor overloading?
A) Overloading can be done by changing the number of parameters.
B) Overloading can be done by changing the types of parameters.
C) Overloading can be done by changing the order of parameters.
D) Overloading can be done by changing the return type.
4. What will be the output of the code?
class Test {
Test() {
[Link]("Constructor called");
}
void Test() {
[Link]("Method called");
}
}
public class Main {
public static void main(String[] args) {
Test t = new Test();
}
}
1
JAVA Question Bank
A) Constructor called
B) Method called
C) Constructor called followed by Method called
D) No output
5. Which of the following are valid ways to call a constructor in Java?
A) Using the `new` keyword
B) Using the `super` keyword
C) Using the `this` keyword
D) Using a method call
6. What will be the output of the code?
class Test {
Test() {
[Link]("Constructor 1");
}
Test(int x) {
this();
[Link]("Constructor 2");
}
}
public class Main {
public static void main(String[] args) {
Test t = new Test(5);
}
}
A) Constructor 1
B) Constructor 2
C) Constructor 1 followed by Constructor 2
D) Runtime Error
7. Which of the following are valid ways to declare a constructor?
A) public MyClass() {}
B) MyClass void() {}
C) public void MyClass() {}
D) private MyClass() {}
8. What will be the output of the code?
class A {
A() {
[Link]("Constructor A");
}
2
JAVA Question Bank
}
class Sample {
Sample() {
[Link]("Default");
}
Sample(int x, int y) {
[Link](x + " " + y);
}
}
public class Main {
public static void main(String[] args) {
Sample obj = new Sample(10, 20);
}
}
A) Default
B) 10 20
C) Compiler Error
D) Runtime Error
9. Select the true statements about constructor overloading:
A) Overloaded constructors can have the same parameter list.
B) Overloaded constructors must have diMerent parameter lists.
C) Overloaded constructors can be called using `this()`.
D) Overloaded constructors can be called using `super()`.
10. Which of the following is a key diHerence between constructors and methods?
A) Constructors initialize an object; methods perform actions.
B) Methods initialize an object; constructors perform actions.
C) Constructors and methods are the same.
D) Methods cannot be overloaded, but constructors can.
11. Which of the following are characteristics of constructors?
A) They cannot be static.
B) They can be abstract.
C) They are called automatically.
D) They cannot be final.
12. What will be the output of the code?
3
JAVA Question Bank
class A {
A() {
[Link]("A");
}
}
class B extends A {
B() {
[Link]("B");
}
}
class C extends B {
C() {
[Link]("C");
}
}
public class Main {
public static void main(String[] args) {
C obj = new C();
}
}
A) A B
B) C B A
C) B A C
D) A B C
13. Which of the following code snippets demonstrates constructor overloading?
1.
class Demo {
Demo(int x) {}
Demo(String s) {}
}
2.
class Demo {
Demo(int x) {}
void Demo(String s) {}
}
3.
class Demo {
Demo(int x) {}
Demo(int x, int y) {}
}
4.
class Demo {
Demo() {}
Demo() {}
}
A. 1 and 3
4
JAVA Question Bank
B. 1 and 2
C. Only 3
D. 1, 2, and 3
14. What will be the output of the following code?
class Alpha {
Alpha() {
[Link]("No-arg Constructor");
}
Alpha(String msg) {
this();
[Link]("Parameterized Constructor: " + msg);
}
public static void main(String[] args) {
new Alpha("Hello");
}
}
A. Parameterized Constructor: Hello
B. No-arg Constructor
Parameterized Constructor: Hello
C. Compilation Error
D. None of the above
15. What happens if a constructor explicitly calls another constructor but misses the `this()`
keyword?
A. Recursion occurs
B. Runtime Error
C. Compilation Error
D. None of the above
16. Identify the correct behaviour of constructors in inheritance.
class Parent {
Parent() {
[Link]("Parent Constructor");
}
}
class Child extends Parent {
Child() {
[Link]("Child Constructor");
}
public static void main(String[] args) {
Child c = new Child();
}
}
A. Child Constructor
B. Parent Constructor
Child Constructor
5
JAVA Question Bank
C. Compilation Error
D. None of the above
17. What is the output of the following code?
class Delta {
Delta() throws Exception {
[Link]("Constructor with Exception");
}
public static void main(String[] args) throws Exception {
new Delta();
}
}
A. Compilation Error
B. Constructor with Exception
C. Runtime Exception
D. None of the above
18. What is the output of the following code?
class Zeta {
Zeta(int x) {
[Link]("Zeta Constructor: " + x);
}
}
class SubZeta extends Zeta {
SubZeta() {
super(100);
[Link]("SubZeta Constructor");
}
public static void main(String[] args) {
new SubZeta();
}
}
A. Runtime error
B. SubZeta Constructor
Zeta Constructor: 100
C. Compilation Error
D. Zeta Constructor: 100
SubZeta Constructor
19. What will be the output of the code?
class Example {
Example(int x) {
[Link]("Parameterized Constructor: " + x);
}
6
JAVA Question Bank
Example() {
this(5);
[Link]("Default Constructor");
}
public static void main(String[] args) {
new Example();
}
}
A) Parameterized Constructor: 5
Default Constructor
B) Default Constructor
Parameterized Constructor: 5
C) Compilation Error
D) Runtime Error
20. What will be the output of the above code?
class Gamma {
Gamma(int a, int b) {
[Link]("Sum: " + (a + b));
}
Gamma() {
this(5, 10);
[Link]("Default Constructor");
}
public static void main(String[] args) {
new Gamma();
}
}
A) Default Constructor
Sum: 15
B) Sum: 15
Default Constructor.
C) Compilation Error
D) None of the above
21. Which of the following statements about constructor chaining are true?
A) Constructor chaining can be achieved using this().
B) Constructor chaining can be achieved using super().
C) Constructor chaining must start from the parameterized constructor.
D) Constructor chaining can lead to infinite loops if not handled properly.
22. What will happen if this() and super() are used in the same constructor?
A) Runtime Exception
B) Only this() will execute
7
JAVA Question Bank
C) Only super() will execute
D) Compilation Error
23. Constructor chaining is the process of calling one constructor from another constructor.
True/False
24. Private constructors can be used to prevent object creation from outside the class.
True/False
25. Constructors can be overloaded but not overridden. True/False
26. A constructor can directly invoke a superclass constructor. True or False