Java Constructors (original) (raw)

Which of the following is true about Java constructors?

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

What will be the output of the following Java code?

Java `

class Test { int x; Test() { x = 10; } } public class Main { public static void main(String[] args) { Test obj = new Test(); System.out.println(obj.x); } }

`

Can a Java constructor be private?

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

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(); } }

`

What will happen if you explicitly define a constructor with parameters but do not define a no-argument constructor?

Can a constructor be final in Java?

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(); } }

`

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(); } }

`

There are 10 questions to complete.

Take a part in the ongoing discussion