ProgrammingHardTCS NQT · Infosys · Capgemini

What will be the output of the following pseudocode? (Static scoping: a name used inside a procedure refers to the variable visible where the procedure is written, not to the caller's local variables. Parameters are passed by value.)

What will be the output of the following pseudocode? (Static scoping: a name used inside a procedure refers to the variable visible where the procedure is written, not to the caller's local variables. Parameters are passed by value.)
Integer x = 10
Procedure q()
    x = x * 2
    Print x
End Procedure
Procedure p(Integer x)
    x = x + 5
    q()
    Print x
End Procedure
p(x)
Print x
  1. A. 30 15 20
  2. B. 20 20 20
  3. C. 30 30 10
  4. D. 20 15 20

Correct answer: D. 20 15 20

Inside p, x is a local copy (15 after the addition). q was written at the top level, so its x is the global one: it doubles 10 to 20 and prints 20. p then prints its own x = 15, and the global x is still 20. Dynamic scoping would give 30 30 10.

More Pseudocode and Code Tracing questions

Exams that ask this topic