Consider the following program: Which one of the following options represents…

2023

Consider the following program:

Which one of the following options represents the activation tree corresponding to the main function?

Attempted by 122 students.

Show answer & explanation

Key idea: build the activation tree by following the runtime call sequence and showing each call as a child of the activation that invoked it.

  1. main calls f1(). f1 immediately returns and is a leaf in the activation tree.

  2. main calls f2(2). During f2(2):

    1. f2(2) first calls f3(). That f3 is a leaf under the f2(2) activation.

    2. Then f2(2) makes the recursive call f2(1).

      1. Inside f2(1), f3() is called (leaf) and then f1() is called (leaf).

  3. After f2 returns to main, main calls f3() once more; that f3 is another leaf child of main.

Activation tree (structure):

  • main

    • f1 (leaf)

    • f2

      • f3 (leaf)

      • f2 (recursive call for X=1)

        • f3 (leaf)

        • f1 (leaf)

    • f3 (leaf)

Therefore, the correct activation tree is the image that shows main with three children labeled f1, f2, f3; the f2 child has a child f3 and a recursive f2 child, and that recursive f2 has children f3 and f1.

Explore the full course: Gate Guidance By Sanchit Sir