recategorized by
1,784 views
1 votes
1 votes

Consider the program below:

Program main:
    var r:integer;
    procedure two:
        begin write (r); end
    procedure one:
        var r:integer;
        begin r:=5; two; end
    begin r:=2;
        two; one; two;
    end

What is printed by the above program if

  1. Static scoping is assumed for all variables;
  2. Dynamic scoping is assumed for all variables.

Give reasons for your answer.

recategorized by

1 Answer

Best answer
7 votes
7 votes

In static scoping all variables are assigned scope before the program execution. If a variable declaration is missing in local scope it is checked in global scope. So, here the output will be 

2 2 2 

as the $r$ is procedure 2 is always the global $r$.

In dynamic scoping, non local variables are assigned scope during program execution. If a variable declaration is missing in local scope, it is checked in the function which called the current one. So, the output will be

2 5 2

as when procedure $one$ calls procedure $two$, $r$ will be from procedure $one$ and it has value $5$.

edited by

Related questions

3 votes
3 votes
0 answers
1
Kathleen asked Oct 5, 2014
539 views
Every element $a$ of some ring $(R, +, o)$ satisfies the equation $a\;o\;a=a$. Decide whether or not the ring is commutative.
0 votes
0 votes
3 answers
2
Kathleen asked Oct 4, 2014
11,582 views
Match the following items(i) Newton-Raphson(a) Integration(ii) Runge-Kutta(b) Root finding(iii) Gauss-Seidel(c) Ordinary Differential Equations(iv) Simpson's Rule(d) Solu...
0 votes
0 votes
0 answers
3
3 votes
3 votes
1 answer
4
Kathleen asked Oct 4, 2014
1,630 views