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.
Consider the function declaration: void func(int x); and later in the code: int result = func(5);. What will be the compilation result?
ACompilation error because func returns void but is assigned to an int variable
BCompilation warning but will compile successfully; result will be undefined
CSuccessful compilation and result will be 5
DRuntime error will occur
Correct Answer:
A. Compilation error because func returns void but is assigned to an int variable
EXPLANATION
A void function cannot return a value. Attempting to assign the return value of a void function to a variable will cause a compilation error in standard C compilers.
A function in C is declared as: int calculate(int *arr, int size). When this function modifies the array elements, what happens to the original array passed from the calling function?
AThe original array is modified because arrays are passed by reference
BThe original array remains unchanged; only the local copy is modified
CIt depends on whether the pointer is declared as const
DThe modification only affects the first element of the array
Correct Answer:
A. The original array is modified because arrays are passed by reference
EXPLANATION
In C, arrays decay to pointers when passed to functions, meaning the address is passed. Any modifications through the pointer affect the original array. This is effectively pass-by-reference behavior for arrays.
Which of the following demonstrates proper use of the return statement in error handling?
Areturn NULL; for pointer functions to indicate failure
Breturn -1; for integer functions to indicate error status
CUse dedicated error codes or special return values to indicate success/failure
DAll of the above are valid approaches
Correct Answer:
D. All of the above are valid approaches
EXPLANATION
All three approaches are valid for error handling in C. Functions typically return NULL for pointers, negative values for integers, or use predefined error codes to indicate failure states.
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.
What is the purpose of function inlining in modern C compilers?
ATo increase code size for better readability
BTo reduce function call overhead by replacing calls with function body
CTo prevent function pointers from working
DTo enforce external linkage
Correct Answer:
B. To reduce function call overhead by replacing calls with function body
EXPLANATION
Inlining substitutes a function call with the actual function body at compile time, eliminating function call overhead. This is a compiler optimization for performance-critical code.
Consider a function that modifies an array passed as a parameter. Which statement is true?
AThe array is passed by value, so modifications don't affect the original
BThe array decays to a pointer, so modifications affect the original array
CArrays cannot be modified within functions
DThe const qualifier must be used to modify arrays in functions
Correct Answer:
B. The array decays to a pointer, so modifications affect the original array
EXPLANATION
When an array is passed to a function, it decays to a pointer to its first element. This means modifications to array elements affect the original array outside the function.
What happens if a function is declared but never defined in C?
AThe compiler throws an error immediately
BThe program compiles but fails at link time
CThe program executes without issues
DThe compiler generates a default implementation
Correct Answer:
B. The program compiles but fails at link time
EXPLANATION
If a function is declared but not defined, the compiler generates code to call it (compilation succeeds), but the linker fails to find the function definition, resulting in a linker error.