The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+6 votes
981 views

Consider the following program

Program P2
    var n : int;
    
    procedure W(var x : int)
    begin
        x = x + 1;
        print x;
    end

    procedure D
    begin
        var n : int;
        n = 3;
        W(n);
    end

    begin    \\begin P2
        n=10;
        D;
    end

If the language has dynamic scooping and parameters are passed by reference, what will be printed by the program?

  1. 10
  2. 11
  3. 3
  4. None of the above
asked in Programming by Veteran (59.5k points)
edited by | 981 views

1 Answer

+11 votes
Best answer
ans is D.

here, because of dynamic scoping the value of n passed in w(n) will be n=3.

therefore o/p will be 4, which is not in any option.
answered by Loyal (8.2k points)
selected by
0
dynamic scoping part is understood by me the answer will be 4..but does parameters are passed by reference mean the below when converted to C language.

 int n;

 W(int *x) {*x = *x + 1; print *x;}     

D(){int n; n = 3; W(&n); }

main(){n=10; D(); }
0

You are correct  Nitika Gupta.Internally how it will change it does not matter,What matters in the actual value of n will be changed by which is passed by D to W 

Also, in the answer it says that

 here, because of dynamic scoping the value of n passed in w(n) will be n=3.

I dont think dynamic scoping will have any role here,because as long as we have local variable(x),it will be used.Scoping comes into picture,when variable getting used is not defined in the scope of the function which is using it.

Answer:

Related questions



Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

37,117 questions
44,701 answers
127,278 comments
43,765 users