Recursion questions — page 1 of 2

Q61

GATE CS 2026 CS-1RecursionNAT2 marks
Consider the recursive functions represented by the following code segment:
int bar(int n){
 if (n == 1) return 0;
 else return 1 + bar(n/2);
}

int foo(int n){
 if (n == 1) return 1;
 else return 1 + foo(bar(n));
}
The smallest positive integer n for which foo(n) returns 5 is ______. (answer in integer)

Note: Ignore syntax errors (if any) in the function.
Show answer without attempting

Answer: 65536

Q61

GATE CS 2026 CS-2RecursionNAT2 marks
Consider the following ANSI-C function.
int func(int start, int end){
 int length=end+1-start;
 if((length<1)||(start<0)||(end<0)){ return(0); }
 if(length%3==0){
 return(func(start+1, end));
 } else if(length%3==1){
 return(1+func(start, end-1));
 } else {
 return(func(start+2, end));
 }
}
The maximum possible value that can be returned from this function is ____________. (answer in integer)

Note: Ignore syntax errors (if any) in the function.
Show answer without attempting

Answer: 1

Q61

GATE CS 2025 CS-1RecursionNAT2 marks
#include <stdio.h>
int foo(int S[],int size){
 if(size == 0) return 0;
 if(size == 1) return 1;
 if(S[0] != S[1]) return 1+foo(S+1,size-1);
 return foo(S+1,size-1);
}
int main(){
 int A[]={0,1,2,2,2,0,0,1,1};
 printf("%d",foo(A,9));
 return 0;
}
The value printed by the given C program is _______ . (Answer in integer)
Show answer without attempting

Answer: 5

Q63

GATE CS 2025 CS-2RecursionNAT2 marks
Consider the following C program:
#include <stdio.h>
int g(int n) {
    return (n+10);
}
int f(int n) {
    return g(n*2);
}
int main() {
    int sum, n;
    sum=0;
    for (n=1; n<3; n++)
        sum += g(f(n));
    printf ("%d", sum);
    return 0;
}
The output of the given C program is ________. (Answer in integer)
Show answer without attempting

Answer: 46

Q19

GATE CS 2024 CS-1RecursionMCQ1 mark−1/3 if wrong
Consider the following C program:
#include <stdio.h>
void fX();
int main(){
fX();
return 0;
}
------------------------
void fX(){
char a;
if((a=getchar()) != '\n')
fX();
if(a != '\n')
putchar(a);
}
Assume that the input to the program from the command line is 1234 followed by a newline character. Which one of the following statements is CORRECT?
Show answer without attempting

Answer: C

Q36

GATE CS 2023 CSRecursionMCQ2 marks−2/3 if wrong
Consider the following program:
int main()
{
f1();
f2(2);
f3();
return(0);
}
int f1()
{
return(1);
}
int f2(int X)
{
f3();
if (X==1)
return f1();
else
return (X*f2(X-1));
}
int f3()
{
return(5);
}
Which one of the following options represents the activation tree corresponding to the main function?
Show answer without attempting

Answer: A

Q23

GATE CS 2021 CS-2RecursionNAT1 mark
Consider the following ANSI C function:
int SomeFunction(int x, int y)
{
    if ((x == 1) || (y == 1)) return 1;
    if (x == y) return x;
    if (x > y) return SomeFunction(x - y, y);
    if (y > x) return SomeFunction(x, y - x);
}
The value returned by SomeFunction(15, 255) is ____________.
Show answer without attempting

Answer: 15

Q49

GATE CS 2021 CS-2RecursionNAT2 marks
Consider the following ANSI C program:
#include <stdio.h>
int foo(int x, int y, int q)
{
    if ((x <= 0) && (y <= 0))
        return q;
    if (x <= 0)
        return foo(x, y-q, q);
    if (y <= 0)
        return foo(x-q, y, q);
    return foo(x, y-q, q) + foo(x-q, y, q);
}
int main()
{
    int r = foo(15, 15, 10);
    printf("%d", r);
    return 0;
}
The output of the program upon execution is ____________.
Show answer without attempting

Answer: 60

Q46

GATE CS 2020 CSRecursionNAT2 marks
Consider the following C functions.
int fun1(int n) {
    static int i = 0;
    if (n > 0) {
        ++i;
        fun1(n-1);
    }
    return(i);
}

int fun2(int n) {
    static int i = 0;
    if (n > 0) {
        i = i + fun1(n);
        fun2(n-1);
    }
    return(i);
}
The return value of fun2(5) is __________.
Show answer without attempting

Answer: 55

Q26

