ProgrammingModerateTCS NQT · Infosys

What is printed? A sorted int array {1, 3, 5, 7} is searched with bsearch for the key 6.

What is printed? A sorted int array {1, 3, 5, 7} is searched with bsearch for the key 6.
#include <stdio.h>
#include <stdlib.h>
int cmp(const void *a, const void *b) {
    int x = *(const int *)a, y = *(const int *)b;
    return (x > y) - (x < y);
}
int main(void) {
    int a[] = {1, 3, 5, 7}, k = 6;
    printf("%d", bsearch(&k, a, 4, sizeof a[0], cmp) == NULL);
}
  1. A. 0
  2. B. 2
  3. C. 1
  4. D. 3

Correct answer: C. 1

6 is not in the array, so bsearch returns NULL, and the comparison NULL == NULL is 1. A search for 5 would return a non-NULL pointer and print 0.

More C Programming questions

Exams that ask this topic