Back to Practice C Programming

Functions & Storage Classes - Practice MCQs for CCAT

50 Questions Section B: Programming C Programming

Functions & Storage Classes Question Bank for C-CAT

Topic-wise Functions & Storage Classes MCQs for CDAC C-CAT preparation with answers and explanations.

Q1.
What is the correct way to declare a function in C?
Afunc();
Bdeclare func();
Cint func();
Dfunction func();
Show Answer & Explanation

Correct Answer: C - int func();

In C, a function declaration specifies the return type and function name.

Q2.

What will be the output?

void fun()
{
  printf("Hello");
}
int main()
{
  fun();
  return 0;
}
ANo output
BHello
CCompilation error
DRuntime error
Show Answer & Explanation

Correct Answer: B - Hello

The function fun() is called from main(), so it prints Hello.

Q3.
Which type of function does not return any value?
Aint
Bvoid
Cfloat
Dchar
Show Answer & Explanation

Correct Answer: B - void

void functions are used when no value needs to be returned.

Q4.
What is call by value?
APassing address
BPassing reference
CPassing copy of variable
DPassing pointer
Show Answer & Explanation

Correct Answer: C - Passing copy of variable

In call by value, a copy of the variable is passed to the function.

Q5.

What will be the output?

void fun(int x)
{
  x = x + 5;
}
int main()
{
  int a = 10;
  fun(a);
  printf("%d", a);
  return 0;
}
A10
B15
C5
DError
Show Answer & Explanation

Correct Answer: A - 10

Since call by value is used, changes inside function do not affect original variable.

Q6.
What is call by reference implemented using in C?
AVariables
BStructures
CArrays
DPointers
Show Answer & Explanation

Correct Answer: D - Pointers

C uses pointers to simulate call by reference.

Q7.

What will be the output?

void fun(int *x)
{
  *x = *x + 5;
}
int main()
{
  int a = 10;
  fun(&a);
  printf("%d", a);
  return 0;
}
A10
BError
C5
D15
Show Answer & Explanation

Correct Answer: D - 15

Here address of a is passed, so original value gets modified.

Q8.
Which of the following is true about recursion?
AHas no base condition
BUses loop internally
CExecutes once
DFunction calls itself
Show Answer & Explanation

Correct Answer: D - Function calls itself

A recursive function is one that calls itself.

Q9.
Why is base condition important in recursion?
AImproves speed
BCalls main()
CAllocates memory
DStops recursion
Show Answer & Explanation

Correct Answer: D - Stops recursion

Without a base condition, recursion becomes infinite.

Q10.

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;
}
A3
B0
C6
DError
Show Answer & Explanation

Correct Answer: C - 6

This computes sum 3+2+1 using recursion.

Q11.
Which storage class gives variable local scope but retains value between calls?
Astatic
Bextern
Cauto
Dregister
Show Answer & Explanation

Correct Answer: A - static

static variables retain their value even after function execution ends.

Q12.
What is the default storage class of local variables?
Aauto
Bextern
Cstatic
Dregister
Show Answer & Explanation

Correct Answer: A - auto

Local variables are auto by default.

Q13.
Which storage class is used to access global variable from another file?
Astatic
Bextern
Cauto
Dregister
Show Answer & Explanation

Correct Answer: B - extern

extern allows sharing global variables across multiple files.

Q14.

What will be the output?

void fun()
{
  static int x = 0;
  x++;
  printf("%d", x);
}
int main()
{
  fun();
  fun();
  return 0;
}
A11
BError
C01
D12
Show Answer & Explanation

Correct Answer: D - 12

static variable retains its value between function calls.

Q15.
Which storage class suggests compiler to store variable in CPU register?
Aauto
Bstatic
Cextern
Dregister
Show Answer & Explanation

Correct Answer: D - register

register improves access speed by suggesting register storage.

Q16.
Which of the following is true about register variables?
AAddress can be accessed
BStored in RAM
CGlobal scope
DMay not have address
Show Answer & Explanation

Correct Answer: D - May not have address

register variables may not have memory address.

Q17.
What is function prototype?
AFunction body
BFunction call
CFunction declaration
DReturn statement
Show Answer & Explanation

Correct Answer: C - Function declaration

Prototype informs compiler about function return type and parameters.

Q18.
Which of the following supports recursion best?
Afor loop
Bgoto
Cfunctions
Dmacros
Show Answer & Explanation

Correct Answer: C - functions

Recursion is achieved using functions.

Q19.
What happens if return statement is missing in non-void function?
AUndefined behavior
BCompilation error
CReturns 0
DReturns garbage
Show Answer & Explanation

Correct Answer: A - Undefined behavior

Missing return in non-void function leads to undefined behavior.

Q20.
Which storage class limits variable scope to the file only?
Aextern
Bstatic
Cauto
Dregister
Show Answer & Explanation

Correct Answer: B - static

