Defining and Calling Functions MCQs in C Language

Defining and Calling Functions MCQs in C Language


1.

What is the output?

#include <stdio.h>
void fun() {
    printf("Hello");
}
int main() {
    fun();
    return 0;
}

A) Hello
B) fun
C) main
D) Compiler error

Answer: A
Explanation:
Function fun() is defined and called inside main() → prints “Hello”.


2.

Which of the following is correct for function declaration?

A) int fun();
B) fun int();
C) void int fun();
D) int() fun;

Answer: A
Explanation:
C function declaration syntax → return_type function_name(parameters);.


3.

Output:

#include <stdio.h>
int add(int a, int b) {
    return a+b;
}
int main() {
    printf("%d", add(2,3));
}

A) 23
B) 5
C) 6
D) 0

Answer: B
Explanation:
add(2,3) returns 5 → printed by printf.


4.

Which of the following is function definition with no return value?

A) int fun() {}
B) void fun() {}
C) float fun() {}
D) char fun() {}

Answer: B
Explanation:
void → indicates no value returned.


5.

What is output?

#include <stdio.h>
void display(int x) {
    x += 5;
    printf("%d", x);
}
int main() {
    int a=10;
    display(a);
    printf("%d", a);
}

A) 1510
B) 1015
C) 15 10
D) 10 15

Answer: C
Explanation:
display receives a copy of a → increments inside function → prints 15 → a in main unchanged → prints 10.


6.

Which is correct call to a function int sum(int, int);?

A) sum(2,3);
B) int sum(2,3);
C) int sum = sum(2,3);
D) sum(int 2, int 3);

Answer: A
Explanation:
Function is called with actual parameters → sum(2,3).


7.

What is the output?

#include <stdio.h>
int fun() {
    return 5;
}
int main() {
    printf("%d", fun());
}

A) fun
B) 5
C) main
D) Compiler error

Answer: B
Explanation:
fun() returns 5 → printed by printf.


8.

Which of the following is function prototype?

A) int sum();
B) void sum(int,int);
C) Both A and B
D) int sum(int a,int b) { }

Answer: C
Explanation:
Both declarations are valid prototypes; definition is different.


9.

Output:

#include <stdio.h>
void swap(int a, int b) {
    int t = a;
    a = b;
    b = t;
}
int main() {
    int x=2,y=3;
    swap(x,y);
    printf("%d %d", x,y);
}

A) 3 2
B) 2 3
C) 5 5
D) 0 0

Answer: B
Explanation:
C passes arguments by value → original x,y unchanged.


10.

Which is correct function returning float?

A) float fun();
B) void fun();
C) int fun();
D) char fun();

Answer: A
Explanation:
float → specifies return type of function.


11.

Output:

#include <stdio.h>
void fun(int a, int b) {
    printf("%d", a*b);
}
int main() {
    fun(2,3);
}

A) 5
B) 6
C) 23
D) 0

Answer: B
Explanation:
2*3=6 → printed.


12.

Which of the following is invalid function call?

A) fun();
B) fun(5);
C) fun(1,2);
D) fun(1,2,3);

Answer: D
Explanation:
Function expects 2 parameters → passing 3 causes error.


13.

Output:

#include <stdio.h>
int factorial(int n) {
    if(n==0) return 1;
    return n*factorial(n-1);
}
int main() {
    printf("%d", factorial(3));
}

A) 6
B) 3
C) 9
D) 0

Answer: A
Explanation:
3! = 321 = 6 → printed.


14.

Which statement is TRUE about recursion?

A) Recursion cannot have base case
B) Base case is optional
C) Recursion must have a base case
D) Recursion never returns value

Answer: C
Explanation:
Base case is needed to terminate recursion, otherwise infinite recursion.


15.

Output:

#include <stdio.h>
void add(int *a, int *b) {
    *a = *a + *b;
}
int main() {
    int x=2,y=3;
    add(&x,&y);
    printf("%d", x);
}

A) 2
B) 3
C) 5
D) 6

Answer: C
Explanation:
Passing by pointer → modifies original x → 2+3=5 → prints 5.


16.

Which of the following is valid recursion?

A) void fun() { fun(); }
B) int fun() { return fun(); }
C) int fun(int n) { if(n==0) return 0; return fun(n-1); }
D) All of the above

Answer: D
Explanation:
All are syntactically valid; only base case prevents infinite recursion.


17.

Output:

#include <stdio.h>
int sum(int a,int b) {
    return a+b;
}
int main() {
    printf("%d", sum(2+3,4+5));
}

A) 14
B) 11
C) 12
D) 9

Answer: A
Explanation:
sum(5,9) → returns 14 → printed.


18.

Which of the following is TRUE for function declaration?

A) Only return type mandatory
B) Parameter names mandatory
C) Parameter types mandatory
D) None

Answer: C
Explanation:
Parameter types mandatory; names optional in prototype.


19.

Output:

#include <stdio.h>
void display() {
    printf("Hi ");
}
int main() {
    display();
    display();
}

A) Hi
B) Hi Hi
C) HiHi
D) Compiler error

Answer: C
Explanation:
display() prints “Hi” twice → output concatenated → HiHi.


20.

Output:

#include <stdio.h>
int sum(int a,int b) {
    return a+b;
}
int main() {
    int (*ptr)(int,int) = sum;
    printf("%d", ptr(2,3));
}

A) Compiler error
B) 23
C) 5
D) 6

Answer: C
Explanation:
Function pointer ptr calls sum(2,3) → 5 → printed.

21.

Output:

#include <stdio.h>
void fun(int a=5) {
    printf("%d", a);
}
int main() {
    fun();
}

A) 0
B) 5
C) Compiler error
D) Nothing

Answer: C
Explanation:
C does not support default arguments (C++ only) → compiler error.


