Question: Analyze the following code and from the statements below select those that are true. public class TwoRecursiveMethods { public static void main ( String [

Analyze the following code and from the statements below select those that are true.
public class TwoRecursiveMethods {
public static void main(String[] args){
System.out.println(f1(3,0));
System.out.println(f2(3));
}
public static int f1(int n, int result){
if (n ==0)
return result;
else
return f1(n -1, n + result);
}
}
public static int f2(int n){
if (n ==0)
return 0;
else {
return n + f2(n -1);
}
}
Group of answer choices
f1 is tail recursion, but f2 is not
For a non-negative input n, f2 computes 0+1+2+...+ n
For a non-negative input n and initial value of result =0, f1 computes 0+1+2+...+ n
Neither f1 nor f2 is tail recursive
For all negative inputs both f1 and f2 run infinitely and cause a StackOverflowError
f2 is tail recursion, but f1 is not

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!