How can you modify a variable inside a function and reflect the change in the calling function?
Answer: D
All three methods work: return value, pass pointers, or use global variables. Each has different use cases and implications.
Q.2Hard
What is the output of this code?
int* getPointer(int *p) { return p; }
int main() { int a = 10; int *ptr = getPointer(&a); printf("%d", *ptr); }
Answer: A
The function receives the address of 'a', returns it, and dereferencing gives 10. However, returning pointers to local variables is risky in other contexts.
Q.3Hard
What are static local variables in functions used for?
Answer: A
Static local variables retain their value between function calls, initialized only once, and are stored in data segment.
Q.4Hard
What will be the output?
int counter() { static int count = 0; return ++count; }
int main() { printf("%d ", counter()); printf("%d ", counter()); printf("%d", counter()); }
Answer: B
Static variable 'count' retains its value. First call: 0+1=1, second: 1+1=2, third: 2+1=3.
Q.5Hard
Which of the following is TRUE about inline functions in C99 standard?
Answer: B
The 'inline' keyword is a hint to the compiler to replace function calls with the function body, reducing overhead but increasing code size.
Advertisement
Q.6Hard
What is function overloading in the context of C?
Answer: B
C does not support function overloading. To achieve similar functionality, use different names or variadic functions (using ...) or union of function pointers.
Q.7Hard
In C99 and later standards, what is the maximum number of parameters a function can have?
Answer: D
While C99 guarantees support for at least 127 parameters, the actual limit is implementation-dependent and varies by compiler.
Q.8Hard
What is the primary advantage of using function pointers in C?
Answer: B
Function pointers enable runtime polymorphism by allowing selection of which function to call based on runtime conditions.
Q.9Hard
How does the volatile keyword affect function behavior in C?
Answer: B
The volatile keyword tells the compiler not to optimize away variable accesses, ensuring the variable is read from memory each time.
Q.10Hard
What will happen if a recursive function has no base case?
Answer: B
Without base case, recursion never stops, causing stack to overflow as return addresses keep accumulating.
Q.11Hard
Consider mutual recursion: void funcA(int n); void funcB(int n); funcA calls funcB and vice versa. What is required?
Answer: A
For mutual recursion, you need forward declaration (prototype) of at least one function before it's called by another.
Q.12Hard
What is the correct way to return multiple values from a function in C?
Answer: D
C doesn't support multiple return values. Both pointers (call-by-reference) and structs are valid techniques.
Q.13Hard
Analyze: int (*funcPtr)(int, int); What does this declaration represent?
Answer: B
The syntax (*funcPtr) indicates funcPtr is a pointer. It points to functions taking two ints and returning int.
Q.14Hard
In C99 standard, what is true about function declarations at block scope?
Answer: B
C99 allows function declarations within blocks; they have scope limited to that block. This is different from function definitions which remain at file scope.
Q.15Hard
Which approach is more efficient for passing large structures to functions?
Answer: B
Passing large structures by pointer is more efficient as only address (typically 4-8 bytes) is copied, not entire structure.
Q.16Hard
What does the restrict keyword do when used with pointer parameters in C99?
Answer: B
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.
Q.17Hard
What will this code output?
int x = 10;
int* getAddress() { return &x; }
main() { int *ptr = getAddress(); printf("%d", *ptr); }
Answer: A
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.
Q.18Hard
What happens when a function with variadic parameters is called with fewer arguments than expected?
Answer: C
Variadic functions (using ...) don't validate argument count. Calling with fewer arguments leads to undefined behavior as the function reads undefined values from stack.
Q.19Hard
How are function arguments evaluated in C?
Answer: C
The C standard does not specify the order of evaluation of function arguments. The order is implementation-dependent and can vary between compilers.
Q.20Hard
In the context of function pointers, what does the restrict keyword do?
Answer: B
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.