search
Log In
8 votes
2k 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
in Programming
edited by
2k views

3 Answers

14 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.

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(); }
3

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.

0
Even if you run it in C, the answer will be 4. C uses static scoping if anyone wants to check.
0 votes
It print 4 , which is not in option,

So correct option is D
0 votes
n=3
W(n)=W(3)
Procedure W(var x; int)
begin
x = x+1 = 3+1 = 4
Print x → Print x=4
end

Hence option D is correct
0
I think it  will print 5

it is passing parameters by reference,1. the first function call is W(&n)(passing reference given in the question) so *x=*x+1 it will print 4 and the value of n is changed into 4

now the 2nd function call is D where it will call W(&n) again, then *x=*x+1 so it will print 5.

correct me if I'm wrong
Answer:

Related questions

17 votes
3 answers
1
3.9k views
What is printed by the print statements in the program $P1$ assuming call by reference parameter passing? Program P1() { x = 10; y = 3; func1(y,x,x); print x; print y; } func1(x,y,z) { y = y + 4; z = x + y + z } $\text{10, 3}$ $\text{31, 3}$ $\text{27, 7}$ None of the above
asked Sep 15, 2014 in Programming Kathleen 3.9k views
3 votes
1 answer
2
650 views
Consider the following program in pseudo-Pascal syntax. What is printed by the program if parameter $a$ in procedure test1 is passed as call-by-reference parameter call-by-value-result parameter program Example (input, output) var b: integer; procedure test2: begin b:=10; end procedure test1 (a:integer ... ('point 2: ', a, b); end begin (*Example*) b:=3; test1(b); writeln('point3: ', b); end
asked Oct 10, 2014 in Programming Kathleen 650 views
5 votes
1 answer
3
1.1k views
Consider the following program in Pseudo-Pascal syntax. program what: var z: integer procedure recur(x): begin if x <= 40 then begin x:x+z recur(x); z:=x+10 end end(*recur*) begin(*what*) z=10; recur(z); writeln(z) end Suppose the parameter ... value is printed by program? How many times is &lsquo;recur&rsquo; called? What value is printed by the program if the parameter is passed by reference?
asked Sep 29, 2014 in Programming Kathleen 1.1k views
10 votes
3 answers
4
3.6k views
Consider the following code written in a pass-by-reference language like FORTRAN and these statements about the code. subroutine swap(ix,iy) it = ix L1 : ix = iy L2 : iy = it end ia = 3 ib = 8 call swap (ia, ib+5) print *, ia, ib end S1: The compiler will generate ... and 8 S5: The program will print 13 and -2 Exactly the following set of statement(s) is correct: S1 and S2 S1 and S4 S3 S1 and S5
asked Sep 26, 2014 in Programming Rucha Shelke 3.6k views
...