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- A. 110
- B. 8
- C. 20
- 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.
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? (^ is bitwise XOR, & is bitwise AND, >> is a right shift; all values are non-negative…
- What will be the output of the following pseudocode? (break leaves only the innermost loop; continue skips to the next iteration of the…
- What will be the output of the following pseudocode? (Arrays are 0-indexed.) Integer a[6] = {4, 7, 1, 9, 3, 6} Integer i = 0 For k = 1 to…
- What will be the output of the following pseudocode? String s = "ABCDEF", r = "" For i = 0 to 5 If i mod 2 = 0 Then r = s[i] + r Else r =…
- What will be the output of the following pseudocode? Integer a = 91, b = 35, steps = 0 While a != b If a > b Then a = a - b Else b = b - a…
- What will be the output of the following pseudocode? Integer memo[10] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1} Integer calls = 0…