Question: Consider the following data field and method. Method maxHelper is intended to return the largest value among the first numVals values in an array; however,

Consider the following data field and method. Method maxHelper is intended to return the largest value among the first numVals values in an array; however, maxHelper does not work as intended.
private int[] nums;
// precondition: 0< numVals <= nums.length
private int maxHelper(int numVals)
{
Line 1: int max = maxHelper(numVals -1);
Line 2: if (max > nums[numVals -1])
return max;
else
return nums[numVals -1];
}
Which of the following corrects the method maxHelper so that it works as intended?
Responses
Insert the following statement before Line 1.
if (numVals ==0)
return numVals;
Insert the following statement before Line 1. if (numVals ==0) return numVals;
Insert the following statement before Line 1.
if (numVals ==1
return nums[0];
Insert the following statement before Line 1. if (numVals ==1 return nums[0];
Insert the following statement between Line 1 and Line 2.
if (numVals ==0)
return numVals;
Insert the following statement between Line 1 and Line 2. if (numVals ==0) return numVals;
Insert the following statement between Line 1 and Line 2.
if (numVals ==1)
return nums[0];
Insert the following statement between Line 1 and Line 2. if (numVals ==1) return nums[0];
Insert the following statement between Line 1 and Line 2.
if (numVals <2)
return numVals;

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!