Output of Java program... if((++x > 2) || (++y > 2))... x++;
- A. 8 2
- B. 8 5
- C. 8 3
- D. 5 3
Answer: A. 8 2
Explanation: Short circuit OR. x=0, y=0. \nLoop 1 (z=0): ++x(1) > 2 False. ++y(1) > 2 False. x++(2). x=2, y=1. \nLoop 2 (z=1): ++x(3) > 2 True. Short circuit (y not inc). x++(4). x=4, y=1. \nLoop 3 (z=2): ++x(5) > 2 True. Short circuit. x++(6). x=6, y=1. \nLoop 4 (z=3): ++x(7) > 2 True. Short circuit. x++(8). x=8, y=1. \nLoop 5 (z=4): ++x(9) > 2 True. Short circuit. x++(10). x=10, y=1. \nWait, z<5 means 0,1,2,3,4. 5 iterations. x starts 0. \nIter 1: ++x=1. Fail. ++y=1. Fail. x++ -> x=2. y=1. \nIter 2: ++x=3. True. Skip y. x++ -> x=4. y=1. \nIter 3: ++x=5. True. Skip y. x++ -> x=6. y=1. \nIter 4: ++x=7. True. Skip y. x++ -> x=8. y=1. \nIter 5: ++x=9. True. Skip y. x++ -> x=10. y=1. \nResult 10 1? Options are 8 2, 8 5, 8 3, 5 3. \nLet's re-eval. `x++` is inside `if` block? `{ x++; }`. \nIter 1: ++x(1)>2 False. ++y(1)>2 False. Block not exec. x=1, y=1. \nIter 2: ++x(2)>2 False. ++y(2)>2 False. Block not exec. x=2, y=2. \nIter 3: ++x(3)>2 True. Block exec: x++(4). y=2. \nIter 4: ++x(5)>2 True. Block exec: x++(6). y=2. \nIter 5: ++x(7)>2 True. Block exec: x++(8). y=2. \nResult: x=8, y=2. Option (a).