Topics Index

Cute Family

If you need explanation Read this topic

If you need Answer Take test on this topic

Cute Family
What will be the output of the following program?
public class CuteFamily {
    public static void main(String[] args) {
        Daddy sharath = new Daddy();
        Mommy vani = new Mommy();
        Kid sahithi = new Kid();
        Kid swathi = new Kid();
        sharath.kid1 = sahithi;
        vani.kid2 = swathi;
        swathi.daddy = sharath;
        sahithi.mommy = vani;
        p("F", sahithi);
        p("A", sahithi.daddy);
        p("M", vani.kid2.daddy);
        p("I", sharath.kid1.mommy);
        p("L", swathi.daddy.kid2);
        p("Y", sahithi.mommy.kid2);
    private static void p(String s, Object object) {
        System.out.print((object != null) ? s : "");
class Daddy { String name; Kid kid1; Kid kid2; }
class Mommy { String name; Kid kid1; Kid kid2; }
class Kid { Daddy daddy; Mommy mommy; }

D. Some other output or Compilation Error
E. Throws NullPointerException
Topic: Behavior Of Java Classes Using Methods

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 B.. all objects valid so they print FAMILY.. it is a type of Association concept..

Posted by Om Prakash Om    2014-05-29 13:39:00

Ans is D. Output is FMIY. sahithi object is initialised so F is printed. sahithi.daddy is nt initialized anywhere so A is not printed. Same is the case with daddy object in swathi object(i.e., vani.kid2 = swathi and vani.kid2.daddy is not initialized) and so on

Posted by Nikhil Sulibhavi    2014-05-29 13:47:27

Ans is D...Op is FMi...sahithi is not null so F wil print n sahithi.daddy is null its not pointg anything so it is null so A wont print n vani.kid2.daddy pointing sharath so m wil print so...

Posted by Uday Kumar    2014-05-29 13:52:05

Asnwer : D FMIY
as sahithi.daddy and  swathi.daddy.kid2 are not initialised they return null and hence nothing is printed in those cases i.e., A ,L

Posted by Deepak Kumar Yadav    2014-05-29 13:55:19

ans d. some other output FMIY.insid the kid,mummy,daddy class variables are ly declared.
by using dot operatr ,instance varibls are assign with some values,hre p functn with A and L objct variables nt assignd or stord any values ,so default null value s takn

Posted by Maheshwari Natarajan    2014-05-29 14:04:39

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

Posted by Merit Campus    2014-05-30 06:01:23

© meritcampus 2019

All Rights Reserved.

Open In App