GATE Computer Science Engineering(CSE) 2027 Test: Recursion- 1 Free Online


MCQ Practice Test & Solutions: Test: Recursion- 1 (10 Questions)

You can prepare effectively for Computer Science Engineering (CSE) GATE Computer Science Engineering(CSE) 2027 Mock Test Series with this dedicated MCQ Practice Test (available with solutions) on the important topic of "Test: Recursion- 1". These 10 questions have been designed by the experts with the latest curriculum of Computer Science Engineering (CSE) 2026, to help you master the concept.

Test Highlights:

  • - Format: Multiple Choice Questions (MCQ)
  • - Duration: 30 minutes
  • - Number of Questions: 10

Sign up on EduRev for free to attempt this test and track your preparation progress.

Test: Recursion- 1 - Question 1

What will be the output of the following C program?

void count (int n) {

static int d=1;

printf ("%d",n);

printf ("%d",d);

d++;

if (n>1) count (n-1);

printf ("%d",d);

}

void main(){

count (3);

}

Detailed Solution: Question 1

Option A is correct: 3 1 2 2 1 3 4 4 4

static int d = 1 is initialized once and retains its value across all calls to the function.

Call 1: n = 3, d = 1. The function prints 3 1, then increments d to 2 and calls count(2).

Call 2: n = 2, d = 2. It prints 2 2, increments d to 3 and calls count(1).

Call 3: n = 1, d = 3. It prints 1 3, increments d to 4. The if condition is false, so it does not recurse; the printf after the if prints 4 for this call.

Returning to Call 2, the printf after the recursive call prints the current d = 4. Returning to Call 1, its final printf also prints the current d = 4.

Putting all prints in chronological order yields: 3 1 2 2 1 3 4 4 4.

Test: Recursion- 1 - Question 2

A language with string manipulation facilities uses the following operations

head(s): first character of a string

tail(s): all but exclude the first character of a string

concat(s1, s2): s1s2

For the string "acbc" what will be the output of

concat(head(s), head(tail(tail(s))))

 

Detailed Solution: Question 2

concat(a,head(tail(tail(acbc))))
concat(a,head(tail(cbc)))
concat(a,head(bc))
concat(a,b)
ab.

Test: Recursion- 1 - Question 3

Consider the following C function:

int f(int n)

{

static int i = 1;

if(n >= 5) return n;

n = n+i;    

i++;

return f(n);

}

The value returned by f(1) is

Detailed Solution: Question 3

Function Flow:

  • Starts with n = 1 and i = 1 (static).
  • Adds i to n and increments i recursively until n >= 5.

Steps:
We start with n = 1 and i = 1.

            First Call: f(1)

  • Initial values: n = 1, i = 1.
  • Condition: n >= 5 → False.
  • Update n: n = n + i = 1 + 1 = 2.
  • Increment i: i = 2.
  • Recursive call: f(2).

           Second Call: f(2)

Third Call: f(4)

Fourth Call: f(7)

  • Initial values: n = 2, i = 2 (retained from the previous call).
  • Condition: n >= 5 → False.
  • Update n: n = n + i = 2 + 2 = 4.
  • Increment i: i = 3.
  • Recursive call: f(4).
  • Initial values: n = 4, i = 3 (retained from the previous call).
  • Condition: n >= 5 → False.
  • Update n: n = n + i = 4 + 3 = 7.
  • Increment i: i = 4.
  • Recursive call: f(7).
  • Initial values: n = 7, i = 4 (retained from the previous call).
  • Condition: n >= 5 → True.
  • Return value: n = 7.

    Once the base case is met (n = 7), the recursion stops. Each recursive call returns the same value (7) back up the chain:

  • The third call (f(4)) returns 7.
  • The second call (f(2)) returns 7.
  • The first call (f(1)) returns 7

    Answer:

    (c) 7

Test: Recursion- 1 - Question 4

double foo(int n)

{

int i;

double sum;

if(n == 0)

{

return 1.0;
}

else

{

sum = 0.0;

for(i = 0; i < n; i++)

{         

sum += foo(i);

}
return sum;

}
}

The space complexity of the above code is?

Detailed Solution: Question 4

A. The code here is storing only local variables. So, the space complexity will be the recursion depth- maximum happening for the last iteration of the for loop- foo(n-1) - foo(n-2) - .... - foo(0) all live giving space complexity O(n).

B. To store the n values we need space complexity O(n). So, the space complexity won't change and will be O(n).

Test: Recursion- 1 - Question 5

double foo(int n)

{

int i;

double sum;

if(n == 0)

{return 1.0;

}

else

{

sum = 0.0;

for(i = 0; i < n; i++)

{

 sum += foo(i);

}

return sum;

}
}

Suppose we modify the above function foo() and stores the value of foo(i) 0 <= i < n, as and when they are computed. With this modification the time complexity for function foo() is significantly reduced. The space complexity of the modified function would be:

Detailed Solution: Question 5

