Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David Kroenke, David Auer, Scott Vandenberg, Robert Yoder

9th Edition

0135188148, 978-0135188149, 9781642087611

More Books

Students also viewed these Databases questions

Question

Describe ERP and how it can create efficiency within a business

Answered: 1 week ago