ProgrammingHardProduct company OA

What will be the output of the following pseudocode? (A static variable is initialised once and keeps its value between calls; operands are evaluated left to right.)

What will be the output of the following pseudocode? (A static variable is initialised once and keeps its value between calls; operands are evaluated left to right.)
Function f()
    static Integer c = 0
    c = c + 2
    return c
End Function
Print f() + f() * f()
  1. A. 14
  2. B. 18
  3. C. 12
  4. D. 26

Correct answer: D. 26

With left-to-right evaluation the three calls return 2, 4 and 6 in that order; precedence then groups them as 2 + 4 * 6 = 26. If the two calls of the multiplication ran first (2·4, then + 6) the result would be 14 – precedence decides grouping, not the order of the calls. Without the static keyword every call would return 2.

More Pseudocode and Code Tracing questions

Exams that ask this topic