14.4k views
7 votes
Consider the following method, which is intended to return the index of the first negative integer in a given array of integers. public int positionOfFirstNegative(int[] values) { int index = 0; while (values[index] >= 0) { index++; } return index; } What precondition is needed on the values array so that the method will work as intended?

User Marcz
by
4.6k points

1 Answer

9 votes

Answer:

The answer is "The array values must contain at least one negative integer".

Step-by-step explanation:

In the above-given code, a method "positionOfFirstNegative" is declared, that accepts an array "values" as a parameter, inside the method an integer variable index is declared, that use in the while loop to check array value is greater than equal to 0, if the conditional is true it will increments the index value and return its value, that's why in this question array value include at least one negative value.

User KeironLowe
by
4.7k points