Q41. private int func3(int m, int n) {
if (m < n)
return 0;
else
return 1 + func3(m-n, n);
}
Refer to the code above. What is the value of func3(-5, 1)?
a. -5
b. 0
c. 1
d. 5
Q50. private int func1(int m, int n) {
if (m==n || n==1)
return 1;
else
return func1(m-1,n-1) + n*func1(m-1,n);
}
What precondition must exist in order to prevent the code above from infinite recursion?
a. m > = 0 and n >= 0
b. m >= 0 and n >= 1
c. m >= 1 and n >= 0
d. m >= 1 and n >= 1
Expert Answer
if (m < n)
return 0;
else
return 1 + func3(m-n, n);
}
// func3(-5,1)
/*
func3(-5,1)
m = -5, n = 1
since m is less than n, base case is reached and 0 is returned
output: 0
*/
int func1(int m, int n) {
if (m==n || n==1)
return 1;
else
return func1(m-1,n-1) + n*func1(m-1,n);
}
/*
n should be greater than or equal to 1 inorder to
reach base case n == 1.hence n >= 1
and
m should be greater than n inorder to
reach base case m >= n and n >= 1
hence, m should also be greater than or equal to 1
answer: d. m >= 1 and n >= 1
*/