ISRO | ISRO CS 2013 | Question 63
What is the output of the following Java program?
Class Test { public static void main (String [] args) { int x = 0; int y = 0; for (int z = 0; z < 5; z++) { if((++x > 2) || (++y > 2)) { x++; } } System.out.println( x + " " + y); } }
(A) 8 2
(B) 8 5
(C) 8 3
(D) 5 3
Answer: (A)
Explanation: By applying short circuit technique,
z = 0: x = 1, y = 1, if condition false z = 1: x = 2, y = 2, if condition false z = 2: x = 3, if condition true and due to short circuiting ++y is not evaluated, x++ // x = 4 z = 3: x = 5, again if condition is true, x++ // x = 6 z = 4: x = 7, condition true, x++ // x = 8
So, option (A) is correct.
Quiz of this Question
Please Login to comment...