Consider the following statements. S1: The sequence of procedure calls…

2021

Consider the following statements.

 S1: The sequence of procedure calls corresponds to a preorder traversal of the activation tree.

S2: The sequence of procedure returns corresponds to a postorder traversal of the activation tree.

Which one of the following options is correct?

Answer: C. S1 is true and S2 is trueS1: "The sequence of procedure calls corresponds to a preorder traversal."TRUE Reason: When a function/procedure is called: We first visit the procedure…

  1. A.

    S1 is true and S2 is false

  2. B.

    S1 is false and S2 is true

  3. C.

    S1 is true and S2 is true

  4. D.

    S1 is false and S2 is false

Attempted by 292 students.

Show answer & explanation

Correct answer: C

S1: "The sequence of procedure calls corresponds to a preorder traversal."

TRUE

Reason:
When a function/procedure is called:

  1. We first visit the procedure (root)

  2. Then recursively call its children

This is exactly how Preorder traversal works:

Preorder = Root → Left → Right

So, procedure calls = preorder traversal.

S2: "The sequence of procedure returns corresponds to a postorder traversal."

TRUE

Reason:
A procedure returns only after:

  1. All its child procedures have finished

  2. Then it returns to the caller

This is exactly the Postorder traversal behavior:

Postorder = Left → Right → Root

So procedure returns = postorder traversal.

A video solution is available for this question — log in and enroll to watch it.

Explore the full course: Iocl Engineers Officers Grade A Paper 2

Loading lesson…