Question discussion

What will be the output of the following code?

359436Medium DSSSB TGT Computer Science 0 replies 0 views
0
What will be the output of the following code?
class Test {
    Test(int x) {
        System.out.println("Parameterized Constructor");
    }
    Test() {
        this(10);
        System.out.println("Default Constructor");
    }
    public static void main(String[] args) {
        Test obj = new Test();
    }
}
  • AParameterized Constructor followed by Default Constructor
  • BCompilation Error
  • CDefault Constructor
  • DParameterized Constructor
Correct answer: A

Explanation

new Test() calls Test(), whose first statement this(10) calls the parameterised constructor first. It prints "Parameterized Constructor" and then "Default Constructor".

Discussion 0

Nobody has replied yet. Start the discussion!