Question

In: Computer Science

Question 11 (1 point) What is wrong with the following recursive method, which is meant to...

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

Solutions

Expert Solution

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.

-----------------------------------------------------------------------------------------------


Related Solutions

Question 11 (1 point) Which of the following are early mediators of type I hypersensitivity? Question...
Question 11 (1 point) Which of the following are early mediators of type I hypersensitivity? Question 11 options: histamines, prostaglandins, IL-4, serotonin, altered phospholipids histamines, prostaglandins as well as eosinophil chemotaxins, serotonin histamines, prostaglandins, cytokines, serotonin leukotrienes, platelet activating factors, histamines, prostaglandins Question 12 (1 point) An example of a type I hypersensitivity reaction is Question 12 options: an allergic reaction to the gold in a ring the allergic reaction to peanuts a transfusion reaction the allergic reaction to poison...
Convert the following recursive method to be tail-recursive Explain what is the advantage of a recursive...
Convert the following recursive method to be tail-recursive Explain what is the advantage of a recursive method to be tail-recursive. public int f8( int[] arr, int index ) { if ( index == -1 ) return 0; if (arr[index] == 2) return 1 + f8( arr, index - 1 ); return f8( arr, index - 1); }
Question 6 (1 point) Which ONE of the following statements about the payback method is true?...
Question 6 (1 point) Which ONE of the following statements about the payback method is true? Question 6 options: The payback method is consistent with the goal of shareholder wealth maximization The payback method represents the number of years it takes a project to recover its initial investment plus a required rate of return. There is no economic rational that links the payback method to shareholder wealth maximization. None of these statements are true. Question 7 (1 point) McKenna Sports...
Which of the following is an effective method to reduce groupthink? Question 11 options: a. Having...
Which of the following is an effective method to reduce groupthink? Question 11 options: a. Having a devil's advocate in the group b. Having designated mindguards c. Group voting d. All of the options are correct.
1. What is meant by the term break-even point? 2.What is meant by the margin of...
1. What is meant by the term break-even point? 2.What is meant by the margin of safety? 3.What is meant by a product’s contribution margin ratio? How is this ratio useful in planning business operations? 4. What is significant about the relevant range? 5. What does the contribution margin ratio's calculate and how is that useful for business operations?
1. What is meant by the term break-even point? 2.What is meant by the margin of...
1. What is meant by the term break-even point? 2.What is meant by the margin of safety? 3.What is meant by a product’s contribution margin ratio? How is this ratio useful in planning business operations? 4. What is significant about the relevant range? 5. What does the contribution margin ratio's calculate and how is that useful for business operations?
Which of the following is wrong as a method of fire prevention common to dangerous goods...
Which of the following is wrong as a method of fire prevention common to dangerous goods of Class 3? (1) Store in a cool place. (2) Do not close the container and store it with air permeability. (3) Do not contact with water. (4) Keep away from fire. (5) Be careful of damage or corrosion of the container.
Using Python Implement Recursive Selection Sort with the following recursive method. 1. Find the smallest number...
Using Python Implement Recursive Selection Sort with the following recursive method. 1. Find the smallest number in the list and swaps it with the first number. 2.Ignore the first number and sort the remaining smaller list recursively.
Question 1 (1 point) Which of the following property describe a good software? Question 1 options:...
Question 1 (1 point) Which of the following property describe a good software? Question 1 options: User friendly Reliable Provide the required functions Efficient All of above Question 2 (1 point) Software crisis is caused by the programming language selected. Question 2 options: True False Question 3 (1 point) Software deteriorates over time because software is susceptible to the environmental maladies such as temperature. Question 3 options: True False Question 4 (1 point) Incremental process model has a higher risk...
Question 1 (1 point) Which of the following would not be plotted on the vertical axis...
Question 1 (1 point) Which of the following would not be plotted on the vertical axis of an H-R diagram on which the properties of all stars within 10 parsecs of the Sun are plotted Question 1 options: Absolute Magnitude Light Flux Luminosity Any of the above would be fine Question 2 (1 point) By a star's position on an HR diagram, we can determine its Question 2 options: luminosity, surface temperature and size distance apparent brightness, and mass colour,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT