GATE CSE C Programming Practice Test – Recursion | Set 1

Questions 21-25 of 100

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

Question 21

MCQ · Single Correct 2 mark

What is the output of the following C program?

C
#include <stdio.h>
int f(int *p, int n) {
    if (n <= 1) return 0;
    return *p + f(p + 1, n - 1);
}
int main(void) {
    int a[] = {1, 2, 3, 4, 5};
    printf("%d", f(a, 5));
    return 0;
}
Answers for question 21

Question 22

MCQ · Single Correct 2 mark

What is the output of the following C function for call mystery(14)?

C
void mystery(int n) {
    if (n > 0) {
        mystery(n / 2);
        printf("%d", n % 2);
    }
}
Answers for question 22

Question 23

MCQ · Single Correct 2 mark

Consider the following recursive C code:

CODE
int calc(int n) {
    if (n <= 1) return 1;
    if (n % 2 == 0) return calc(n / 2);
    return calc(3 * n + 1);
}
What is calc(3)?
Answers for question 23

Question 24

MCQ · Single Correct 1 mark

Which type of recursion occurs when a function calls another function, which in turn calls the first function?

Answers for question 24

Question 25

MCQ · Single Correct 1 mark

What does the following recursive function return when called with s = "GATE"?

JAVASCRIPT
int len(char *s) {
    if (*s == '\0') return 0;
    return 1 + len(s + 1);
}
Answers for question 25
Scroll to Top