ProgrammingHardProduct-company MCQ pattern
What is printed? (Assume a 64-bit LP64 ABI, where long is 8 bytes.)
What is printed? (Assume a 64-bit LP64 ABI, where long is 8 bytes.)
long fact(int n) {
return n <= 1 ? 1 : n * fact(n - 1);
}
int main(void) {
printf("%ld", fact(13));
}- A. 1932053504
- B. Compilation error: fact(13) exceeds long range
- C. 6227020800
- D. Undefined behaviour: int multiplication overflows before reaching long precision
Correct answer: C. 6227020800
In `n * fact(n-1)`, fact(n-1) is a long, so the usual arithmetic conversions promote n (int) to long before multiplying — the entire computation happens in 64-bit long precision, never truncating to 32-bit int. 13! = 6227020800, which fits comfortably in a long, so that exact value is printed with no overflow.
More C Programming questions
- What is printed? (Assume int is 32 bits.) c int a = -1; unsigned int b = (unsigned int)a; printf("%u", b);
- What is printed? (Check if two integers have opposite signs, without using multiplication.) c int oppositeSigns(int a, int b) { return (a…
- What is printed? (Integer overflow on multiplication.) c #include <limits.h> int main(void) { int a = INTMAX; int b = 2; int result = a b…
- What happens? c int arr[3] = {1, 2, 3}; int p = arr; p += 10; p = 99; printf("done");
- What happens? c int compare(const void a, const void b) { return (int )a - (int )b; } int main(void) { int arr[] = {5, 2, 8, 1, 9}…
- Assume 32-bit int. What is printed? c unsigned int a = 0; a--; printf("%u", a);