+ 1
What will be the output of the 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. 5 2 B. 5 3 C. 6 3 D. 6 4
4 Answers
+ 11
// output :::: 63
/*
loop1)z=0
x will become 1 and y remains 0
(bcz if 1st condition(++x >2) is false and && comes in between them full statement will result in false ...ie no need to check ++y >2
*/
//loop2) z=1
x becomes 2 and y remains 0 //bcz statement was again false due to 1st condition
//loop3) z=2
x becomes 3 and y becomes 1 ... but now statement is again false due to 2nd condition
//loop4)z=3
x becomes 4 and y becomes 2 // but statement is again false
//loop5)z=4
x becomes 5 and y becomes 3 //condition become true so value of x increased again & x becomes 6
//now the main loop condition becomes false ... so main loop stops
+ 11
yes @gajendra
+ 4
yes 6 3 is the correct ans bcoz loop will work 5 times than the value of x will be increased and Y become 3.
+ 1
6 3 is correct answer