22.

Output:

#include <stdio.h>
int sum(int a, int b) {
    return a+b;
}
int main() {
    int x=2,y=3;
    printf("%d", sum(x,y)+sum(y,x));
}

A) 10
B) 12
C) 5
D) 6

Answer: A
Explanation:
sum(2,3)=5, sum(3,2)=5 → 5+5=10 → printed.


23.

Which statement is TRUE about call by value?

A) Original variable is changed
B) Function receives a copy of variable
C) Pointers not allowed
D) Always faster

Answer: B
Explanation:
Call by value passes a copy → original variable unaffected.


24.

Output:

#include <stdio.h>
void increment(int *p) {
    (*p)++;
}
int main() {
    int x=5;
    increment(&x);
    printf("%d", x);
}

A) 5
B) 6
C) Compiler error
D) 0

Answer: B
Explanation:
Passing pointer → modifies original → x=6.


25.

Which is correct recursive factorial function?

A) int fact(int n) { return n*fact(n-1); }
B) int fact(int n) { if(n==0) return 1; return n*fact(n-1); }
C) int fact(int n) { return 1; }
D) void fact(int n) { return n*fact(n-1); }

Answer: B
Explanation:
Base case n==0 ensures termination → returns factorial correctly.


26.

Output:

#include <stdio.h>
int fun() {
    int x=5;
    return x++;
}
int main() {
    printf("%d", fun());
}

A) 5
B) 6
C) Compiler error
D) 0

Answer: A
Explanation:
Post-increment returns value before increment → prints 5.


27.

Output:

#include <stdio.h>
void swap(int *a,int *b) {
    int t=*a; *a=*b; *b=t;
}
int main() {
    int x=2,y=3;
    swap(&x,&y);
    printf("%d %d", x,y);
}

A) 2 3
B) 3 2
C) 5 5
D) Compiler error

Answer: B
Explanation:
Passing pointers → original variables swapped → 3 2.


28.

Which of the following cannot be passed by reference in C?

A) int
B) float
C) array
D) int literal

Answer: D
Explanation:
Cannot pass literal addresses → only variables or arrays.


29.

Output:

#include <stdio.h>
int add(int a,int b) {
    int c = a+b;
}
int main() {
    printf("%d", add(2,3));
}

A) 5
B) 0
C) Garbage
D) Compiler error

Answer: C
Explanation:
add() has no return → behavior undefined → prints garbage.


30.

Which is TRUE about function pointers?

A) Can point to functions
B) Can store return value
C) Cannot be passed as arguments
D) Always void type

Answer: A
Explanation:
Function pointers store function addresses → callable via pointer.


31.

Output:

#include <stdio.h>
int sum(int a,int b) {
    return a+b;
}
int main() {
    int (*ptr)(int,int)=sum;
    printf("%d", ptr(4,5));
}

A) Compiler error
B) 45
C) 9
D) 10

Answer: C
Explanation:
sum(4,5)=9 → function pointer ptr calls sum → prints 9.


32.

Output:

#include <stdio.h>
void fun() {
    printf("Hi");
}
int main() {
    void (*fp)()=fun;
    fp();
}

A) Hi
B) fp
C) Compiler error
D) Nothing

Answer: A
Explanation:
Function pointer fp calls fun() → prints Hi.


33.

Which of the following is invalid?

A) int sum(int,int);
B) int sum(int a,int b);
C) int sum(a,b); int a,b;
D) int sum(int a=5,int b=5);

Answer: D
Explanation:
C does not support default arguments (C++ only).


34.

Output:

#include <stdio.h>
void fun(int a) {
    printf("%d", a*2);
}
int main() {
    fun(3+2);
}

A) 5
B) 10
C) 6
D) 3

Answer: B
Explanation:
fun(3+2) → fun(5) → 5*2=10 → printed.


35.

Output:

#include <stdio.h>
void fun(int n) {
    if(n==0) return;
    printf("%d", n);
    fun(n-1);
}
int main() {
    fun(3);
}

A) 123
B) 321
C) 3
D) Compiler error

Answer: B
Explanation:
Recursion → prints 3,2,1 → B.


36.

Output:

#include <stdio.h>
void fun(int *x) {
    (*x)++;
}
int main() {
    int a=10;
    fun(&a);
    printf("%d", a);
}

A) 10
B) 11
C) Compiler error
D) 0

Answer: B
Explanation:
Passing pointer → increments original variable → prints 11.


37.

Which is TRUE about recursion?

A) Base case optional
B) Cannot return values
C) Each call has its own stack frame
D) Only one recursion allowed

Answer: C
Explanation:
Each recursive call stores its parameters and local variables in stack → own frame.


38.

Output:

#include <stdio.h>
int sum(int a,int b) {
    static int c=0;
    c += a+b;
    return c;
}
int main() {
    printf("%d ", sum(2,3));
    printf("%d", sum(1,1));
}

A) 5 2
B) 5 7
C) 5 6
D) 0 0

Answer: B
Explanation:
Static variable retains value → first call: c=5 → second: c=5+2=7.


39.

Output:

#include <stdio.h>
int fun(int n) {
    if(n==0) return 0;
    return n + fun(n-1);
}
int main() {
    printf("%d", fun(3));
}

A) 6
B) 3
C) 0
D) 9

Answer: A
Explanation:
3+2+1+0=6 → printed.


40.

Output:

#include <stdio.h>
void fun(int a,int b) {
    int t=a; a=b; b=t;
}
int main() {
    int x=1,y=2;
    fun(x,y);
    printf("%d %d", x,y);
}

A) 2 1
B) 1 2
C) 3 3
D) Compiler error

Answer: B
Explanation:
Call by value → original x,y unchanged → prints 1 2.