What will be the output of the following pseudo code for a = 2, b = 4?
Accenture technical mcq question, verified with a worked answer. Free to practise - no sign-up.
What will be the output of the following pseudo code for a = 2, b = 4?
Integer funn(Integer a, Integer b)
if (a < 5 && (4 - a) > (a - b))
a = 1 + a + a
a = a + 2
End if
return a + b
Show answer & explanation
Answer: A. 11
The condition (a < 5 && (4 - a) > (a - b)) evaluates to true, so the if block executes. After a = 1 + a + a and a = a + 2, a becomes 6. The function returns a + b = 6 + 4 = 10. However, re-checking: a = 1 + 2 + 2 = 5, then a = 5 + 2 = 7, so return 7 + 4 = 11.
Step-by-step Derivation:
Step 1: Initial values: a = 2, b = 4
Step 2: Evaluate condition (a < 5 && (4 - a) > (a - b))
- a < 5: 2 < 5 = true
- (4 - a) > (a - b): (4 - 2) > (2 - 4) → 2 > -2 = true
- true && true = true, so enter if block
Step 3: Execute a = 1 + a + a - a = 1 + 2 + 2 = 5
Step 4: Execute a = a + 2 - a = 5 + 2 = 7
Step 5: Return a + b - return 7 + 4 = 11