GATE CSE C Programming Practice Test – Recursion | Set 1

Questions 86-90 of 100

Your score 0/100 0 answered
Correct 0 Incorrect 0 Unanswered 100 Accuracy 0%

Question 86

NAT · Numerical Answer 2 mark

What is the output of the following C program?

C
#include <stdio.h>
int fun(int n) {
    if (n <= 1) return 1;
    return fun(n - 1) + fun(n - 2);
}
int main(void) {
    printf("%d", fun(6));
    return 0;
}
Answers for question 86

Question 87

NAT · Numerical Answer 1 mark

How many total function calls (including the initial call) are made to compute factorial(5)?

JAVASCRIPT
int factorial(int n) {
    if (n <= 0) return 1;
    return n * factorial(n - 1);
}
Answers for question 87

Question 88

NAT · Numerical Answer 2 mark

What is the output value printed by the following program?

C
#include <stdio.h>
int f(int n) {
    static int r = 0;
    if (n <= 0) return 0;
    if (n > 3) {
        r = n;
        return f(n - 2) + 2;
    }
    return f(n - 1) + r;
}
int main(void) {
    printf("%d", f(5));
    return 0;
}
Answers for question 88

Question 89

NAT · Numerical Answer 2 mark

What is the value returned by mystery(2, 3)?

CODE
int mystery(int m, int n) {
    if (m == 0) return n + 1;
    if (n == 0) return mystery(m - 1, 1);
    return mystery(m - 1, mystery(m, n - 1));
}
Answers for question 89

Question 90

NAT · Numerical Answer 1 mark

What is the value printed by the following C code?

C
#include <stdio.h>
int fun(int n) {
    if (n == 0) return 0;
    return (n % 10) + fun(n / 10);
}
int main(void) {
    printf("%d", fun(4827));
    return 0;
}
Answers for question 90
Scroll to Top