Question 110 pts What is the value returned from the followi
Question 110 pts
What is the value returned from the following method when it is called with the value 9?
int mystery (int n)
{
if (n < 1)
return 0;
else if (n % 2 == 0)
return mystery(n-1);
else return 1 + mystery(n-1);
}
Flag this Question
Question 210 pts
public static int f(int [] a, int begin, int end) {
if(begin == end) {
return a[begin];
}
int mid = (begin + end)/2;
int a1 =f(a, begin, mid);
int a2 = f(a, mid + 1, end);
return (a1 > a2) ? a1 : a2;
}
Flag this Question
Question 310 pts
When too many recursive calls are made creating more activation records than the allocated program memory can handle, what kind of error occurs?
Flag this Question
Question 410 pts
A recursive method that processes a chain of linked nodes.
Flag this Question
Question 510 pts
The number of recursive calls required for recursively calculating xn is
Flag this Question
Question 610 pts
The rate of growth for the Towers of Hanoi problem as the number of disks increases is
Flag this Question
Question 710 pts
When the last action in a recursive method is a recursive call, it is called
Flag this Question
Question 810 pts
What is the output of the following program when the method is called with 4?
void unknown(int n)
{
if (n > 0)
{
System.out.print(\"?\");
unknown(n-1);
}
}
Flag this Question
Question 910 pts
How many recursive calls will be made if the following method is called with 6?
void greeting(int n)
{
System.out.println(\"Hello!\");
greeting(n-1);
}
Flag this Question
Question 1010 pts
A recursive method uses less memory than an iterative method.
| 4 |
Solution
Solution 110: 5
Explanation: This is a recursive function.
Since it is recursive so 1st if() is the terminating condition where it will stop calling itself and return 0. The 2nd if() checks whether it is an even no; if yes then the function calls itself after decrementing the value by 1.The 3rd if() also makes a recursive call but before returning adds 1 to the returning value.
**************************************************************************************************************************
Solution 210: the maximum element in a
Explanation:
This is a recursive function.
the termination condition is the first if() where it checks whether there is one element left in the array or not i.e. begin index = end index.
Then the array is divided into two parts where each of the halves are sent as argument in the recursive call.
This method is an implementation of divide & conquer algorithm, where the array or the data structure is divided unto smallest part possible and then the business logic is implemented.
**********************************************************************************************************************************
Solution 310:
Explaination: stack overflow\'
When there is a function call then the current state of the code flow is stored in a stack. Stack is a LIFO or Last In First Out i.e. the last element which is inserted to the stack is the first one to come out. Eg: a stack of CDs/plates arranged on top of each other.
Now when due to loose logic handling such as not correct terminating condition, then the function keeps on calling itself and consumes the memory stack throwing \'stack overflow\'.
***********************************************************************************************************************************
Solution 610: exponential
************************************************************************************************************************************
Solution 710: tail recusrion
Explanation:
A function is called tail recursive if the last action done by the function is the recursive call.
*************************************************************************************************************************************
Solution 810: ????
Explanation:
Since this is a recursive function it will call itself by decrementing n and the terminating condition is if(n>0).
Since n =4 hence \'?\' is printed four times.
***********************************************************************************************************************************
Solution 910: infinite
Explanation:
Since there is no terminating condition so the recursion will go on infinitely until the stack overflows and it throws a java.lang.StackOverflowError
**********************************************************************************************************************************
Solution 1010: false
Explanation:
The recursive method uses the call stack memory which is an overhead. While on the other hand in iterative method the memory required is allocated at one go.


