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.
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.
Which of the following is a valid way to return multiple values from a function in C?
AUsing a struct to encapsulate multiple values
BUsing pointers to modify caller's variables
CUsing global variables
DAll of the above
Correct Answer:
D. All of the above
EXPLANATION
C provides multiple methods to return values: structs for encapsulation, pointers for indirect modification, and global variables (though not recommended). All three are technically valid approaches.
What is the primary purpose of function pointers in C?
ATo allocate memory dynamically
BTo provide polymorphism and dynamic function dispatch
CTo declare static functions
DTo prevent function inlining
Correct Answer:
B. To provide polymorphism and dynamic function dispatch
EXPLANATION
Function pointers enable polymorphism by allowing runtime selection of which function to call. They are crucial for implementing callbacks and dynamic function dispatch.
Which statement about extern functions is correct?
Aextern functions can only be used within the same file
Bextern functions are accessible across multiple translation units
Cextern functions must be defined in the header file
Dextern is mandatory for all function declarations
Correct Answer:
B. extern functions are accessible across multiple translation units
EXPLANATION
extern functions have external linkage and can be accessed from other translation units. It's the default linkage for functions declared outside a file.
What does the const qualifier do when applied to function parameters?
AMakes the parameter immutable within the function
BMakes the parameter a global constant
CPrevents the function from being called multiple times
DHas no effect in function parameters
Correct Answer:
A. Makes the parameter immutable within the function
EXPLANATION
When const is applied to a function parameter, it prevents modification of that parameter within the function body, making it immutable for the duration of the function.
Which of the following correctly demonstrates function recursion termination?
AA recursive function must always have a return statement
BA recursive function must have a base case to prevent infinite recursion
CRecursion automatically terminates after 100 calls
DThe compiler prevents infinite recursion
Correct Answer:
B. A recursive function must have a base case to prevent infinite recursion
EXPLANATION
A base case is essential in recursive functions to provide a stopping condition. Without it, the function will cause stack overflow. The base case ensures recursion terminates properly.