ProgrammingModerateTCS NQT · Infosys pattern
What is printed?
What is printed?
int a[] = {3, 1, 4, 1, 5};
int sum(int n) { return n == 0 ? 0 : a[n - 1] + sum(n - 1); }
int main(void) { printf("%d", sum(5)); }- A. 14
- B. 9
- C. 15
- D. 13
Correct answer: A. 14
sum(n) adds the last of the first n elements and recurses: 5 + 1 + 4 + 1 + 3 = 14. Using a[n] instead of a[n − 1] would read past the end of the array.
More C Programming questions
- Assume a 64-bit LP64 ABI (4-byte int, 8-byte pointers and double, natural alignment). What is printed? c int a[3][4] = {{1, 2, 3, 4}, {5…
- Assume 32-bit int and two's complement. What is printed? c unsigned char a = 0x0F; unsigned char b = ~a; printf("%d %d %d %d %zu", ~a ==…
- Assume 32-bit int and 64-bit long long. What is printed? c int n = -1; unsigned int len = 3; long long big = -1; printf("%d %d %d %d", n <…
- Assume a 64-bit LP64 ABI (4-byte int, 8-byte pointers and double, natural alignment). What is printed? c struct A { char c; double d; int…
- What is printed? c #define SQ(x) x x #define MIN(a, b) ((a) < (b) ? (a) : (b)) int calls = 0; int next(void) { return ++calls; } int…
- What is printed? c #define N 4 #define STR(x) #x #define XSTR(x) STR(x) #define CAT(a, b) a##b #define XCAT(a, b) CAT(a, b) int NN = 9…