Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Which expression represents the action of the followingrecursive method., if onl

ID: 3608271 • Letter: W

Question

Which expression represents the action of the followingrecursive method., if only positive parameters are passed to themethod?           public int guess(int a, int b)           {                if(a == 0)                    return 1;                else                    return b * guess(a - 1, b);           } a)a + b b)a * b c)a^b d)b^a e)b! Which expression represents the action of the followingrecursive method., if only positive parameters are passed to themethod?           public int guess(int a, int b)           {                if(a == 0)                    return 1;                else                    return b * guess(a - 1, b);           } a)a + b b)a * b c)a^b d)b^a e)b!

Explanation / Answer


guess(4, 2) would first compare 4 to 0(if(a==0)). Since they aren't equal, we will enter theelse statement. We can see therefore that guess(4, 2) will return 2 * guess ( 3 , 2). Guessit's time to calculate guess(3,2)!
guess(3, 2) will also go to the "else" casebecause a is not 0. Therefor guess(3, 2) willreturn 2 * guess(2, 2).
guess(2, 2) must also go to the "else" casebecause a is not 0. Therefor guess(2,2) willreturn 2 * guess(1, 2).
guess(1, 2) must also go to the "else" casebecause a is not 0. Therefor guess(1, 2) willreturn 2 * guess(0, 2).
guess(0, 2) will NOT go to the "else" case,since a is finally equal to 0. Since a is equal to 0, we know thatguess(0, 2) will return 1. Now we can begincollapsing back to the ones we earlier determined as dependent onrecursion.
guess(1, 2), we recall, returns 2 *guess(0, 2) but since we now know thatguess(0,2) = 1, we see that guess(1,2) returns 2.
guess(2, 2) returns 2 * guess(1,2), so guess(2,2) returns 4 (since weknow that guess(1,2) returns 2).
guess(3, 2) returns 2* guess(22), so guess(3,2) returns8 (since we know that guess(2,2) returns4).
guess(4, 2) returns 2* guess(1,2), so guess(2,2) returns16 (since we knowthat guess(3,2) returns 8).
So, we can see that guess(4,2) returns 16.Hopefully by tracing through the recursion you've gained someinsight into exactly which of these functions are being carriedout. If you haven't, you can certainly narrow it down by observingthat 2 + 4 isn't 16, nor is 2*4, nor is 2!. Therefor, it must beeither a^b or b^a. In this case both are equal to 16, so which onewas it? Well, let's look at what actually happened in the recursionwe traced out. We in the end when you substituted in all of therecursions, guess(4, 2) ended up returning 2 * 2 *2 * 2, or in other words, 2^4. Thus we see that this recursionreturns choice d, b^a.
Just to double-check, let us examine a couple of cases,keeping b = 2. 2^0 is equal to 1. Does this hold when we runguess(0, 2)? It does, since a == 0 and thefunction will simply return 1. 2^1 is equal to 2. guess(1,2) returns 2 (we know from our above tracing out) so againwe can see that this holds.
Hope this is helpful. guess(3, 2) returns 2* guess(22), so guess(3,2) returns8 (since we know that guess(2,2) returns4).
guess(4, 2) returns 2* guess(1,2), so guess(2,2) returns16 (since we knowthat guess(3,2) returns 8).
So, we can see that guess(4,2) returns 16.Hopefully by tracing through the recursion you've gained someinsight into exactly which of these functions are being carriedout. If you haven't, you can certainly narrow it down by observingthat 2 + 4 isn't 16, nor is 2*4, nor is 2!. Therefor, it must beeither a^b or b^a. In this case both are equal to 16, so which onewas it? Well, let's look at what actually happened in the recursionwe traced out. We in the end when you substituted in all of therecursions, guess(4, 2) ended up returning 2 * 2 *2 * 2, or in other words, 2^4. Thus we see that this recursionreturns choice d, b^a.
Just to double-check, let us examine a couple of cases,keeping b = 2. 2^0 is equal to 1. Does this hold when we runguess(0, 2)? It does, since a == 0 and thefunction will simply return 1. 2^1 is equal to 2. guess(1,2) returns 2 (we know from our above tracing out) so againwe can see that this holds.
Hope this is helpful.
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote