Java Tracing Recursion Worksheet

ADVERTISEMENT

Java
Name -
Tracing Recursion Worksheet #2
Period -
1.
public void strRecur(String s)
{
if (s.length() < 6)
{
System.out.println(s);
strRecur(s + "*");
}
}
What is displayed by the method call strRecur("wyo") ?
2.
public void printString(String s)
{
if (s.length() > 0)
{
printString(s.substring(1));
System.out.print(s.substring(0, 1));
}
}
What is displayed by the method call printString("wyo") ?
3.
public void doSomething(int n)
{
if (n > 0)
{
doSomething(n - 1);
System.out.print(n);
doSomething(n - 1);
}
}
What is displayed by the method call doSomething(3) ?
4.
public int mystery(int n)
{
if (n < 0)
return 2;
else
return mystery(n - 1) + mystery(n - 3);
}
What value is returned by the method call
mystery(3) ?

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Education
Go