info@meritcampus.com    +91-85006-22255
...
`

Recursive Method Output599
What will be the output of the following program?
`class Recursion {    public static void main(String[] args)     {        int ma = 35;        int in = 67;        int sta = recruse(ma, in);        System.out.println("static = " + sta + "tic");    }    public static int recurse(int pub, int lic)     {        if (pub % 13 == 0)         {            return pub + lic;        }        return recurse(pub++, lic / 2);    }}`

 A. static = 41tic B. static = 47tic C. static = 43tic D. Compilation Error E. Throws StackOverflowError
Topic:

### User comments below.All of them might not be correct.

ans:D
because method recruse(ma.in) is not defined in the class thats why it gives complile time error
and if we change recruse to recurse()  also it give  runtime error called stack over flow  error
it go infinite recursive because of  recurse(pab++,lic/2) this funtion call

if we want excecute  this program succesfully
changes are recruse() to recurse() and
in method recurse()
recurse(pab++,lic/2) to recurse(++pab,lic/2)

Posted by Goutham Singarapu    2015-02-06 04:26:00

ans c.initially ma ,in assigned with 35 ,67.sta assigned with recrush() method.here we used different names as parameters in function definition. if condition satisfied only ma is completely divided by 13.so return method recursively calls this method upto it get value 35 to 39.1st tim pub++ is 36, lic/2=33 passed 2nd time pub is 37 ,lic/2=16 these values passed, 3rd time pub is 38,lic/2=8,4th tim pub++ is 39,lic/2 is 4.this time if condition passed. it returns pub+lic=39+4=43.finally sop prints static 43tic

Posted by Maheshwari Natarajan    2015-02-06 04:44:37

compilation error

Posted by Vino Krishnan    2015-02-06 06:49:14

c

Posted by Shubham Bansal    2015-02-06 18:16:57

This dose is now closed and the winners are Goutham Singarapu,  for 'First Correct Comment', Goutham Singarapu,  for 'Best Comment' and Goutham Singarapu for the 'Popular Comment'. The 'lucky liker' is Gopinath Manchikanti. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2015-02-09 04:50:08