Question

on 2 Consider the following recursive method test: public static int test(String s, int last) if (last < 0) { return 0; if (s
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the answer for the above given question ..

ANSWER :  
The Output of test("11001",4) is 25

OUTPUT :

Files : Main.java @ saved 1 class Main { //Test function definition public static int test(String s, int last) Add file Open

Explanation :

The entire code talk about the one of the function's feature called Recursion. So the above function will get returned to the main function only when the last value goes negative. So in total the test function will get executed for 5 times since the length of the given string is of 5. So all the function calls will get stored in a STACK.

when the last value is -1 and 0 will be returned back to the function call.

I will give the values of function returns based on the last value:

When last = -1 ,  function returns 0.

last = 0 , 1+2*0 = 1

last =1 , 1+2*1 = 3

last =2 , 2*3 = 6

last =3 , 2*6 = 12

last =4 , 1+2*12 = 1+24 =25

Hence the result is 25.

Thanks..

Add a comment
Know the answer?
Add Answer to:
on 2 Consider the following recursive method test: public static int test(String s, int last) if...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT