Recursion Test

By Mickey Arnold
Last updated 10 months ago
20 Questions

What do you call the condition that stops recursion?

Consider the following method.
public static int myst(int i)
{
if(i<=0)
return 0;
if(i<=3)
return i;
return myst(i-2)+myst(i-1);
}
What is returned by the call myst(5)?

What is returned by the call ben(51) ?
public static String ben(int x)
{
if( x / 5 <= 0 )
return "" + x % 5;
else
return "" + ( x % 5 ) + ben( x / 5 );
}

Consider the following method.
public static int alice(int m, int n)
{
if(m < 3)
return n;
return alice(n-2, m-1);
}
What value does of alice(11, 12) return?

What is returned the call fun(6) ?
public static int fun(int x)
{
if(x < 1)
return 1;
else
return x + fun(x - 1);
}

What is returned by the call fun(8) ?
public static int fun(int x)
{
if(x < 1)
return 1;
else
return x + fun(x - 1);
}

What is returned by the call fun(8) ?
public static int fun(int x){
if(x < 1)
return 1;
else
return x + fun(x - 2);
}

What is returned by the call fun(6) ?
public static int fun(int x){
if(x < 1)
return 1;
else
return x + fun(x - 2);
}

What is returned by the call fun(1) ?
public static int fun(int x)
{
if(x < 1)
return 1;
else
return x - fun(x - 3);
}

What is returned by the call fun(10) ?
public static int fun(int x)
{
if (x < 1)
return x;
else
return x + fun(x - 2);
}

What is returned by the call wacky(5,5) ?
public static int wacky(int x, int y){
if(x <= 1)
return y;
else
return wacky(x - 1,y - 1) + y;
}

What is returned by the call wacky(4,6) ?
public static int wacky(int x, int y){
if(x <= 1)
return y;
else
return wacky(x - 1,y - 1) + y;
}

What is returned by the call wacky(2,6) ?
public static int wacky(int x, int y)
{
if(x <= 1)
return y;
else
return wacky(x - 1,y - 1) + y;
}

What is returned by the call funny(0) ?
public static int funny(int x)
{
if(x<1)
return 1;
else
return x + funny(x - 1) - funny(x - 2);
}

What is returned by the call go(2,6) ?
public static int go(int x, int y)
{
if(x <= 1)
return y;
else
return go(x - 1,y) + y;
}

What is returned by the call go(4,2) ?
public static int go(int x, int y)
{
if(x <= 1)
return y;
else
return go(x - 1,y) + y;
}

What is returned by the call fly(2) ?
public static int fly(int x)
{
if(x < 1)
return 1;
else
return x + fly(x - 3) - fly(x - 2);
}

What is returned by the call go(7,3)?
public static int go(int x, int y)
{
if(x <= 1)
return y;
else
return go(x - 1,y) + y;
}

Which of the following answer choices best describes the algorithmic purpose of method ben?
public static int ben(int[] ray, int i, int x)
{
if( i >= ray.length )
return 0;
if( ray[i] == x )
return 1 + ben( ray, i+1, x );
return 0 + ben( ray, i+1, x );
}

Which of the following answer choices best describes the algorithmic purpose of method ben?
public static int ben(int[] ray, int i)
{
if( i >= ray.length )
return 0;
if( ray[i] % 2 == 0 )
return 1 + ben( ray, i+1 );
return 0 + ben( ray, i+1 );
}