Menu
Topics Index
...
`

Recursive methods output901


If you need explanation Read this topic

If you need Answer Take test on this topic

Recursive methods output901
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)
                //break;
                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 = 32
C. out = 17
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.

Mohsin Ahmed Khan

Posted by Owais Siddiqui    2014-07-08 09:33:42


Answer c
whn both i and j equals 5
if loop pasd and insid if loop else part s active
main() returns 2 bcz a-b==0,
out+=2 gives 17

Posted by Ananda Kumar    2014-07-08 10:02:42


Ans is D  error as atleast one public class should present in main

Posted by Bharath Yelchuri    2014-07-08 14:57:03


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

Posted by Merit Campus    2014-07-09 04:45:43


© meritcampus 2019

All Rights Reserved.

Open In App