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)); }- A. 4 -1
- B. 3 5
- C. 3 6
- 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
- For ints a, b >= 0, which test correctly tells, before adding, whether a + b would overflow?
- What is printed? c const char s = " hi there you "; int words = 0, in = 0; for (; s; s++) { if (s == ' ') in = 0; else if (!in) { in = 1…
- What is printed? c int sub(const char a, const char b) { / is a a subsequence of b? / if (!a) return 1; if (!b) return 0; return sub(a ==…
- What is printed? c char src[] = "abc", dst[8]; char d = dst; const char s = src; while ((d++ = s++)); printf("%s %td %td", dst, d - dst, s…
- What does calloc(n, size) guarantee that malloc(n size) does not?
- What is printed? c int a[3][3] = {{1}, {2, 3}, [2][1] = 7}; printf("%d %d %d %d", a[0][1], a[1][1], a[2][0], a[2][1]);