What will be the output of this code?
int counter = 0;
void increment() { static int count = 0; count++; counter++; }
main() { increment(); increment(); printf("%d %d", count, counter); }
A0 2
B2 2
CCompilation error - 'count' undeclared
D2 0
Correct Answer:
C. Compilation error - 'count' undeclared
EXPLANATION
The static variable 'count' is local to increment() and inaccessible from main(). Trying to print 'count' causes compilation error: undefined identifier.
Which of the following correctly declares a variadic function?
Aint func(int x, ...);
Bint func(...);
Cint func(int ...x);
Dint func(int x, int ...);
Correct Answer:
A. int func(int x, ...);
EXPLANATION
Variadic functions use ... syntax, and at least one named parameter must precede it. Option A is correct; Option B violates requirement of at least one named parameter.
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.
In C, what is the purpose of the return statement in a void function?
AIt is optional and not needed
BTo return 0 by default
CTo exit the function immediately without returning a value
DTo cause compilation error if used
Correct Answer:
C. To exit the function immediately without returning a value
EXPLANATION
In void functions, return statement (without value) can be used to exit the function prematurely. It's optional at the end since function exits automatically.
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.
Which approach correctly implements a function returning multiple values?
AUsing a struct containing multiple fields
BUsing pointers as output parameters
CUsing global variables modified by function
DA and B are both correct approaches
Correct Answer:
D. A and B are both correct approaches
EXPLANATION
Both approaches are valid in C: returning a struct with multiple fields or using pointers as output parameters. Using globals is possible but not recommended.
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.
Consider the function: void func(int *arr, int size). Which statement is correct?
Aarr is a local copy of the original array
Barr receives the address of the first element, allowing modification of original
CThe function cannot access the original array
Darr and size must be passed by pointers
Correct Answer:
B. arr receives the address of the first element, allowing modification of original
EXPLANATION
Array names decay to pointers in function calls. The parameter arr receives the address of the first element, so modifications affect the original array.