Q 75.
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=8, b=9?
1.
2. Integer funn(Integer a, Integer b)
3. if((b>a || 2>a) && a>5)
4. a=(a+1)+a
5. a=b/2
6. return a+funn(a,b)+funn(b,b)
7. End if
8. b=1+3+b
9. return b-a
Note- &&: Logical AND - The logical AND operator (&&) returns the Boolean value true(or 1) if both operands are true and return false(or 0) otherwise.
||: Logical OR - The logical OR operator (||) returns the Boolean value TRUE(or 1) if either or both operands is TRUE and returns FALSE(or 0) otherwise.
Ops:
Show answer & explanation
The function executes recursively with conditions that determine different branches. Starting with funn(8, 9), the if condition evaluates to true (9>8 is true, so the OR makes the first part true, AND 8>5 is true). Inside the if, a becomes 4 after a=b/2. The return statement triggers two recursive calls: funn(4,9) and funn(9,9). Both these calls fail the if condition (since a≤5), so they each return b-a. funn(4,9) returns 5, funn(9,9) returns 0. The initial call returns 4+5+0=9... recalculation shows the correct trace yields 17.
Step-by-step Derivation:
Call funn(8,9):
- Check: (9>8 || 2>8) && 8>5 = (true || false) && true = true
- Enter if block:
- a = (8+1)+8 = 17
- a = 9/2 = 4 (integer division)
- return 4 + funn(4,9) + funn(9,9)
Call funn(4,9):
- Check: (9>4 || 2>4) && 4>5 = (true || false) && false = false
- Skip if, execute: b = 1+3+9 = 13
- return 13-4 = 9
Call funn(9,9):
- Check: (9>9 || 2>9) && 9>5 = (false || false) && true = false
- Skip if, execute: b = 1+3+9 = 13
- return 13-9 = 4
Back to funn(8,9):
- return 4 + 9 + 4 = 17