The modification being proposed is tabulation method of Dynamic Programming.
So let we have an auxillary array which stores the value of foo(i) once it is computed and can be used at later stage .So foo(i) is not called again , instead lookup from the table takes place. It would be something like :

#include <stdio.h>
#define max 1000
int foo[max];

foo(int n) {
if(foo[n] == -1) //compute it and store at foo[n] and return
else // return foo[n] directly
}

int main(void) {
//initialize all elements of foo[max] to -1
return 0;
}Using this approach , we need an auxillary array of size n to store foo(i) where i ranges from 0 to n-1.

So space complexity of this method = O(n) And function foo(n) computes 2n - 1    

Test: Recursion- 1 - Question 6

int f(int n)

{

static int r = 0;

if (n <= 0) return 1;

if (n > 3)

{

r = n;

return f(n-2) + 2;

}

return f(n-1) + r;

}

What is the value of f(5)?

Detailed Solution: Question 6

Option D is correct; f(5) = 18.

Call f(5). Since n > 3, the code sets r = 5 and returns f(3) + 2.

Compute f(3). Here n = 3 is not > 3, so the function returns f(2) + r. The static variable r retains the value 5.

f(0) = 1 by the base case.

f(1) = f(0) + r = 1 + 5 = 6.

f(2) = f(1) + r = 6 + 5 = 11.

f(3) = f(2) + r = 11 + 5 = 16.

Therefore f(5) = f(3) + 2 = 16 + 2 = 18.

Test: Recursion- 1 - Question 7

The function f is defined as follows:

int f (int n) {

if (n <= 1) return 1;

else if (n % 2 == 0) return f(n/2);

else return f(3n - 1);
}

Assuming that arbitrarily large integers can be passed as a parameter to the function, consider the following statements.

i. The function f terminates for finitely many different values of n ≥ 1.

ii. The function f terminates for infinitely many different values of n ≥ 1.

iii. The function f does not terminate for finitely many different values of n ≥ 1.

iv. The function f does not terminate for infinitely many different values of n ≥ 1.

Which one of the following options is true of the above?

A. i and iii

B. i and iv

C. ii and iii

D. ii and iv

Detailed Solution: Question 7

The function terminates for all powers of 2 (which is infinite), hence (i) is false and (ii) is TRUE.
Let n = 5.
Now, recursive calls will go like 5 - 14 - 7 - 20 - 10 - 5 -
And this goes into infinite recursion. And if we multiply 5 with any power of 2, also result will be infinite recursion. Since, there are infinite powers of 2 possible, there are infinite recursions possible (even considering this case only). So, (iv) is TRUE and (iii) is false.
So, correct answer is (D)

Test: Recursion- 1 - Question 8

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

Detailed Solution: Question 8

  • Unroll recursion up to a point where we can distinguish the given options and choose the correct one!
  • Options B and D are eliminated.
  • A is the answer.

Test: Recursion- 1 - Question 9

Consider the following computation rules. Parallel-outermost rule: Replace all the outermost occurrences of F (i.e., all occurrences of F which do not occur as arguments of other F's) simultaneously. Parallel - innermost rule: Replace all the innermost occurrences of F (i.e.,all occurrences of F with all arguments free of F's) simultaneously. Now consider the evaluations of the recursive program over the integers.  

F(x, y) <== if x = 0 then 0 else

[ F(x + 1, F(x, y)) * F(x - 1, F(x, y))]

 where the multiplication functions * is extended as follows:  

0 * w & w * 0 are 0

a * w & w * a are w (for any non-zero integer a)

w * w is w  

We say that F(x, y) = w when the evaluation of F(x, y) does not terminate. Computing F(1, 0) using the parallel - innermost and parallel - outermost rule yields

Detailed Solution: Question 9

Answer is A) w and 0

If we solve using parallel innermost rule
F(1,0) = F(2,F(1,0)) * F(0,F(1,0))

=F(2, F(2,F(1,0)) * F(0,F(1,0)) ) * F(0, F(2,F(1,0)) * F(0,F(1,0)) )

Since computation of F(1,0) goes on
we assign F(1,0) to w
So F(1,0)= w

Using parallel outermost rule
F(1,0)= F(2,F(1,0)) * F(0,F(1,0))
= F(2,F(1,0)) * 0
= 0

Test: Recursion- 1 - Question 10

Consider the class of recursive and iterative programs. Which of the following statements is false?

Detailed Solution: Question 10

Recursive and iterative programs are equivalent in computational power; neither is more powerful than the other. Hence, Option A is false. Option B is true because any iterative program can be converted into a recursive one. Option C is true since recursion uses the call stack, which involves dynamic memory allocation. Option D is also true because improper recursive definitions can lead to non-termination (infinite recursion).

56 docs|215 tests
Information about Test: Recursion- 1 Page
In this test you can find the Exam questions for Test: Recursion- 1 solved & explained in the simplest way possible. Besides giving Questions and answers for Test: Recursion- 1, EduRev gives you an ample number of Online tests for practice
Download as PDF