Q 77.
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?**
1. Integer p,q,r
2. Set p=9, q=5, r=6
3. if((7^5)<p)
4. if((r+p-q)<(q-r))
5. r=4+p
6. Else
7. r=(q^4)+q
8. End if
9. End if
10. Print p+q+r
Note: ^ is the bitwise exclusive OR operator that compares each bit of its first operand to the corresponding bit of its second operand. If one bit is 0 and the other bit is 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0.
Ops:
Show answer & explanation
The outer if condition (7^5)<p evaluates to 2<9, which is true. The inner if condition (r+p-q)<(q-r) evaluates to 10<-1, which is false. So the else branch executes: r=(q^4)+q = (5^4)+5 = 1+5 = 6. Finally, p+q+r = 9+5+6 = 20.
Step-by-step Derivation:
Step 1: Initialize variables
p = 9, q = 5, r = 6
Step 2: Evaluate outer if condition (7^5)<p
7 in binary: 0111
5 in binary: 0101
7^5 (XOR): 0010 = 2
Is 2 < 9? YES, condition is true
Step 3: Evaluate inner if condition (r+p-q)<(q-r)
Left side: r+p-q = 6+9-5 = 10
Right side: q-r = 5-6 = -1
Is 10 < -1? NO, condition is false
Step 4: Execute else branch
r = (q^4)+q
5 in binary: 0101
4 in binary: 0100
5^4 (XOR): 0001 = 1
r = 1 + 5 = 6
Step 5: Print p+q+r
p+q+r = 9+5+6 = 20