In: Computer Science
Question 11 (1 point)
What is wrong with the following recursive method, which is meant to compute the sum of all numbers from 1 to n? public int summation(int n) { return n + summation(n-1);}
Question 11 options:
The method lacks a base case |
|
The method lacks a recursive step |
|
The base case is incorrect |
|
The recursive step is incorrect |
Question 12 (1 point)
In which of these situations does it make sense to consider a recursive solution?
Question 12 options:
The problem involves arithmetic such as addition, multiplication, etc. |
|
The problem is really confusing |
|
The problem involves something that must be done repeatedly |
|
The problem can be divided up into smaller, but identical, sub-problems |
Question 13 (1 point)
An incorrect recursive algorithm sometimes results in a StackOverflowError. Which of the following statements is true about this type of error?
Question 13 options:
The recursive step may be incorrect |
|
The base case may be missing |
|
This is analogous to an infinite loop for an iterative solution |
|
All of these |
|
None of these |
Question 14 (1 point)
The following two method signatures are equivalent.
public getItem(E[] items)
public getItem(T[] items)
Question 14 options:
True | |
False |
Question 15 (1 point)
You are looking through the documentation for a software library you want to use in your application and see this method. What types of objects can be passed to it as an argument? Choose all valid options.
public int doSomething(E arg) { ... }
Question 15 options:
instances of Double |
|
instance of Number |
|
instances of a class called E |
|
instances of Object |
Question 16 (1 point)
Based on this method signature, what types of objects can be passed to it as an argument? Choose all valid options.
public <E extends Comparable<E>> int doWork(E arg) { }
Question 16 options:
instances of Double |
|
instance of Number |
|
instances of a class called E |
|
instances of Object |
Answer to Question 11:
As specified in the question, the method public int summation(int n) { return n + summation(n-1);} is meant to compute the sum of all numbers from 1 to n. However, according to the code given above, the method is evaluated even for values 0, -1, -2 and so on (i.e, 1-1=0 when n=1, 0-1=-1 when n=0 and so on). This is because the method lacks a base case.
For this method to be correct, the code would be as follows:
public int summation(int n) {
if(n==1)
return 1; //When value of n reaches 1, 1 is
added to summation and the recursion stops.
else
return n + summation(n-1);
}
Hence, the answer to question 11 is : The recursive method is wrong as the method lacks a base case.
-----------------------------------------------------------------------------------------------
Answer to Question 12:
A recursive solution is considered when the problem involves something that must be done repeatedly.
Recursion is the process of solving a problem in terms of itself. When a solution involves repeating of a particular step, each time with a different value, until a certain terminal condition is met, the solution is then termed as being recursive.
-----------------------------------------------------------------------------------------------
Answer to Question 13:
The correct answer to Question 13 is All of these.
When a recursive step is incorrect, it may generate unreachable code, code with conditions that cannot exist, hence exhausting the stack. This leads to a StackOverflowError.
Also, in situations where the base case is missing, the recursive step will execute infinitely. Without the base case, the recursive method will not exit and hence, will only exhaust the stack, leading to StackOverflowError. This situation is similar to an infinte loop.
-----------------------------------------------------------------------------------------------
Answer to Question 14:
It is FALSE that the following two method signatures are equivalent.
Method 1: public getItem(E[] items)
Method 2: public getItem(T[] items)
In Method 1, the parameter passed to it is an array of type E; whereas in Method 2, the parameter passed to it is an array of type T, which represent two different classes E and T.
-----------------------------------------------------------------------------------------------
Answer to Question 15:
The correct answer to Question 15 is instances of a class called E.
The method, public int doSomething(E arg) { ... } takes as argument arg, which is of type E. arg is only an instance of the class E.
-----------------------------------------------------------------------------------------------
Answer to Question 16:
The correct answer to Question 16 is all options are valid.
-----------------------------------------------------------------------------------------------