Menu
Topics Index
...
`

Creating Class with multiple methods1756


If you need explanation Read this topic

If you need Answer Take test on this topic

Creating Class with multiple methods1756
What will be the output of the following program?
class GoodBoy {
    int a;
    private final int b;
    static int c = 7;
    GoodBoy() {
        a = 77;
        b = 55;
        c = 33;
    }
    public void getBoy() {
        System.out.print("-GoodBoy-");
    }
}
class GoodGirl extends GoodBoy {
    int a;
    private final int b;
    static int c = 40;
    GoodGirl() {
        a = 33;
        b = 22;
        c = 11;
    }
    public void getBoy() {
        System.out.print("-GoodGirl-");
    }
}
public class SolveIt {
    public static void main(String arg[]) {
        GoodBoy a = new GoodBoy();
        GoodBoy b = new GoodGirl();
        a.getBoy();
        b.getBoy();
        System.out.print("-" + GoodBoy.c + "-");
        System.out.print("-" + GoodBoy.c + "-");
    }
}

A. -GoodBoy--GoodGirl--7--7-
B. -GoodBoy--GoodGirl--33--11-
C. -GoodBoy--GoodGirl--33--33-
D. -GoodBoy--GoodGirl--40--40-
E. Some other output
F. Compilation Error or Runtime Error
Topic: Static Keyword 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
------Value of variable c is 33 in class goodboy so goodboy.c=33
-----so first a.getBoy() will print---- "goodboy"
------then b.getBoy() will print ----"goodgirl" as reference of goodgirl is assigned to goodboy.

so output is " -goodboy--goodgirl--33--33-"

Posted by Manisha Ahuja    2014-07-18 13:29:42


Congratulations Rohan Sharma. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-07-21 04:18:28


© meritcampus 2019

All Rights Reserved.

Open In App