In a recursive function implementing factorial calculation, the base case is missing. What is the most likely consequence during execution?
AThe program will run infinitely without crashing
BStack overflow due to infinite recursive calls until memory is exhausted
CThe function will automatically return 1 when n becomes 0
DA compile-time error will be generated
Correct Answer:
B. Stack overflow due to infinite recursive calls until memory is exhausted
EXPLANATION
Without a base case, recursion never terminates. Each function call pushes data onto the stack. Eventually, the stack memory is exhausted, causing a stack overflow error. This is a runtime error, not a compile-time error.
In C, what is the relationship between array parameters and pointers in functions?
AArrays and pointers are identical in function parameters
BArray parameters are automatically converted to pointers to the first element
CPointers cannot be used where arrays are expected
DArrays maintain their size information when passed to functions
Correct Answer:
B. Array parameters are automatically converted to pointers to the first element
EXPLANATION
When an array is used as a function parameter, it undergoes array-to-pointer decay, converting to a pointer to the first element. This is why size information is lost.
AA function that returns to the caller immediately
BA function pointer passed as an argument to be invoked later
CA function that calls itself recursively
DA function declared with the callback keyword
Correct Answer:
B. A function pointer passed as an argument to be invoked later
EXPLANATION
A callback is a function pointer passed to another function, which then invokes it at some point. Callbacks are essential for event-driven programming and implementing custom behavior.
Which of the following about variadic functions is true?
AThey must have at least one fixed parameter before the ellipsis
BThey can have only variadic parameters
CType checking is enforced by the compiler for variadic arguments
DThe va_list macro automatically handles type conversion
Correct Answer:
A. They must have at least one fixed parameter before the ellipsis
EXPLANATION
Variadic functions must have at least one fixed parameter before the ellipsis (...). This allows the function to know where variadic arguments begin. Type checking is NOT enforced for variadic arguments.
In the context of function pointers, what does the restrict keyword do?
ARestricts the pointer from pointing to different functions
BTells the compiler that the pointer is the only way to access the data
CMakes the function inaccessible from other files
DPrevents pointer arithmetic
Correct Answer:
B. Tells the compiler that the pointer is the only way to access the data
EXPLANATION
The restrict keyword informs the compiler that the pointer is the sole means of accessing the data it points to, allowing for optimization. It's a hint for compiler optimization.
What happens when a function with variadic parameters is called with fewer arguments than expected?
ACompilation error occurs
BRemaining arguments are initialized to 0
CUndefined behavior - garbage values are read
DFunction call is rejected at runtime
Correct Answer:
C. Undefined behavior - garbage values are read
EXPLANATION
Variadic functions (using ...) don't validate argument count. Calling with fewer arguments leads to undefined behavior as the function reads undefined values from stack.
What will this code output?
int x = 10;
int* getAddress() { return &x; }
main() { int *ptr = getAddress(); printf("%d", *ptr); }
A10
BAddress of x
CUndefined behavior (dangling pointer would occur if x was local)
DCompilation error
Correct Answer:
A. 10
EXPLANATION
Since x is static (global scope), returning its address is safe and the pointer remains valid. The output is 10. If x were local, it would be undefined behavior.
What does the restrict keyword do when used with pointer parameters in C99?
APrevents the pointer from being modified
BTells compiler that pointer is not aliased, enabling optimizations
CRestricts pointer to point only to local variables
DMakes pointer read-only
Correct Answer:
B. Tells compiler that pointer is not aliased, enabling optimizations
EXPLANATION
The restrict keyword (C99) is a promise to the compiler that the pointer is the only way to access that memory during its lifetime, allowing aggressive optimizations.