Function C Programming Mock Test – 1


Question 1
What is the output of the following program?
#include < stdio.h > 
void fun(int n)
{
    int k=0;
    printf("%d ",n);
    n--;
    k++;
    if(n<0)return;
    fun(n-1);
    printf("%d ",k);
    printf("%d ",n);
}

int main()
{
    int i=3;
    fun(i);
    return 0;
}
A
3 1 -1 3 1 3 1
B
3 1 -1 3 0 3 2
C
3 1 -1 3 3 3 2
D
3 1 -1 1 0 1 2
   C Programming
Question 1 Explanation: 
This questions is based on the recursive function with call by value concept and all local variable in the function.

NOTE: For each function separate copy of it's local variable is maintained by compiler before transferring call to next function.

Try to understand with the easy technique to solve this types of questions.
First gives the number to all important lines of function as shown in figure.

Then start with first function call as shown in below figure. Maintain the separate copy of local variables for each function call as shown in below figure.
Then execute function line by line as per number given by you as shown in above figure. Execute one by one line as shown in below figure and whenever function call is there transfer the control to next function. When function return or complete (execute last line) it return to the function from where it gets called.


OUTPUT : 3 1 -1 1 0 1 2
Click to Join Our Telegram Group for Latest Update of MOCK TEST

Click Here to Practice ALL Previous MOCK TEST FREE
Question 2
What is the output of the following program?
#include < stdio.h > 
void fun(int n)
{
    static int k=0;
    printf("%d ",n);
    n--;
    k++;
    if(n<0)return;
    fun(n-1);
    printf("%d ",k);
    printf("%d ",n);
}
 int main()
{
    int i=3;
    fun(i);
    return 0;
}
A
3 1 -1 3 1 3 1
B
3 1 -1 3 0 3 2
C
3 1 -1 3 3 3 2
D
3 1 -1 1 0 1 2
   C Programming
Question 2 Explanation: 
This questions is based on the recursive function with call by value concept and local and static variable in the function.

NOTE: For each function separate copy of it's local variable is maintained by compiler before transferring call to next function
while global single copy of static variable is maintained by compiler which is accessed by all functions.

Try to understand with the easy technique to solve this types of questions.
First gives the number to all important lines of function as shown in figure.

Then start with first function call as shown in below figure. Maintain the separate copy of local variables for each function call as shown in below figure. For static variable create only single copy which gets accessed by all function as shown in below figure with variable k.
Then execute function line by line as per number given by you as shown in above figure. Execute one by one line as shown in below figure and whenever function call is there transfer the control to next function. When function return or complete (execute last line) it return to the function from where it gets called.


OUTPUT : 3 1 -1 3 0 3 2
Click to Join Our Telegram Group for Latest Update of MOCK TEST

Click Here to Practice ALL Previous MOCK TEST FREE


Question 3
What is the output of the following program?
#include < stdio.h > 
void fun(int *n)
{
    static int k=0;
    printf("%d ",*n);
    (*n)--;
    k++;
    if(*n<0)return;
    fun(n);
    printf("%d ",k);
    printf("%d ",*n);
}

int main()
{
    int i=2;
    fun(&i);
    return 0;
}
A
2 1 0 1 -1 1 -1
B
2 1 0 2 -1 1 -1
C
2 1 0 3 -1 3 -1
D
2 1 0 3 1 3 1
   C Programming
Question 3 Explanation: 
This questions is based on the recursive function with call by address concept and local and static variable in the function.

NOTE: For each function separate copy of it's local variable is maintained by compiler before transferring call to next function
while global single copy of static variable is maintained by compiler which is accessed by all functions.
in call by address, when we dereference the pointer variable in function, it refer to the variable whose address is passed during function call.

Try to understand with the easy technique to solve this types of questions.
First gives the number to all important lines of function as shown in figure.

Then start with first function call as shown in below figure. Maintain the separate copy of local variables for each function call as shown in below figure. For static variable create only single copy which gets accessed by all function as shown in below figure with variable k.
Then execute function line by line as per number given by you as shown in above figure. Execute one by one line as shown in below figure and whenever function call is there transfer the control to next function. When function return or complete (execute last line) it return to the function from where it gets called.


OUTPUT : 2 1 0 3 -1 3 -1
Click to Join Our Telegram Group for Latest Update of MOCK TEST

Click Here to Practice ALL Previous MOCK TEST FREE
Question 4
What is the output of the following program?
#include < stdio.h > 
int fun(int n)
{
    int k=0;
    if(n==0)return 1;
    n--;
    k++;
    return fun(n)*k;
}

int main()
{
    int i=5;
    printf("%d",fun(i));
    return 0;
}
A
625
B
1
C
3125
D
5
   C Programming
Question 4 Explanation: 
This questions is based on the recursive function with call by value concept and all local variable in the function.

NOTE: For each function separate copy of it's local variable is maintained by compiler before transferring call to next function.

Try to understand with the easy technique to solve this types of questions.
First gives the number to all important lines of function as shown in figure.

Then start with first function call as shown in below figure. Maintain the separate copy of local variables for each function call as shown in below figure.
Then execute function line by line as per number given by you as shown in above figure. Execute one by one line as shown in below figure and whenever function call is there transfer the control to next function. When function return or complete (execute last line) it return to the function from where it gets called.


OUTPUT : 1
Click to Join Our Telegram Group for Latest Update of MOCK TEST

Click Here to Practice ALL Previous MOCK TEST FREE
Question 5
What is the output of the following program?
#include < stdio.h > 
int fun(int n)
{
    static int k=0;
    if(n==0)return 1;
    n--;
    k++;
    return fun(n)*k;
}

int main()
{
    int i=5;
    printf("%d",fun(i));
    return 0;
}
A
625
B
1
C
3125
D
5
   C Programming
Question 5 Explanation: 
This questions is based on the recursive function with call by value concept and local and static variable in the function.

NOTE: For each function separate copy of it's local variable is maintained by compiler before transferring call to next function
while global single copy of static variable is maintained by compiler which is accessed by all functions.

Try to understand with the easy technique to solve this types of questions.
First gives the number to all important lines of function as shown in figure.

Then start with first function call as shown in below figure. Maintain the separate copy of local variables for each function call as shown in below figure. For static variable create only single copy which gets accessed by all function as shown in below figure with variable k.
Then execute function line by line as per number given by you as shown in above figure. Execute one by one line as shown in below figure and whenever function call is there transfer the control to next function. When function return or complete (execute last line) it return to the function from where it gets called.


OUTPUT : 3125
Click to Join Our Telegram Group for Latest Update of MOCK TEST

Click Here to Practice ALL Previous MOCK TEST FREE


There are 5 questions to complete.

2 thoughts on “Function C Programming Mock Test – 1”

Leave a Comment