C - Arrays - Discussion

You Are Here :: Home > C > Arrays - Discussion

 



Q.

the following program segment is intended to find the index of the first negative integer in arr [0] ... arr [ N-1], where arr is an array of N integers.

int i = 0;
while (arr [i] >= 0)
{
i++;
}
location = i ;
This segment will work as intended

A. Always B. Never
C. Whenever arr contains at least one negative integer D. Whenever arr contains at least one nonnegative integer

Answer: Option C
Explaination:

 


If arr contains no gegative intergers, the value of i will eventually exceed n-1, and arra (I) and arr (i) will cause an Array IndexOutOf BoundsException to be thrown .



Discussion

Your Comments Goes here...
NameDiscussion