Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions