chapter

    Recursion and Recursive Program Analysis Practice Questions for Gate Cs

    Solve 165+ Recursion and Recursive Program Analysis practice questions for GATE CS with answers and detailed solutions. Free sample questions below.

    Try a question

    Answer it here to see how it works. Nothing is recorded until you sign in.

    Question 1
    Level 1: Warm-up

    Assertion (A): A recursive function that calls itself before printing the current character will output the string in reverse order.

    Reason (R): The print statement is deferred until the call stack unwinds, causing the last character processed to be printed first.

    Question 2
    Level 1: Warm-up

    Consider an array of size consisting of elements that are either 0 or 1. The array is processed by a recursive function that counts the number of contiguous blocks of identical elements. What is the maximum possible number of contiguous blocks this function can return for an array of size 8?

    Question 3
    Level 1: Warm-up

    Consider a recursive function that makes a recursive call and then prints the current element. Which of the following statements is true regarding the execution of the print statements?

    Question 4
    Level 1: Warm-up

    In the subtraction-based Euclidean algorithm for finding the Greatest Common Divisor (GCD) of two positive integers and , which of the following state transitions is impossible if the algorithm strictly maintains its mathematical invariant?

    Question 5
    Level 1: Warm-up

    An unknown recursive function f(x, y) is defined as:

    ```c

    int f(int x, int y) {

    if (y == 0) return x;

    return f(x + 1, y - 1);

    }

    ```

    What is the value returned by f(10, 3)?

    Question 6
    Level 1: Warm-up

    Let S be a pointer to the first element of an array of int in C, where the size of int is 4 bytes. If the memory address stored in S is 2048, what is the memory address (in bytes) obtained by evaluating S + 3?

    Question 7
    Level 1: Warm-up

    A recursive function counts the number of contiguous blocks of identical elements in an array of size . If the array is guaranteed to contain at least one occurrence of every integer from 1 to , what is the minimum possible number of blocks the function can return?

    Question 8
    Level 1: Warm-up

    A recursive array traversal function reduces the problem size by 1 in each step and accesses S[0] and S[1]. To safely prevent out-of-bounds access for any initial array size , how many distinct base cases are minimally required?

    Question 9
    Level 1: Warm-up

    In the subtraction-based Euclidean algorithm, what is the maximum number of recursive calls initiated (excluding the final base case evaluation) to compute ?

    Question 10
    Level 1: Warm-up

    Which of the following statements is strictly true regarding the evaluation bound of the nested recursive expression ?

    Free preview ends here

    Login to view the complete practice questions and solutions

    Creating an account is free. You get the rest of this chapter, step-by-step solutions, and a study plan built around the topics you are actually weak at.

    Explore complete test series, official past papers, and chapter-wise study tools.

    Why MastersUp

    Personalised first. High quality throughout.

    Most platforms hand everyone the same content. Here the content moves with your performance, topic by topic.

    Built around you, not around a syllabus PDF

    Every answer you give moves your topic-level intelligence rate. The next question, the next revision card and tomorrow's plan all change with it.

    Revision that hits your weak spots

    We only revise topics you have actually attempted and are still below the safe bar on — never the same chapter on repeat.

    Questions calibrated to the real exam

    Each question carries a measured toughness. You are served a rung above your current level, so practice keeps stretching you.

    Notes written for recall, not for volume

    Full lesson cards for first study, curated short-note cards for the last mile — with derivations, traps and exam patterns marked.

    One place for everything

    Notes, chapter practice, previous-year questions, test series and full-length papers — all feeding one picture of your preparation.

    Honest progress

    No vanity streaks. Progress here means chapters mastered and accuracy that held up on harder questions.

    Unlock the whole course

    Full notes and short notes, the complete question bank with worked solutions, mock tests, full-length papers, and an adaptive plan that rebuilds itself as you improve.

    Recursion and Recursive Program Analysis Practice Questions for GATE CS

    Solve 165+ Recursion and Recursive Program Analysis practice questions for GATE CS with answers and detailed solutions. Free sample questions below.

    Boundary and Scope of Recursive Traversal

    Boundary and Scope of Recursive Traversal

    This specification defines the boundary of recursive traversal. We restrict our focus to linear structures. You will analyze how a function reduces an array or transforms a string by delegating the remainder of the structure to a subsequent call.

    The anchor for our analysis is an array of eight integers containing four contiguous blocks of identical values: [4, 4, 1, 1, 1, 7, 2, 2]. We will use this structure to isolate the mechanics of state accumulation and deferred execution.

    Explain this more simply

    Think of a conveyor belt moving boxes past a scanner. The scanner processes the current box, then signals the next section of the belt to move the remaining boxes. Recursive traversal operates identically: the function processes the current element, then delegates the rest of the array to the next recursive call.

    Go one level deeper

    Linear recursion is strictly bounded by the size of the input. Unlike tree or graph recursion, there is only one recursive call per invocation. This guarantees that the recursion depth is exactly N, making the call stack predictable and the execution path a single, unbranching line of descent followed by a single line of ascent.

    Pointer Arithmetic in Recursive Steps

    When passing an array to a recursive call, the standard mechanism is pointer arithmetic. If S is a pointer to the first element of an integer array, the expression S+1 does not add one to the value of the first element. It advances the memory address by the size of one integer.

    Consequently, the recursive call f(S+1, size-1) shifts the view of the array forward by one element while reducing the tracked size. This is the recursive equivalent of incrementing an index in an iterative loop.

    S[0]
    S[1]
    S[2]
    S[3]

    S+1 moves the pointer to the second box. The data inside the boxes does not change.

    Explain this more simply

    Imagine a row of mailboxes. S points to mailbox 0. S+1 does not change the mail inside mailbox 0; it simply moves your physical position to point at mailbox 1. The recursive function is just a person walking down the row, looking at one mailbox at a time.

    Go one level deeper

    Misinterpreting this shift as value addition is a primary source of error. In C, S+1 relies on the base type of the pointer. If S is a char pointer, S+1 advances by one byte. If S is an int pointer, it advances by four bytes. The compiler handles the scaling, but the programmer must understand that the array view shifts, not the underlying data.

    Recursion and Recursive Program Analysis: Solved Questions with Step-by-Step Explanations (10 Problems)

    Question 1 · Programming and Data Structures MCQ

    Assertion (A): A recursive function that calls itself before printing the current character will output the string in reverse order.

    Reason (R): The print statement is deferred until the call stack unwinds, causing the last character processed to be printed first.

    1. A.

      Both A and R are true and R is the correct explanation of A.

    2. B.

      Both A and R are true but R is not the correct explanation of A.

    3. C.

      A is true but R is false.

    4. D.

      A is false but R is true.

    Correct Answer:

    A

    Step-by-Step Solution

    Key idea: This is an assertion-reason question about string reversal using recursion.

    Step 1: Evaluate Assertion (A). Calling the function before printing defers the print to the ascent phase, reversing the string. (A is true).

    Step 2: Evaluate Reason (R). The print is indeed deferred until the stack unwinds, meaning the deepest call (last character) prints first. (R is true).

    Step 3: Check if R explains A. Yes, the deferred execution on the unwinding stack is exactly why the string is reversed.

    Answer: Both A and R are true and R is the correct explanation of A.

    Question 2 · Programming and Data Structures MCQ

    Consider an array of size consisting of elements that are either 0 or 1. The array is processed by a recursive function that counts the number of contiguous blocks of identical elements. What is the maximum possible number of contiguous blocks this function can return for an array of size 8?

    1. A.

      4

    2. B.

      7

    3. C.

      8

    4. D.

      9

    Correct Answer:

    C

    Step-by-Step Solution

    Key idea: This is an observation question about the maximum number of blocks in an array.

    Step 1: A contiguous block is a sequence of identical adjacent elements.

    Step 2: To maximize the number of blocks, every element must be different from its neighbors.

    Step 3: For an array of size 8, the alternating sequence 0, 1, 0, 1, 0, 1, 0, 1 creates 8 separate blocks.

    Step 4: Thus, the maximum number of blocks is 8.

    Answer: 8

    Question 3 · Programming and Data Structures MCQ

    Consider a recursive function that makes a recursive call and then prints the current element. Which of the following statements is true regarding the execution of the print statements?

    1. A.

      They execute during the descent phase.

    2. B.

      They execute in the reverse order of the recursive calls.

    3. C.

      They execute in the exact same order as the recursive calls.

    4. D.

      They execute before the base case is reached.

    Correct Answer:

    B

    Step-by-Step Solution

    Key idea: This is a statement truth question about deferred actions in recursion.

    Step 1: The print statement is placed after the recursive call.

    Step 2: This means the print action is deferred until the recursive call returns.

    Step 3: The descent phase reaches the base case without printing anything.

    Step 4: As the call stack unwinds (ascent phase), the deferred print statements execute in reverse order of the calls.

    Answer: They execute in the reverse order of the recursive calls.

    Question 4 · Programming and Data Structures MCQ

    In the subtraction-based Euclidean algorithm for finding the Greatest Common Divisor (GCD) of two positive integers and , which of the following state transitions is impossible if the algorithm strictly maintains its mathematical invariant?

    1. A.

      when

    2. B.

      when

    3. C.

      when

    4. D.

      when

    Correct Answer:

    C

    Step-by-Step Solution

    Key idea: This is a GCD invariant question, recognizable because it asks about state transitions in the subtraction-based Euclidean algorithm.

    Step 1: The invariant of the Euclidean algorithm is that the set of common divisors remains unchanged when the larger number is replaced by its difference with the smaller number.

    Step 2: Therefore, valid transitions are when , or when .

    Step 3: Adding the numbers () changes the set of common divisors and violates the invariant.

    Answer: when is impossible.

    Question 5 · Programming and Data Structures MCQ

    An unknown recursive function f(x, y) is defined as:

    ```c

    int f(int x, int y) {

    if (y == 0) return x;

    return f(x + 1, y - 1);

    }

    ```

    What is the value returned by f(10, 3)?

    1. A.

      10

    2. B.

      12

    3. C.

      13

    4. D.

      30

    Correct Answer:

    C

    Step-by-Step Solution

    Key idea: This is a reverse engineering question, recognizable because it provides an unknown recursive function and asks for its output on specific inputs.

    Step 1: Trace the function f(x, y) with initial values .

    Step 2: Call 1: f(10, 3). Since , it returns f(10 + 1, 3 - 1) = f(11, 2).

    Step 3: Call 2: f(11, 2). Returns f(12, 1).

    Step 4: Call 3: f(12, 1). Returns f(13, 0).

    Step 5: Call 4: f(13, 0). Base case is met, so it returns , which is 13.

    Answer: 13

    Question 6 · Programming and Data Structures NAT

    Let S be a pointer to the first element of an array of int in C, where the size of int is 4 bytes. If the memory address stored in S is 2048, what is the memory address (in bytes) obtained by evaluating S + 3?

    Correct Answer:

    2060.00

    Step-by-Step Solution

    Key idea: This is a pointer arithmetic question. Adding an integer to a pointer scales by the size of the base type.

    Step 1: The base type is int, which occupies 4 bytes.

    Step 2: The expression S + 3 advances the pointer by 3 elements, not 3 bytes.

    Step 3: The byte offset is bytes.

    Step 4: The new address is .

    Answer: 2060.00

    Question 7 · Programming and Data Structures MCQ

    A recursive function counts the number of contiguous blocks of identical elements in an array of size . If the array is guaranteed to contain at least one occurrence of every integer from 1 to , what is the minimum possible number of blocks the function can return?

    1. A.

    2. B.

    3. C.

    4. D.

    Correct Answer:

    A

    Step-by-Step Solution

    Key idea: This is a contradiction/bounding question about the minimum number of blocks given distinct elements.

    Step 1: The array contains all integers from 1 to at least once.

    Step 2: Each distinct integer must form at least one contiguous block.

    Step 3: To minimize the total number of blocks, all occurrences of the same integer should be grouped together.

    Step 4: If grouped perfectly, there is exactly 1 block per distinct integer, totaling blocks.

    Answer:

    Question 8 · Programming and Data Structures MCQ

    A recursive array traversal function reduces the problem size by 1 in each step and accesses S[0] and S[1]. To safely prevent out-of-bounds access for any initial array size , how many distinct base cases are minimally required?

    1. A.

      1

    2. B.

      2

    3. C.

      3

    4. D.

      4

    Correct Answer:

    B

    Step-by-Step Solution

    Key idea: This is a boundary condition question, recognizable because it asks for the minimal base cases to prevent out-of-bounds access.

    Step 1: The function accesses S[0] and S[1]. If the size n is 1, accessing S[1] is out of bounds. Thus, n == 1 must be a base case.

    Step 2: If the initial array size can be 0, accessing S[0] is also out of bounds. Thus, n == 0 must also be a base case.

    Answer: 2 distinct base cases are minimally required.

    Question 9 · Programming and Data Structures MCQ

    In the subtraction-based Euclidean algorithm, what is the maximum number of recursive calls initiated (excluding the final base case evaluation) to compute ?

    1. A.

      2

    2. B.

      4

    3. C.

      5

    4. D.

      10

    Correct Answer:

    B

    Step-by-Step Solution

    Key idea: This is an observation question, recognizable because it asks for the number of steps in a specific, small-scale execution of a known algorithm.

    Step 1: Trace the calls for . Since , it calls . (Call 1)

    Step 2: calls . (Call 2)

    Step 3: calls . (Call 3)

    Step 4: calls . (Call 4)

    Step 5: meets the base case and returns 2 without initiating further calls.

    Answer: 4 recursive calls are initiated.

    Question 10 · Programming and Data Structures MCQ

    Which of the following statements is strictly true regarding the evaluation bound of the nested recursive expression ?

    1. A.

      The outer function must be fully expanded before is evaluated.

    2. B.

      The inner function must resolve to a concrete integer before can proceed.

    3. C.

      Both and evaluate simultaneously to bound the execution time.

    4. D.

      The base case of is checked before is evaluated.

    Correct Answer:

    B

    Step-by-Step Solution

    Key idea: This is a bounding statement question, recognizable because it asks for the strict evaluation constraint of a nested recursive expression.

    Step 1: Identify the structure: is called with the result of as its argument.

    Step 2: Apply the rule of nested call resolution: strict inner-first evaluation.

    Step 3: Conclude that must completely finish and return a concrete integer value before the outer function can substitute that value and proceed.

    Answer: The inner function must resolve to a concrete integer before can proceed.

    More practice questions in this unit