program L;var n : char;procedure W;beginwriteln(n);end;procedure D;var n : char;beginn := ‘D’;W;end;beginn := ‘L’;W;D;end.What is the output of the program under:(a) Static scope(b) Dynamic scope.
Question
program L;var n : char;procedure W;beginwriteln(n);end;procedure D;var n : char;beginn := ‘D’;W;end;beginn := ‘L’;W;D;end.What is the output of the program under:(a) Static scope(b) Dynamic scope.
Solution
(a) Static Scope: The output will be: L L
Explanation: In static (or lexical) scoping, a variable's definition is resolved by searching its containing block or function, then if that fails, searching the outer containing block, and so on. So, the procedure W will always print the global variable n, which is 'L'. Even though there is a local variable n in procedure D, it doesn't affect the global variable n.
(b) Dynamic Scope: The output will be: L D
Explanation: In dynamic scoping, a variable's definition is resolved by searching the most recent active function call, then if that fails, searching the next most recent active function call, and so on. So, the procedure W will print the most recent active variable n. In the first call to W, the most recent active n is the global n, which is 'L'. In the second call to W (which is inside procedure D), the most recent active n is the local n in procedure D, which is 'D'.
Similar Questions
1. What will be the output of the following pseudo-code program? (lex declares a lexically-scoped variable,whereas dyn declares a dynamically-scopedvariable.)//define b as a dynamically-scoped var.dyn b = 41;//define a as a dynamically-scoped var.dyn a = 26;//define c as a lexically-scoped var.lex c = 15;//define function f3f3() {dyn b = 64; //redeclare bdyn a = 47; //redeclare alex c = 2; //redeclare cf1();}//define function f2f2() {print c, a, b;}//define function f1f1() {dyn b = 94; //redeclare bdyn a = 62; //redeclare af2();}f3(); //initial callRemember to justify your answer. 25-pointsSolution:The output for the above program will be 15 47 94
Which of the following can change during the execution of a program:Question 4Answera.a variable's valueb.a variable's typec.a variable's scoped.a variable's visibilitye.all of the above can change
The portion of a program within which you can reference a variable is the variable's _____. a. scope b. space c. domain d. range
What is the output after the following code is compiled and its executable run?#include <iostream>using namespace std;int main(){ int a, b, c; a = 5; b = 6; c = (a>b) ? a : b; cout << c << "\n"; return 0;}Select one:a.1b.5c.6d.11
What is the output after this code is compiled and its executable run?#include <iostream>using namespace std;void pn( int x, int& p, int& n ){ p = x-1; n = x+1;}int main(){ int x = 10, y, z; pn( x, y, z ); cout << y << ", " << z << "\n"; return 0;}Select one:a.9, 10b.9, 11c.10, 11d.11, 9
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.