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);
}- A. 0
- B. 2
- C. 1
- 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
- How many subsets of {1, 2, 3, 4, 5} sum to exactly 10? Compute with this program. c #include <stdio.h> int main(void) { int c = 0; for…
- What is printed? c #include <stdio.h> #include <stdlib.h> int cmp(const void a, const void b) { int x = (const int )a, y = (const int )b…
- What is printed? c #include <stdio.h> int main(void) { unsigned s = 0; for (unsigned b = 1; b; b <<= 1) s += 1; printf("%u", s); }
- What is printed? c #include <stdio.h> int main(void) { printf("%d", (3 & 1) == 1 ? 'y' : 'n'); }
- What is printed? c #include <stdio.h> int main(void) { printf("%d", (7 & 5) | (2 ^ 6) << 1); }
- What is printed? c #include <stdio.h> int main(void) { printf("%d", 5 > 3 ? 2 > 1 ? 7 : 8 : 9); }