Menu
Topics Index
...
`

Linked Classes


If you need explanation Read this topic

If you need Answer Take test on this topic

Linked Classes
What will be the output of the following program?
public class LinkedClasses {
    public static void main(String[] args) {
        int count = 6;
        M m = new M(); m.print(count--);
        m.n = new N(); m.print(count--);
        m.n.o = new O(); m.print(count--);
        m.n.o.m = m; m.print(count--);
    }
}
class M {
    N n;
    void print(int count) {
        System.out.print("M" + count + " ");
        if (n != null && count > 0) n.print(count - 1);
    }
}
class N {
    O o;
    void print(int count) {
        System.out.print("N" + count + " ");
        if (o != null && count > 0) o.print(count - 1);
    }
}
class O {
    M m;
    void print(int count) {
        System.out.print("O" + count + " ");
        if (m != null && count > 0) m.print(count - 1);
    }
}

A. M5 M4 N3 M3 N2 O1 M2 N1 O0 M-1
B. M5 M4 N3 M3 N2 O1 M2 N1 O0
C. M6 M5 N4 M4 N3 O2 M3 N2 O1 M0
D. Some other output not shown here
E. Compilation Error or Run-time Error or goes into infinite loop
Topic: Calling A Class From Another Class 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 :E ..because here m,n,o objects are not created..because here circular dependency is happend ..so..it will give compilation error...

Posted by Sapparapu Pradeep Kumar    2014-02-21 09:18:16


Ans :E ..because here m,n,o objects are not created..because here circular dependency is happend ..so..it will give compilation error...

Posted by Sapparapu Pradeep Kumar    2014-02-21 09:18:17


C is the answer. For the first m.print() statement, 6 is passed to print method in Class M and hence M6 is printed. contorller returns back to main method. For second m.print() method 5 is passed to print method in calss M and M5 is printed. 4 is passed to print method in calss N and N4 is printed. contorller returns back to main method. For third m.print() method 4 is passed to print method in calss M and M4 is printed. 3 is passed to print method in calss N and N3 is printed. 2 is passed to print method in Class O and O2 is printed. contorller returns back to main method. For fourth m.print() method 3 is passed to print method in calss M and M3 is printed. 2 is passed to print method in calss N and N2 is printed. 1 is passed to print method in Class O and O1 is printed. 0 is passed to print method in calss M and M0 is printed. contorller returns back to main method. Here "Count" variable is local to all the classes or methods

Posted by Ramya Bonagiri    2014-02-22 10:52:34


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

Posted by Merit Campus    2014-02-24 04:29:40


Thank you!!!! :D @Merit Campus

Posted by Ramya Bonagiri    2014-02-24 15:26:12


© meritcampus 2019

All Rights Reserved.

Open In App