ProgrammingHardProduct company OA
What is printed?
What is printed?
void f(int *a, int n) {
if (n <= 1) return;
a[n - 2] += a[n - 1];
f(a, n - 1);
}
int main(void) {
int a[] = {1, 2, 3, 4};
f(a, 4);
printf("%d %d %d %d", a[0], a[1], a[2], a[3]);
}- A. 10 9 7 0
- B. 3 5 7 4
- C. 1 3 6 10
- D. 10 9 7 4
Correct answer: D. 10 9 7 4
Working from the right, each element adds the already-updated element after it: a[2] = 3 + 4 = 7, a[1] = 2 + 7 = 9, a[0] = 1 + 9 = 10. The result is suffix sums. '3 5 7 4' adds the original neighbours only.
More C Programming questions
- With a 32-bit unsigned int x, which expression has undefined behaviour?
- What is printed? c char s[] = "7392"; int n = 0; for (int i = 0; s[i]; i++) n = n 10 + (s[i] - '0'); printf("%d %d", n % 1000, n / 100 %…
- What is printed? c int f(int n) { if (n == 0) return 0; return (n & 1) + f(n >> 1) 10; } int main(void) { printf("%d", f(13)); }
- What is printed? c struct S { int a; int b; } s[] = {{1, 10}, {2, 20}, {3, 30}}; struct S p = s; int x = ++p->a; int y = (++p)->b; int z =…
- Assume 32-bit unsigned int. What is printed? c unsigned x = 0x12345678; printf("%X", (x >> 16) | (x << 16));
- What is printed? c int ok(const char s, int d) { if (d < 0) return 0; if (!s) return d == 0; return ok(s + 1, d + (s == '(') - (s ==…