ProgrammingHardWipro · Cognizant pattern
Assume a 64-bit LP64 ABI. What is printed?
Assume a 64-bit LP64 ABI. What is printed?
int sumArray(int *arr, int n) {
if (n == 0) return 0;
return *arr + sumArray(arr + 1, n - 1);
}
int main(void) {
int nums[] = {1, 2, 3, 4, 5};
printf("%d", sumArray(nums, 5));
}- A. 1
- B. 0
- C. 15
- D. 5
Correct answer: C. 15
Each recursive call adds the current element and advances the pointer by one, recursing on the remaining n-1 elements, until n reaches 0: 1+2+3+4+5=15.
More C Programming questions
- Assume a 64-bit LP64 ABI. What is printed? c int compareInts(const void a, const void b) { int ia = (const int )a; int ib = (const int )b…
- Assume a 64-bit LP64 ABI. What is printed? c int fibIterative(int n) { int a = 0, b = 1; for (int i = 0; i < n; i++) { int tmp = a + b; a…
- Assume a 64-bit LP64 ABI. What is printed? c int filterEven(int arr, int n, int outCount) { static int result[10]; int count = 0; for (int…
- Assume a 64-bit LP64 ABI. What is printed? c int countOccurrences(const char str, char target) { int count = 0; while (str) { if (str ==…
- What is printed? c int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } int main(void) { printf("%d", gcd(48, 18)); }
- What is printed? c int isPalindrome(const char s, int left, int right) { if (left >= right) return 1; if (s[left] != s[right]) return 0…