Question

4. (35 points Consider the following pseudocode which uses dynamic scoping. What does the program print if the language uses shallow binding? What does it print with deep binding? x: integer global procedure print x write integer (x) procedure first procedure second (F procedure) x: integer x 5 F print x second (first) print x

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Deep binding binds the environment at the time a procedure is passed as an argument.

Shallow binding binds the environment at the time a procedure is actually called.

When deep binding;

Sequence of execution is

  • x=7
  • Second(first)
    • x=5
    • First()
      • x=x*3
      • x=7*3 …(x is taken as global in deep binding)
      • x=21 ---(now globa x become 21)
    • Print_x()
      • x=5 is passed to print_x so it will print 5 <-----ANS
  • Print_x()
    • x=21 is been passed to function so 21 will be printed <---ANS

So 5,21 will be printed

When shallow binding

Sequence of execution is

  • x=7
  • Second(first)
    • x=5
    • First()
      • x=x*3
      • x=5*3 … (travel in func call stack until value of x found)
      • x=15
    • Print_x()
      • x=15 will be printed ….<----ANS
  • Print_x()
    • x=7 will be printed<---ANS ..as x=7 is last value of x in function call stack

So 15,7 will be printed

Add a comment
Know the answer?
Add Answer to:
Consider the following pseudocode which uses dynamic scoping. What does the program print if the language...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Consider the following pseudocode Xinteger procedure set x(n global integer) Xin procedure print x write integer(x)...

    Consider the following pseudocode Xinteger procedure set x(n global integer) Xin procedure print x write integer(x) procedure first set x(1) print x procedure second xinteger set x(2) print x set x(e) first() print x second print x What does this program print if the language uses static scoping? What does it print with dynamic scoping? Why?

  • Consider the following code: int a:=10 //global int b:=12 //global proc F a:= a-b proc P...

    Consider the following code: int a:=10 //global int b:=12 //global proc F a:= a-b proc P (M:proc) int a:=2 M() proc K int b:=3 P(F) K() //main program print(a) //built in function a- what does this code print if it uses dynamic scoping and deep binding? b- what does this code print if it uses dynamic scoping and shallow binding?

  • x: integer -- global procedure second x: integer --local x:= 2 second --main program x:= 0...

    x: integer -- global procedure second x: integer --local x:= 2 second --main program x:= 0 first write_integer (x) //What does this program print if the language uses static scoping? //What does it print if the language uses dynamic scoping?

  • Question 7 (1 point) Suppose the program below outputs 10 30. The language uses int vari...

    Question 7 (1 point) Suppose the program below outputs 10 30. The language uses int vari int var2 = 10 40 def bar() : print varl var2 def foo (f): int var2 = 30 f() def qux(): int varl = 20 foo (bar) # execution starts here qux () # execution starts here qux ( ) dynamic scoping with shallow binding lexical scoping dynamic scoping with deep binding None of these options

  • Which of the following are the two ways dynamic scoping can be implemented? (May contain more...

    Which of the following are the two ways dynamic scoping can be implemented? (May contain more than one possible answer) A.)   Shallow access B.)   Shallow binding C.)   Deep access D.)   Deep binding

  • Consider the following program written in C syntax int a , b , c ; //...

    Consider the following program written in C syntax int a , b , c ; // first declaration void g() { print(a,b,c); } int f(int a) // parameter declaration { int b; // second declaration b = a + 1; g(); // first call { int a; // third declaration int c; // fourth declaration c = b; a = b + c; g(); // second call } g(); // third call return a + b ; } int main()...

  • For the given program determine what is printed after each write statement assuming static scoping, deep...

    For the given program determine what is printed after each write statement assuming static scoping, deep binding and parameters are passed-by-value-result. program main;    var X: integer;    procedure Q(var I: integer; function R(J: integer):integer);             var X: integer;             begin              X:=4;              write(“In Q, before call of R, I=”, I, “X=”, X);              I=R(I);              write(“In Q, after call of R, I=”, I, “X=”, X);           end;        procedure P;            var I: integer;           ...

  • 1. Which of the following is a problem with dynamic scoping? Readability Access to nonlocal variables...

    1. Which of the following is a problem with dynamic scoping? Readability Access to nonlocal variables takes longer Static type checking may not be possible 2.Languages with dynamic type binding are usually implemented using: Compilers Interpreters Hybrid implementations JIT 3.Which variables are created using operator new? Static Stack-dynamic Explicit heap-dynamic Implicit heap-dynamic 4.Which variables are bound to memory cells before program execution and remain bound to the same memory cell during program execution? Static Stack-dynamic Explicit heap-dynamic Implicit heap-dynamic

  • Will rate, thank you. Consider the following pseudocode, assuming nested subroutines, lexical scope, and that local...

    Will rate, thank you. Consider the following pseudocode, assuming nested subroutines, lexical scope, and that local variables (including formal parameters) are stored in the stack. procedure main() g: integer procedure B(a: integer) x: integer procedure A(n: integer) g := n procedure R(m: integer) write_integer (x) x := x/2 -- integer division if x > 1 R(m + 1) else A (m) --body of B x := a * a R(1) --body of main B(3) write_integer(g) (a) What does this program...

  • Consider the following JavaScript skeletal program: //the main program var x: function sub1 () { var...

    Consider the following JavaScript skeletal program: //the main program var x: function sub1 () { var x: function sub2 () { } } function sub3 () { } Assume the execution of this program is in the following unit order: main calls sub1 sub1 calls sub2 sub2 calls sub3 a. Assuming static scoping, which declaration of x is the correct one for a reference to x in: i. sub1 ii. sub2 iii. sub3 b. Repeat part a, but assume dynamic...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT