ProgrammingHardProduct company OA
What is printed?
What is printed?
void f(int *p, int n) {
if (n == 0) return;
*p += n;
f(p, n - 1);
*p *= 2;
}
int main(void) {
int x = 0;
f(&x, 3);
printf("%d", x);
}- A. 12
- B. 22
- C. 24
- D. 48
Correct answer: D. 48
All the additions happen on the way down (x = 3 + 2 + 1 = 6) and the three doublings on the way back up: 6 → 12 → 24 → 48. '12' doubles only once; '22' interleaves the steps as if each call added and doubled before recursing.
More C Programming questions
- On an LP64 system (8-byte pointers), what is printed? c printf("%zu %zu", sizeof("abc"), sizeof("abc" + 1));
- On an LP64 system (8-byte pointers), what is printed? c char a[] = "cat", b = "cat"; printf("%d %d", strcmp(a, b) == 0, sizeof(a) ==…
- What is printed? c struct P { int x; int y; }; struct P a[3] = {{1, 2}, {3, 4}, {5, 6}}; struct P p = a; int r1 = (p + 2)->x; int r2 =…
- With a C99 (or later) compiler, what is printed? c int ds(int n) { return n == 0 ? 0 : n % 10 + ds(n / 10); } int main(void) {…
- What is printed? c int f(unsigned x) { int c = 0; while (x) { c += x & 1; x >>= 2; } return c; } int main(void) { printf("%d", f(0xFF) +…
- What is printed? c #define VAR(n) v##n #define PRINT(n) printf("%d ", VAR(n)) int v1 = 10, v2 = 20, v12 = 30; PRINT(1); PRINT(1 + 1)…