static global variables are restricted to the file scope.

Q21.

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);
}
A15
B5
C10
D0
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.

Q22.

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);
}
A24
B120
C60
D720
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.

Q23.

What is the output?

int counter() {
    static int count = 0;
    count++;
    return count;
}
int main() {
    printf("%d ", counter());
    printf("%d ", counter());
    printf("%d", counter());
}
A1 1 1
B0 1 2
C1 2 3
D3 3 3
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.

Q24.

What is wrong with this function?

int* getLocal() {
    int x = 42;
    return &x;
}
ANothing, it works correctly
BIt returns a dangling pointer to a local variable
CSyntax error in return statement
DCannot return a pointer from a function
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.

Q25.

What does the following function declaration mean?

void process(const int *ptr);
Aptr itself cannot be changed
BThe value pointed to by ptr cannot be modified through ptr
CBoth ptr and the value it points to cannot be changed
DThe function returns a constant
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'.

Q26.

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);
}
A10 20
B0 0
C20 10
DCompilation error
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.

Q27.
What is the return type of the main() function in a standard C program?
Avoid
Bfloat
Cchar
Dint
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.

Q28.

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));
}
A7
B12
C0
DCompilation error
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.

Q29.
What does the 'inline' keyword suggest to the compiler?
AThe function must be expanded at the call site
BThe compiler may replace the function call with the function body
CThe function should not be called recursively
DThe function can only be called once
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.

Q30.

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));
}
A5
B8
C13
D6
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.

Q31.
What happens when a function is declared but not defined, and it is called in the program?
ACompilation error
BRuntime error
CLinker error
DThe function returns 0 by default
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.

Q32.

What is the scope of variable 'x' in this code?

void func() {
    {
        int x = 10;
        printf("%d", x);
    }
    // Can x be used here?
}
Ax is accessible throughout func()
BCompilation error due to nested braces
Cx is a global variable
Dx is accessible only within the inner braces
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.

Q33.

What is the output?

int sum(int n) {
    if(n == 0) return 0;
    return n + sum(n - 1);
}
int main() {
    printf("%d", sum(10));
}
A55
B45
C50
D100
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.

Q34.

What does this function declaration specify?

int compute(int, int);
AAn invalid declaration — parameters must be named
BA function that takes no parameters
CA function that takes two unnamed int parameters and returns int
DA function pointer declaration
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.

Q35.

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);
}
A20 20
B20 4
C20 8
D5 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.

Q36.

What is the output of the following?

int global = 10;
void change() {
    int global = 20;
    printf("%d ", global);
}
int main() {
    change();
    printf("%d", global);
}
A20 10
B10 10
C20 20
D10 20
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.

Q37.

What happens with this recursive function call?

void infinite(int n) {
    printf("%d ", n);
    infinite(n + 1);
}
APrints numbers indefinitely
BCompilation error
CStack overflow / segmentation fault
DPrints nothing
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.

Q38.

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)));
}
A7
B5
C3
D2
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.

Q39.
Which of the following correctly declares a function that takes a variable number of arguments?
Aint func(var args);
Bint func(..., int n);
Cint func(int ...n);
Dint func(int n, ...);
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.

Q40.

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));
}
A128
B256
C64
D16
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.

Q41.
What is the default return type of a function in C89/C90 if no return type is specified?
Aint
Bvoid
Cchar
DNo default; compilation error
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.

Q42.

What is the output?

void greet();
int main() {
    greet();
    return 0;
}
void greet() {
    printf("Hello CDAC!");
}
AHello CDAC!
BCompilation error
CLinker error
DRuntime error
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!'.

Q43.

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));
}
A2
B3
C6
D18
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.

Q44.

What is the problem with this code?

char* getName() {
    char name[20] = "CDAC";
    return name;
}
AReturns address of a local array that will be destroyed
BNo problem, it returns a valid string
CSyntax error in array initialization
DCannot return char* from a function
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.

Q45.
What does the 'extern' keyword do in a function declaration?
AMakes the function private to the current file
BMakes the function recursive
CForces the function to be inlined
DIndicates the function is defined in another file or later in the same file
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.

Q46.

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));
}
A5 10 3
B18 18 18
C5 15 3
D5 15 18
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.

Q47.

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]);
}
A2 4 6
B1 2 3
C0 0 0
DCompilation error
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.

Q48.

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);
}
A1234
B4 3 2 1
C1 2 3 4
D4321
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.

Q49.
Which of the following correctly passes a 2D array to a function?
Avoid func(int arr[][], int rows, int cols);
BBoth B and C are valid approaches
Cvoid func(int **arr, int rows, int cols);
Dvoid func(int arr[][4], int rows);
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.

Q50.

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));
}
A0 1 1 2 3 5 8
B0 1 2 3 4 5 6
C1 1 2 3 5 8 13
D1 2 3 5 8 13 21
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.

Showing 1-10 of 50 questions