Menu
Topics Index
...
`

Recursive methods output


If you need explanation Read this topic

If you need Answer Take test on this topic

Recursive methods output
What will be the output of the following program?
class RecursiveFor
{
    public static void main(String[] args)
    {
        int out = 15;
        for (int i = 4; i < 6; i++)
        {
            for (int j = 7; j >= 5; j--)
            {
                if (i == j) continue;
                if (i > j)
                {
                    out += main(i, j);
                }
                else
                {
                    out += main(j, i);
                }
            }
        }
        System.out.println("out = " + out);
    }

    private static int main(int a, int b)
    {
        if (a + b == 0)
        {
            return 2;
        }
        return a + main(a - 1, b);
    }
}

A. out = 78
B. out = 100
C. out = 80
D. Compilation Error
E. Goes into infinite loop
Topic: Recursive 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 B :: firstly main(7,4) is called whose value is 24 . then main(6,4)=17, then main(5,4)=11. thus nw out =67. now main(7,5 ) is called whose value is 20. then main(6,5) is called value=13. thus out value is now =100. vn i==j it skips.

Posted by Shaik Nyamath    2014-04-18 13:19:31


ans d:since main file doesn't contain any public class

Posted by Manasa Jyothi Sree    2014-04-19 13:42:57


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

Posted by Merit Campus    2014-04-21 04:12:39


© meritcampus 2019

All Rights Reserved.

Open In App