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- A. 30 15 20
- B. 20 20 20
- C. 30 30 10
- 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.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- What will be the output of the following pseudocode? (Arrays are 0-indexed.) Integer a[7] = {1, 2, 4, 5, 7, 9, 10} Integer i = 0, j = 6, c…
- What will be the output of the following pseudocode? (Division is integer division; arrays are 0-indexed.) Integer a[9] = {2, 3, 3, 3, 5…
- What will be the output of the following pseudocode? (& is bitwise AND; arrays are 0-indexed.) Integer m[4][4] For i = 0 to 3 For j = 0 to…
- What will be the output of the following pseudocode? Function J(n, k) If n = 1 Then Return 0 Return (J(n - 1, k) + k) mod n End Function…
- What will be the output of the following pseudocode? Integer n = 1, s = 0 Repeat s = s + n n = n 2 + 1 Until s >= 100 Print n, s
- What will be the output of the following pseudocode? Integer s = 0 for (each i from 1 to 5) s = s + i i end for Print s