What will be the output of the following pseudo-code when parameters are passed by reference and dynamic scoping is assumed? a=3; void n(x) {x = x * a; print (x) ; } void m(y) {a = 1; a = y - a; n (a); print (a) ; } void main() { m(a); }  

What will be the output of the following pseudo-code when parameters are passed by reference and dynamic scoping is assumed? a=3; void n(x) {x = x * a; print (x) ; } void m(y) {a = 1; a = y - a; n (a); print (a) ; } void main() { m(a); }   Correct Answer 4, 2

The correct answer is option 3.

Concept:

Static Scope:

Referencing the environment of a statement in a static scope language is the collection of all local variables and all other ancestor variables that are visible in the statement.

Dynamic Scope:

Referencing the environment of a statement in a dynamical scope language is the collection of all local variables and all other active subprogram variables which are visible in the statement.

Explanation:

Step 1:

Here 'a' is a global variable it can be accessed throughout the program.

Step 2:

In m(y) here a is not a local variable so it updates the value in the global section. Then control moves to n(x) function.

Step 3:

In n(x), the local variable x is updated by x = x*a. So print x value as 4. Then n(x) function is removed from stack moves back to m(y) function call.

Step 4:

In m(y), The value 'a' prints by checking the local variable and other active sub-programs. Hence the main function has the variable 'a'. Hence it prints the 'a' as 2 as output.

Hence the correct answer is 4,2.

Related Questions