GATE CS 2019 CSRecursionMCQ2 marks−2/3 if wrong
Consider the following C function:
void convert(int n){
    if(n<0)
        printf("%d",n);
    else {
        convert(n/2);
        printf("%d",n%2);
    }
}
Which one of the following will happen when the function convert is called with any positive integer n as argument?
Show answer without attempting

Answer: D

Q21

GATE CS 2018 CSRecursionNAT1 mark
Consider the following C program:
#include <stdio.h>
int counter = 0;
int calc (int a, int b) {
 int c;
 counter++;
 if (b==3) return (a*a*a);
 else {
 c = calc(a, b/3);
 return (c*c*c);
 }
}
int main (){
 calc(4, 81);
 printf ("%d", counter);
}
The output of this program is _____.
Show answer without attempting

Answer: 4

Q32

GATE CS 2018 CSRecursionMCQ2 marks−2/3 if wrong
Consider the following C code. Assume that unsigned long int type length is 64 bits.
unsigned long int fun(unsigned long int n){
 unsigned long int i, j = 0, sum = 0;
 for (i = n; i > 1; i = i/2) j++;
 for ( ; j > 1; j = j/2) sum++;
 return(sum);
}
The value returned when we call fun with the input 2402^{40} is
Show answer without attempting

Answer: B

Q45

GATE CS 2018 CSRecursionNAT2 marks
Consider the following program written in pseudo-code. Assume that x and y are integers.
Count(x,y) {
 if (y != 1){
 if (x != 1) {
 print("*");
 Count(x/2, y);
 }
 else {
 y = y-1;
 Count(1024, y);
 }
 }
}
The number of times that the print statement is executed by the call Count(1024,1024) is _____.
Show answer without attempting

Answer: 10230

Q35

GATE CS 2017 CS-1RecursionMCQ2 marks−2/3 if wrong
Consider the following two functions.
void fun1(int n) {
    if (n == 0) return;
    printf("%d", n);
    fun2(n - 2);
    printf("%d", n);
}

void fun2(int n) {
    if (n == 0) return;
    printf("%d", n);
    fun1(++n);
    printf("%d", n);
}
The output printed when fun1(5) is called is
Show answer without attempting

Answer: A

Q36

GATE CS 2017 CS-1RecursionMCQ2 marks−2/3 if wrong
Consider the C functions foo and bar given below:
int foo(int val) {
    int x = 0;
    while(val > 0) {
        x = x + foo(val--);
    }
    return val;
}

int bar(int val) {
    int x = 0;
    while(val > 0) {
        x = x + bar(val-1);
    }
    return val;
}
Invocations of foo(3) and bar(3) will result in:
Show answer without attempting

Answer: C

Q37

GATE CS 2016 CS-2RecursionNAT2 marks
Consider the following program:
int f(int *p, int n)
{
    if (n <= 1) return 0;
    else return max(f(p+1,n-1),p[0]-p[1]);
}

int main()
{
    int a[] = {3,5,2,6,4};
    printf("%d", f(a,5));
}
Note: max(x,y) returns the maximum of x and y.

The value printed by this program is _________.
Show answer without attempting

Answer: 3.0

Q39

GATE CS 2016 CS-2RecursionNAT2 marks
The given diagram shows the flowchart for a recursive function A(n)A(n). Assume that all statements, except for the recursive calls, have O(1)O(1) time complexity. If the worst case time complexity of this function is O(nα)O(n^\alpha), then the least possible value (accurate up to two decimal positions) of α\alpha is _________.
Question figure
Show answer without attempting

Answer: 2.2 to 2.4

Q38

GATE CS 2015 CS-3RecursionMCQ2 marks−2/3 if wrong
Consider the following recursive C function.
void get(int n)
{
    if (n<1) return;
    get(n-1);
    get(n-3);
    printf("%d", n);
}
If get(6) function is being called in main() then how many times will the get() function be invoked before returning to the main()?
Show answer without attempting

Answer: B

Q24

GATE CS 2015 CS-2RecursionNAT1 mark
Consider the following C function.
int fun(int n){
    int x=1, k;
    if (n==1) return x;
    for (k=1; k<n; ++k)
        x = x + fun(k) * fun(n-k);
    return x;
}
The return value of fun(5) is _________.
Show answer without attempting

Answer: 51

Q40

GATE CS 2014 CS-2RecursionNAT2 marks
Consider the following function
double f(double x){
   if( abs(x*x - 3) < 0.01) return x;
   else return f(x/2 + 1.5/x);
}
Give a value q (to 2 decimals) such that f(q) will return q:_____.
Show answer without attempting

Answer: 1.72 to 1.74

0/200 correct0 marks

Related topics in Programming and Data Structures