Java Reference
In-Depth Information
d. In the general case, the solution to the problem is obtained directly.
e. A recursive method always returns a value.
2. What is a base case?
3. What is a recursive case?
4. What is direct recursion?
5. What is indirect recursion?
6. What is tail recursion?
7. Consider the following recursive method:
public static int mystery( int number)
//Line 1
{
if (number == 0)
//Line 2
return number;
//Line 3
else
//Line 4
return (number + mystery(number - 1));
//Line 5
}
a. Identify the base case.
b. Identify the general case.
c. What valid values can be passed as parameters to the method mystery ?
d. If mystery(0) is a valid call, what is its value? If it is not a valid call,
explain why.
e. If mystery(5) is a valid call, what is its value? If not, explain why.
f. If mystery(-3) is a valid call, what is its value? If not, explain why.
8. Consider the following recursive method:
public static void funcRec( int u, char v)
//Line 1
{
if (u == 0)
//Line 2
System.out.print(v);
//Line 3
else if (u == 1)
//Line 4
System.out.print(( char )(( int) (v) + 1);
//Line 5
else
//Line 6
funcRec(u - 1, v);
//Line 7
}
a. Identify the base case.
b. Identify the general case.
c. What is the output of the following statement?
funcRec(5,'A');
9. Consider the following recursive method:
public static void exercise( int x)
{
if (x > 0 && x < 10)
Search WWH ::




Custom Search