ProgrammingHardTCS NQT · Infosys pattern
What is printed? (recursion with a pointer parameter used only for read, not accumulation)
What is printed? (recursion with a pointer parameter used only for read, not accumulation)
int maxOf(int a[], int n) {
if (n == 1) return a[0];
int rest = maxOf(a + 1, n - 1);
return a[0] > rest ? a[0] : rest;
}
int main(void) {
int a[] = {3, 7, 2, 9, 4};
printf("%d", maxOf(a, 5));
}- A. 7
- B. 4
- C. 3
- D. 9
Correct answer: D. 9
Each call advances the pointer by one and shrinks n by one, recursing down to a single element, then bubbling back up the maximum seen so far. The largest value in {3,7,2,9,4} is 9.
More C Programming questions
- What is printed? (recursive function returning a computed value through a chain of ternaries) c int classify(int n) { return n == 0 ? 0…
- What is printed? c #define SQUARE(x) x x int main(void) { int a = 3; printf("%d", SQUARE(a + 1)); }
- What is printed? c #define PRINT(x) printf(#x " = %dn", x) int main(void) { int apple = 10; PRINT(apple); }
- What is printed? c #define CONCAT(a, b) a ## b int xy = 100; printf("%d", CONCAT(x, y));
- What is printed? c #define N 5 #undef N #define N 10 printf("%d", N);
- What is printed? c #if defined(DEBUG) #define LOG(msg) printf("DEBUG: %sn", msg) #else #define LOG(msg) #endif int main(void) {…