ProgrammingVery hardTCS NQT · Infosys · Capgemini

What will be the output of the following pseudocode? (AND has higher precedence than OR, and && style short-circuit evaluation is used: the right operand is evaluated only if needed.)

What will be the output of the following pseudocode? (AND has higher precedence than OR, and && style short-circuit evaluation is used: the right operand is evaluated only if needed.)
Integer c = 0
Function g(k)
    c = c + k
    Return (c mod 2 = 1)
End Function
If g(1) OR g(2) AND g(3) Then
    c = c * 10
End If
If g(4) AND g(5) OR g(6) Then
    c = c + 100
End If
Print c
  1. A. 110
  2. B. 8
  3. C. 20
  4. D. 175

Correct answer: C. 20

g(1) makes c = 1 (odd, true), so the OR skips g(2) and g(3); c becomes 10. In the second test g(4) gives c = 14 (even, false), so the AND skips g(5); the OR then calls g(6): c = 20 (even, false), so nothing is added. Output 20.

More Pseudocode and Code Tracing questions

Exams that ask this topic