Functions & Storage Classes Question Bank for C-CAT
Topic-wise Functions & Storage Classes MCQs for CDAC C-CAT preparation with answers and explanations.
Show Answer & Explanation
Correct Answer: C - int func();
In C, a function declaration specifies the return type and function name.
What will be the output?
void fun()
{
printf("Hello");
}
int main()
{
fun();
return 0;
}Show Answer & Explanation
Correct Answer: B - Hello
The function fun() is called from main(), so it prints Hello.
Show Answer & Explanation
Correct Answer: B - void
void functions are used when no value needs to be returned.
Show Answer & Explanation
Correct Answer: C - Passing copy of variable
In call by value, a copy of the variable is passed to the function.
What will be the output?
void fun(int x)
{
x = x + 5;
}
int main()
{
int a = 10;
fun(a);
printf("%d", a);
return 0;
}Show Answer & Explanation
Correct Answer: A - 10
Since call by value is used, changes inside function do not affect original variable.
Show Answer & Explanation
Correct Answer: D - Pointers
C uses pointers to simulate call by reference.
What will be the output?
void fun(int *x)
{
*x = *x + 5;
}
int main()
{
int a = 10;
fun(&a);
printf("%d", a);
return 0;
}Show Answer & Explanation
Correct Answer: D - 15
Here address of a is passed, so original value gets modified.
Show Answer & Explanation
Correct Answer: D - Function calls itself
A recursive function is one that calls itself.
Show Answer & Explanation
Correct Answer: D - Stops recursion
Without a base condition, recursion becomes infinite.
What will be the output?
int fun(int n)
{
if(n==0) return 0;
return n + fun(n-1);
}
int main()
{
printf("%d", fun(3));
return 0;
}Show Answer & Explanation
Correct Answer: C - 6
This computes sum 3+2+1 using recursion.
Show Answer & Explanation
Correct Answer: A - static
static variables retain their value even after function execution ends.
Show Answer & Explanation
Correct Answer: A - auto
Local variables are auto by default.
Show Answer & Explanation
Correct Answer: B - extern
extern allows sharing global variables across multiple files.
What will be the output?
void fun()
{
static int x = 0;
x++;
printf("%d", x);
}
int main()
{
fun();
fun();
return 0;
}Show Answer & Explanation
Correct Answer: D - 12
static variable retains its value between function calls.
Show Answer & Explanation
Correct Answer: D - register
register improves access speed by suggesting register storage.
Show Answer & Explanation
Correct Answer: D - May not have address
register variables may not have memory address.
Show Answer & Explanation
Correct Answer: C - Function declaration
Prototype informs compiler about function return type and parameters.
Show Answer & Explanation
Correct Answer: C - functions
Recursion is achieved using functions.
Show Answer & Explanation
Correct Answer: A - Undefined behavior
Missing return in non-void function leads to undefined behavior.
Show Answer & Explanation
Correct Answer: B - static
static global variables are restricted to the file scope.
What is the output of the following code?
void modify(int x) {
x = x + 10;
}
int main() {
int a = 5;
modify(a);
printf("%d", a);
}Show Answer & Explanation
Correct Answer: B - 5
C uses pass-by-value for function arguments. The function modify() receives a copy of 'a', so changes to x inside the function do not affect the original variable 'a' in main(). The value of a remains 5.
What will this recursive function return when called as factorial(5)?
int factorial(int n) {
if(n <= 1) return 1;
return n * factorial(n - 1);
}Show Answer & Explanation
Correct Answer: B - 120
factorial(5) = 5 × factorial(4) = 5 × 4 × factorial(3) = 5 × 4 × 3 × factorial(2) = 5 × 4 × 3 × 2 × factorial(1) = 5 × 4 × 3 × 2 × 1 = 120.
What is the output?
int counter() {
static int count = 0;
count++;
return count;
}
int main() {
printf("%d ", counter());
printf("%d ", counter());
printf("%d", counter());
}Show Answer & Explanation
Correct Answer: C - 1 2 3
The static variable 'count' is initialized only once and retains its value between function calls. First call: count becomes 1. Second call: count becomes 2. Third call: count becomes 3. Output: 1 2 3.
What is wrong with this function?
int* getLocal() {
int x = 42;
return &x;
}Show Answer & Explanation
Correct Answer: B - It returns a dangling pointer to a local variable
The variable x is local to getLocal() and is destroyed when the function returns. Returning &x gives a pointer to memory that is no longer valid (a dangling pointer). Using this pointer leads to undefined behavior.
What does the following function declaration mean?
void process(const int *ptr);Show Answer & Explanation
Correct Answer: B - The value pointed to by ptr cannot be modified through ptr
'const int *ptr' means the integer value pointed to by ptr cannot be modified through this pointer (read-only access). However, ptr itself can be reassigned to point to a different address. To make ptr unchangeable, the syntax would be 'int * const ptr'.
What is the output?
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int x = 10, y = 20;
swap(&x, &y);
printf("%d %d", x, y);
}Show Answer & Explanation
Correct Answer: C - 20 10
The swap function receives pointers to x and y. By dereferencing the pointers (*a, *b), it modifies the original variables. After swap: x becomes 20 and y becomes 10. This is the standard way to simulate pass-by-reference in C.
Show Answer & Explanation
Correct Answer: D - int
According to the C standard (C99 and later), the main() function must return int. The return value indicates the program's exit status to the operating system. Returning 0 typically indicates successful execution.
What will this code output?
int add(int a, int b) { return a + b; }
int mul(int a, int b) { return a * b; }
int apply(int (*func)(int, int), int x, int y) {
return func(x, y);
}
int main() {
printf("%d", apply(add, 3, 4));
}Show Answer & Explanation
Correct Answer: A - 7
The apply function takes a function pointer as its first argument. When called with 'add' and arguments 3, 4, it calls add(3, 4) which returns 3 + 4 = 7.
Show Answer & Explanation
Correct Answer: B - The compiler may replace the function call with the function body
'inline' is a suggestion (not a mandate) to the compiler to replace the function call with the actual function body to avoid the overhead of a function call. The compiler is free to ignore this suggestion based on its own optimization decisions.
What is the output?
int fib(int n) {
if(n <= 0) return 0;
if(n == 1) return 1;
return fib(n-1) + fib(n-2);
}
int main() {
printf("%d", fib(6));
}Show Answer & Explanation
Correct Answer: B - 8
The Fibonacci sequence: fib(0)=0, fib(1)=1, fib(2)=1, fib(3)=2, fib(4)=3, fib(5)=5, fib(6)=8. The function correctly computes the 6th Fibonacci number as 8.
Show Answer & Explanation
Correct Answer: C - Linker error
A function declaration (prototype) tells the compiler about the function's signature, so compilation succeeds. However, since no definition (body) exists, the linker cannot resolve the function reference and reports an 'undefined reference' error.
What is the scope of variable 'x' in this code?
void func() {
{
int x = 10;
printf("%d", x);
}
// Can x be used here?
}Show Answer & Explanation
Correct Answer: D - x is accessible only within the inner braces
Variable x is declared inside a block (inner braces), so its scope is limited to that block. Attempting to use x outside the inner braces but still inside func() would result in a compilation error. This is called block scope.
What is the output?
int sum(int n) {
if(n == 0) return 0;
return n + sum(n - 1);
}
int main() {
printf("%d", sum(10));
}Show Answer & Explanation
Correct Answer: A - 55
sum(10) = 10 + sum(9) = 10 + 9 + sum(8) = ... = 10 + 9 + 8 + ... + 1 + sum(0) = 10+9+8+7+6+5+4+3+2+1+0 = 55. This is the sum of first 10 natural numbers using the formula n(n+1)/2 = 55.
What does this function declaration specify?
int compute(int, int);Show Answer & Explanation
Correct Answer: C - A function that takes two unnamed int parameters and returns int
In C function prototypes (declarations), parameter names are optional. Only the types are required. This declares a function 'compute' that accepts two int arguments and returns an int. The parameter names are only required in the function definition.
What is the output?
void printArr(int arr[], int size) {
printf("%lu ", sizeof(arr));
}
int main() {
int data[] = {1, 2, 3, 4, 5};
printf("%lu ", sizeof(data));
printArr(data, 5);
}Show Answer & Explanation
Correct Answer: C - 20 8
In main(), sizeof(data) gives the total array size: 5 ints × 4 bytes = 20. However, when an array is passed to a function, it decays to a pointer. So sizeof(arr) inside printArr gives the size of a pointer (8 bytes on 64-bit, 4 on 32-bit). Output on 64-bit: 20 8.
What is the output of the following?
int global = 10;
void change() {
int global = 20;
printf("%d ", global);
}
int main() {
change();
printf("%d", global);
}Show Answer & Explanation
Correct Answer: A - 20 10
Inside change(), the local variable 'global' shadows the global variable with the same name. So the local value 20 is printed. In main(), the local variable of change() is out of scope, so the global variable (still 10) is printed. Output: 20 10.
What happens with this recursive function call?
void infinite(int n) {
printf("%d ", n);
infinite(n + 1);
}Show Answer & Explanation
Correct Answer: C - Stack overflow / segmentation fault
This function has no base case to stop recursion. Each recursive call adds a new frame to the call stack. Eventually the stack space is exhausted, causing a stack overflow and typically a segmentation fault. The program will crash after printing some numbers.
What is the output?
int max(int a, int b) {
return (a > b) ? a : b;
}
int main() {
printf("%d", max(max(3, 7), max(5, 2)));
}Show Answer & Explanation
Correct Answer: A - 7
Inner calls: max(3,7) returns 7, max(5,2) returns 5. Outer call: max(7, 5) returns 7. The function is composed to find the maximum of four values.
Show Answer & Explanation
Correct Answer: D - int func(int n, ...);
In C, variadic functions use an ellipsis (...) as the last parameter, with at least one named parameter before it. The <stdarg.h> header provides macros (va_start, va_arg, va_end) to access the variable arguments. Example: printf is a variadic function.
What is the output?
int power(int base, int exp) {
if(exp == 0) return 1;
return base * power(base, exp - 1);
}
int main() {
printf("%d", power(2, 8));
}Show Answer & Explanation
Correct Answer: B - 256
power(2, 8) recursively computes 28. Base case: power(2,0) = 1. Then 2×1=2, 2×2=4, 2×4=8, 2×8=16, 2×16=32, 2×32=64, 2×64=128, 2×128=256. So 28 = 256.
Show Answer & Explanation
Correct Answer: A - int
In C89/C90 (ANSI C), if a function is defined without a return type, it defaults to returning int. This was called 'implicit int'. However, C99 and later standards removed this rule and require explicit return type specification.
What is the output?
void greet();
int main() {
greet();
return 0;
}
void greet() {
printf("Hello CDAC!");
}Show Answer & Explanation
Correct Answer: A - Hello CDAC!
The function prototype 'void greet();' at the top allows the compiler to know about greet() before it is called in main(). The function is defined after main(), which is perfectly valid. The program compiles, links, and prints 'Hello CDAC!'.
What is the output?
int gcd(int a, int b) {
if(b == 0) return a;
return gcd(b, a % b);
}
int main() {
printf("%d", gcd(48, 18));
}Show Answer & Explanation
Correct Answer: C - 6
Using Euclid's algorithm: gcd(48,18) → gcd(18, 48%18=12) → gcd(12, 18%12=6) → gcd(6, 12%6=0) → returns 6. The GCD of 48 and 18 is 6.
What is the problem with this code?
char* getName() {
char name[20] = "CDAC";
return name;
}Show Answer & Explanation
Correct Answer: A - Returns address of a local array that will be destroyed
The array 'name' is local to getName() and its memory is deallocated when the function returns. Returning a pointer to this local array creates a dangling pointer. The caller would access invalid memory. To fix this, use static, malloc, or pass the buffer as a parameter.
Show Answer & Explanation
Correct Answer: D - Indicates the function is defined in another file or later in the same file
'extern' declares that a function (or variable) is defined elsewhere — either in another source file or later in the same file. For functions, extern is the default linkage, so 'extern int func();' is equivalent to 'int func();'. It is more commonly used with variables.
What is the output?
int accumulate(int val) {
static int total = 0;
total += val;
return total;
}
int main() {
printf("%d ", accumulate(5));
printf("%d ", accumulate(10));
printf("%d", accumulate(3));
}Show Answer & Explanation
Correct Answer: D - 5 15 18
The static variable 'total' persists between calls. First call: total = 0+5 = 5. Second call: total = 5+10 = 15. Third call: total = 15+3 = 18. Output: 5 15 18.
What is the output?
void doubleValues(int arr[], int n) {
for(int i = 0; i < n; i++)
arr[i] *= 2;
}
int main() {
int nums[] = {1, 2, 3};
doubleValues(nums, 3);
printf("%d %d %d", nums[0], nums[1], nums[2]);
}Show Answer & Explanation
Correct Answer: A - 2 4 6
Arrays in C are passed by reference (actually, a pointer to the first element is passed). Changes made to arr[] inside doubleValues() directly modify the original array nums[]. So each element is doubled: 1→2, 2→4, 3→6. Output: 2 4 6.
What is the output of this recursive function?
void printReverse(int n) {
if(n == 0) return;
printf("%d ", n % 10);
printReverse(n / 10);
}
int main() {
printReverse(1234);
}Show Answer & Explanation
Correct Answer: B - 4 3 2 1
printReverse(1234): prints 1234%10=4, calls printReverse(123). printReverse(123): prints 123%10=3, calls printReverse(12). printReverse(12): prints 12%10=2, calls printReverse(1). printReverse(1): prints 1%10=1, calls printReverse(0) which returns. Output: 4 3 2 1.
Show Answer & Explanation
Correct Answer: B - Both B and C are valid approaches
Option B passes a 2D array with a known second dimension — the compiler needs column count for address calculation. Option C passes a pointer to pointer, which works differently (array of pointers to rows). Both are valid approaches depending on how the array is allocated. Option A is invalid because at least the second dimension must be specified.
What is the output?
int mystery(int n) {
if(n < 2) return n;
return mystery(n - 1) + mystery(n - 2);
}
int main() {
for(int i = 0; i < 7; i++)
printf("%d ", mystery(i));
}Show Answer & Explanation
Correct Answer: A - 0 1 1 2 3 5 8
This is the Fibonacci sequence. mystery(0)=0, mystery(1)=1, mystery(2)=1, mystery(3)=2, mystery(4)=3, mystery(5)=5, mystery(6)=8. Output: 0 1 1 2 3 5 8.