Questions
Questions

CS_1114_21571_202501 Final Exam- Requires Respondus LockDown Browser

Single choice

The following code for method minVal contains a logic error on a single line in the method body, on one of the four lines indicated by comments: public static int minVal(int[] y, int first, int last) { /* This method returns the value of the minimum element in the * subsection of the array "y", starting at position * "first" and ending at position "last". */ int bestSoFar = y[first]; for (int i=first+1; i<=last; i++) // line 1 { if ( y[i] < y[bestSoFar] ) // line 2 bestSoFar = y[i]; // line 3 } // for return bestSoFar; // line 4 } // method minVal Which one of the four lines indicated by the comments contains the logic error?

Options
A.2
B.1
C.3
D.4
View Explanation

View Explanation

Verified Answer
Please login to view
Step-by-Step Analysis
The question asks us to identify which line among the four marked lines contains the logic error. We'll evaluate each option in turn, explaining why it is correct or incorrect, and how it affects the method. Option 1 (line 1): for (int i=first+1; i<=last; i++) — This loop boundary is appropriate for scanning the subarray from first+1 up to last inclusive. The index progression i++ is correct, and the condition i <= ......Login to view full explanation

Log in for full answers

We've collected over 50,000 authentic exam questions and detailed explanations from around the globe. Log in now and get instant access to the answers!

Similar Questions

More Practical Tools for Students Powered by AI Study Helper

Join us and instantly unlock extensive past papers & exclusive solutions to get a head start on your studies!