Menu
Question Index
...

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; }


FAMIY
FAMILY
FMILY
Some other output or Compilation Error
Throws NullPointerException

Doubts

Problems

Topic: Behavior Of Java Classes Using Methods

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App