ProgrammingHardProduct company OA

What is printed?

What is printed?
int a[6] = {4, 8, 15, 16, 23, 0};
int find(int x) {
    a[5] = x;
    int i = 0;
    while (a[i] != x) i++;
    return i < 5 ? i : -1;
}
int main(void) { printf("%d %d", find(16), find(42)); }
  1. A. 4 -1
  2. B. 3 5
  3. C. 3 6
  4. D. 3 -1

Correct answer: D. 3 -1

The spare last slot holds a sentinel copy of x, so the loop needs no bounds test and always stops. 16 is found at index 3. For 42 the loop stops at the sentinel, index 5, which the return converts to −1. '3 5' forgets that conversion.

More C Programming questions

Exams that ask this topic