ProgrammingHardTCS NQT Advanced · Accenture

What will be the output of the following pseudocode? (parameters are passed by value; inside g, x is the global x; operands are evaluated left to right)

What will be the output of the following pseudocode? (parameters are passed by value; inside g, x is the global x; operands are evaluated left to right)
Integer x = 5
Function f(Integer x)
    x = x + 1
    return x * 2
End Function
Function g()
    x = x + 3
    return x
End Function
Integer y = f(x) + g()
Print y, x
  1. A. 20 5
  2. B. 21 9
  3. C. 20 8
  4. D. 18 8

Correct answer: C. 20 8

f gets a copy of x, so its x = 6 is local and it returns 12 while the global x stays 5. g changes the global x to 8 and returns 8. y = 12 + 8 = 20 and x = 8.

More Pseudocode and Code Tracing questions

Exams that ask this topic