Identify the function of the following recursive methods and trace them for give
ID: 675430 • Letter: I
Question
Identify the function of the following recursive methods and trace them for given call
1.
public static void xMethod(int n)
{
if (n > 0)
{
System.out.print(n % 10);
xMethod(n / 10);
}
}
Function:
Trace xMethod (15763)
2.
int[] x = {10, 21, 39, 47, 58};
public static void xMethod(int[] x, int length)
{
System.out.print(" " + x[length - 1]);
xMethod(x, length - 1);
}
Function:
Trace xMethod (a,5)
3.
public static void xMethod(int N)
{
If (N == 0)
return 1;
else
return 2* xMethod(N-1) +3;
}
Function:
Trace xMethod (5)
Explanation / Answer
1.
3
36
367
3675
36751
------------------------------------------------------------------------------------------------------------------------------------------------
2:
58
58 47
58 47 39
58 47 39 21
58 47 39 21 10
------------------------------------------------------------------------------------------------------------------------
3:
1st = (2*1 + 3) = 5
2nd = (5*2 + 3) = 13
3rd = (2*13 + 3) = 29
4th = (2*29 + 3) = 61
5th = (2*61 + 3) = 122+3 = 125 (Ans)