ProgrammingHardProduct company OA
What is printed?
What is printed?
int a[] = {5, 1, 4, 2, 3};
int *lo = a, *hi = a + 4;
while (lo < hi) {
if (*lo > *hi) lo++;
else hi--;
}
printf("%td %d", lo - a, *lo);- A. 4 3
- B. 2 4
- C. 0 5
- D. 1 1
Correct answer: D. 1 1
Each step discards the larger of the two ends (when they are equal it drops the right one), so the smaller value always survives. The pointers meet on the minimum: 1, at index 1. The search is a two-pointer minimum, not a sort.
More C Programming questions
- What is printed? c long memo[50]; int calls = 0; long f(int n) { calls++; if (n < 2) return n; if (memo[n]) return memo[n]; return memo[n]…
- With 4-byte int, what is printed? c int x = 7; printf("%zu %zu", sizeof (int) 3, sizeof x + 1);
- What is printed? c char s[8] = {'a', 'b'}; printf("%zu %zu %d", strlen(s), sizeof s, s[7]);
- What does r(3) print, and how many digits would r(6) print? c void r(int n) { if (n == 0) return; r(n - 1); printf("%d", n); r(n - 1); }
- What is printed? c const char s = "abcdefgh"; const char p = s + 2, q = strchr(s, 'g'); printf("%td %c %s", q - p, (p + (q - p) / 2), q);
- Given int f(int); and int (fp)(int) = f; which of these calls are valid? (i) fp(2) (ii) (fp)(2) (iii) (fp)(2) (iv) (&fp)(2)