a. displayTriangles(order – 1, p1, p12, p31);
b. displayTriangles(order – 1, p12, p2, p23);
c. displayTriangles(order – 1, p31, p23, p3);
d. displayTriangles(order – 1, p12, p23, p31);
Section 18.9 Recursion versus Iteration
20. Which of the following statements are true?
a. Recursive methods run faster than non-recursive methods.
b. Recursive methods usually take more memory space than non-recursive methods.
c. A recursive method can always be replaced by a non-recursive method.
d. In some cases, however, using recursion enables you to give a natural, straightforward, simple solution to a program
that would otherwise be difficult to solve.
#
Section 18.10 Tail Recursion
21. Analyze the following functions;
public class Test1 {
public static void main(String[] args) {
System.out.println(f1(3));
System.out.println(f2(3, 0));
}
public static int f1(int n) {
if (n == 0)
return 0;
else {
return n + f1(n – 1);
}
}
public static int f2(int n, int result) {
if (n == 0)
return result;
else
return f2(n – 1, n + result);
}
}
a. f1 is tail recursion, but f2 is not
b. f2 is tail recursion, but f1 is not
c. f1 and f2 are both tail recursive
d. Neither f1 nor f2 is tail recursive
#
22. Show the output of the following code:
public class Test1 {
public static void main(String[] args) {
System.out.println(f2(2, 0));