Menu
Topics Index
...
`

Class composition classes having other classes


If you need explanation Read this topic

If you need Answer Take test on this topic

Class composition classes having other classes
What will be the output of the following program?
public class Mummy {
    public static void main(String[] args) {
        Daddy d = new Daddy();
        String result = "";
        int i = 0;
        result += Daddy.Son("20");
        i++;
        result += d.Daughter();
        i++;
        d.Child(i);
        i++;
        result += GrandFather.Son(result);
        System.out.print(i + " - " + result);
    }
}
class GrandFather extends Daddy {
    public String Son() {
        String hai = "Good Morning";
        Daddy.Son(hai);
        return "";
    }
}
class Daddy {
    public static String Son(String str) {    return "I am " + str + " years old, ";    }
    public String Daughter() {
        String str = Son("25");
        str += "I am 24 years old, ";
        return str;
    }
    public void Child(int i) {
        i++;
        String str = "I am child, ";
    }
}

A. Some other output
B. 4 - I am 20 years old I am 25 years old I am 24 years old I am 20 years old I am 25 years old Good Morning
C. 3 - I am 25 years old
 I am 25 years old
 I am 24 years old
 I am 25 years old
D. 4 - I am 20 years old
 I am 25 years old
 I am 24 years old
 I am child
 I am I am 20 years old
 I am 25 years old
 I am 24 years old
  I am Good Morning years old
E. 3 - I am 25 years old I am 25 years old I am 24 years old I am 25 years old
F. Compilation Error or Runtime Error
Topic: Unknown

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.

b is answer all the rerturn stmts are appeded to single and its printed in the final S.O.P

Posted by Karteek Paruchuri    2013-06-24 12:38:19


Ans is B....

Posted by Maanassa Sudha    2013-06-24 12:58:42


no correct answer given in the options..answer should be "3 iam20years oldiam25years oldIam 24 yrs oldiamiam20years oldiam25years oldIam 24 yrs oldyears old" ncoz the function call of GrandFather.Son wth a string executes Son function of its super class where Str containing " iam20years oldiam25years oldIam 24 yrs old" gets concatenated to "iam" and "years old" on the other side...and returns the string which gets concatenated to its original value " iam20years oldiam25years oldIam 24 yrs old" so final output must be as given above.. i gets incremented 3 tyms... i shld be 3 .. incrementing of i in function does not change its value...

Posted by Pramod Jain    2013-06-24 13:11:43


Yes there's no proper option that's y i kept b which is close to the real output . final output is a single line output with no line breaks and final good morning can't be printed

Posted by Karteek Paruchuri    2013-06-24 13:39:02


Apologies for the mistake in the question. As Pramod Jain pointed out the correct answer is '3 - I am 20 years old, I am 25 years old, I am 24 years old, I am I am 20 years old, I am 25 years old, I am 24 years old,  years old, '

Posted by Merit Campus    2013-06-25 03:32:17


Pramod Jain wins this daily dose and the other two participants Karteek Paruchuri and Maanassa Sudha gets are recharge of Rs. 20 each. Please send us your mobile details and we will process the recharge asap.

Posted by Merit Campus    2013-06-25 03:34:23


got the RC thankyou

Posted by Karteek Paruchuri    2013-06-29 06:05:01


thank u mc.. got rc :)

Posted by Pramod Jain    2013-07-21 14:50:34


© meritcampus 2019

All Rights Reserved.

Open In App