Menu
Topics Index
...
`

Constructors Test


If you need explanation Read this topic

If you need Answer Take test on this topic

Constructors Test
What will be the output of the following program?
class Constructors {
    public static void main(String[] args) {
        new A(3);
        new B(5, 6);
        new B(6, 7).C(1, 2, 3);
        new C(3, 3, 4).B(4, 5);
        new C(1, 1, 1);
    }
}
class A {
    A(int i) {    print(i * 2);    }
    void print(int i) {    System.out.print(i + " ");    }
}
class B extends A {
    B(int j, int k) {
        super(j);
        print(k * 4);
    }
    void C(int j, int k, int l) {    print(j + k + l - 3);    }
}
class C extends B {
    C(int j, int k, int l) {
        super(j, k);
        print(l - 3);
    }
    void B(int i, int k) {    print(i * 6);    }
}

A. 6 10 24 12 28 2 3 6 12 1 8 20 24 2 4 -2 
B. 6 10 24 12 28 3 6 12 1 24 2 4 -2 
C. Some other output not shown here
D. Compilation Error
Topic: Inheritance And Constructors In Java

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

ans is C...Ans:6 10 24 6 12 1 24 6 12 1 24 2 4 -2

Posted by Ashok Reddi    2014-02-07 13:31:00


Answer is B,
For new A(3) --> 6
For new B(5,6) --> 10 24
For new B(6,7).C(1,2,3) --> 12 28 for B, 3 for C class method
For new C(3,3,4).B(4,5) --> 6 12 1 for C, 24 for class B method
For new C(1,1,1) --> 2 4 -2 will be printed.

Posted by Amar Kumar Biradar    2014-02-07 13:44:40


Congratulations Amar Kumar Biradar. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-02-10 04:01:19


Thanks Merit Campus ;)
Got Recharge :)

Posted by Amar Kumar Biradar    2014-02-10 08:27:27


© meritcampus 2019

All Rights Reserved.

Open In App