C Recursion MCQ


Q1.What will be the output of the following C code?
#include
main()
{
    int n;
    n=f1(4);
    printf("%d",n);
}
f1(int x)
{
    int b;
    if(x==1)
        return 1;
    else
        b=x*f1(x-1);
        return b;
}
  1. 24
  2. 4
  3. 12
  4. 10

Answer:- (A).
Explanations :The above code returns the factorial of a given number using the method of recursion. The given number is 4 in the above code, hence the factorial of 4, that is, 24 will be returned.
Q2.The above code returns the factorial of a given number using the method of recursion. The given number is 4 in the above code, hence the factorial of 4, that is, 24 will be returned.
  1. Arrey
  2. Linked list
  3. Binary tree
  4. Stack

Answer:- (D).
Explanations : The compiler uses the data type stack for implementing normal as well as recursive function calls.
Q3. The principle of stack is __________?
  1. First in first out
  2. First in last out
  3. Last in first out
  4. Last in last out

Answer:- (C).
Explanations : A stack is a last in first out(LIFO) data type. This means that the last item to get stored in the stack is the first item to get out of it.
Q4. In the absence of a exit condition in a recursive function, the following error is given __________
  1. Compile time error
  2. Run time error
  3. Run time error
  4. No error

Answer:- (B).
Explanations : When a recursive function is called in the absence of an exit condition, it results in an infinite loop due to which the stack keeps getting filled(stack overflow). This results in a run time error.
Q5. What will be the output of the following C code??
#include
main()
{
    int n,i;
    n=f(6);
    printf("%d",n);
}
f(int x)
{
    if(x==2)
            return 2;
    else
    {
        printf("+");
        f(x-1);
    }
}
  1. ++++2
  2. +++++2
  3. +++++
  4. 2

Answer:- (A).
Explanations :When x=6: ‘+’ is printed.
When x=5: ‘+’ is printed.
When x=4: ‘+’ is printed.
When x=3: ‘+’ is printed.
When x=2: 2 is printed.
Hence the output is: ++++2.
Q6. How many times is ‘a’ printed when the following C code is executed?
#include
main()
{
    int a;
    a=f1(10);
    printf("%d",a);
}
f1(int b)
{
    if(b==0)
        return 0;
    else
    {
        printf("a");
        f1(b--);
    }
}
  1. 9 times
  2. 10 times
  3. 0 times
  4. 0 times/li>

Answer:- (D).
Explanations :Although we have specified the exit condition, the code above results in an infinite loop because we have used b- -(decrement operator) to call the recursive function. Due to this, the loop goes on infinitely. However, if we had used f1(b-1) instead, the answer would have been 10 times.
Q7. What will be the output of the following C code?
#include
main()
{
    int n=10;
    int f(int n);
    printf("%d",f(n));
}
int f(int n)
{
    if(n>0)
        return(n+f(n-2));
}
  1. 10
  2. 80
  3. 80
  4. 80

Answer:- (C).
Explanations :The recursive function returns n+f(n-2) till 10>0.
Therefore, the above code will be evaluated as: 10+8+6+4+2, which is equal to 30.
Q8. What will be the output of the following C code?
#include
int main()
{
    printf("Hello");
    main();
    return 0;
}
  1. Hello is printed once.
  2. Hello infinite number of times
  3. Hello infinite number of times
  4. Hello infinite number of times

Answer:- (B).
Explanations :In the above code, we are calling main() from main(), which is recursion. However, we have not defined any condition for the program to exit. Hence, “hello” will be printed infinite number of times. To prevent this, we need to define a proper exit condition in the recursive function.
Q9. What will be the output of the following C code if the input given to the code shown below is “sanfoundry”?
#include
#define NL '\n'
main()
{
    void f(void);
    printf("enter the word\n");
    f();
}
void f(void)
{
    char c;
    if((c=getchar())!=NL)
{
f();
    printf("%c",c);
}
return;
}
  1. sanfoundry
  2. infinite loop
  3. yrdnuofnasy
  4. fnasyrdnuo

Answer:- (C).
Explanations :The above code prints the reverse of the word entered. The recursive function terminates when getchar() is equal to null.
Q10. Iteration requires more system memory than recursion.
  1. True
  2. False

Answer:- (B).
Explanations : Recursion requires more system memory than iteration due to the maintenance of stack.