Note: Java Consider the following recursive method: public static int Fun(int x) { if(x == 0) //line 1 return 0 //line 2 else if( x == 1) //line 3 return 1; //line 4 else //line 5 return (x*Fun(x-1)); //line 5 } a) Is Fun(4) a valid call? If so, what is the value? If not, explain why? Answer: b) Is Fun(-4) is a valid call? If so, what is the value? If not, explain why?
Note: Java
- Consider the following recursive method:
public static int Fun(int x)
{
if(x == 0) //line 1
return 0 //line 2
else if( x == 1) //line 3
return 1; //line 4
else //line 5
return (x*Fun(x-1)); //line 5
}
a) Is Fun(4) a valid call? If so, what is the value? If not, explain why?
Answer:
b) Is Fun(-4) is a valid call? If so, what is the value? If not, explain why?
Answer:
(c) Consider the following method.
public static int res(int[] list, int first, int last)
{
if (first == last)
return list[first];
else
return list[first] + res(list, first + 1, last);
}
Given the declaration
int[] A = {2, 4, 6, 8, 10};
What is the output of the following statement?
- out.println(res(A, 0, 2));
- out.println(res(A, 0, 4));
Answer:
(d) Consider the following method,
public static void Fun(int n) {
if (n < 2) {
System.out.println(n);
} else {
Fun(n / 2);
Fun(n % 2);
}
}
What is the output of the following statement?
- out.println(Fun(6));
- out.println(Fun(13));
Answer:
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 2 images