ProgrammingHardWipro · Cognizant pattern
What is printed?
What is printed?
void f(int n) {
if (n <= 0) return;
printf("%d ", n);
if (n % 2 == 0) f(n / 2);
else f(n - 1);
}
int main() { f(6); }- A. 6 3 2 1
- B. 6 3 1
- C. 6 5 4
- D. 6 3 2
Correct answer: A. 6 3 2 1
n=6 (even): prints 6, recurses with 3. n=3 (odd): prints 3, recurses with 2. n=2 (even): prints 2, recurses with 1. n=1 (odd): prints 1, recurses with 0, which returns immediately (base case, no print). Output: 6 3 2 1.
More C Programming questions
- Assume a 64-bit LP64 ABI. What is printed? c int x = 5, y = 10, z = 15; int arr[3] = {&x, &y, &z}; int p = arr; printf("%d %d %d", arr[1]…
- Assume a 64-bit LP64 ABI. Which declaration makes p an array of 5 pointers to int, and which makes q a pointer to an array of 5 ints?
- Assume a 64-bit LP64 ABI. What is printed? c int mat[2][3] = {{1, 2, 3}, {4, 5, 6}}; int (rowPtr)[3] = mat; printf("%d %d %d %zu"…
- Assume a 64-bit LP64 ABI. What is printed? c int a[4] = {1, 2, 3, 4}; int p = a + 3; int q = a; printf("%ld %d %d", (long)(p - q), p, (p…
- Assume a 64-bit LP64 ABI. What is printed? c void swap(int a, int b) { int tmp = a; a = b; b = tmp; } int main(void) { int x = 1, y = 2…
- Assume a 64-bit LP64 ABI. What is printed? c int a = 1, b = 2, c = 3; int arr[3] = {&a, &b, &c}; int p = arr; p++; p += 10; printf("%d %d…