Java Programming - Operators and Assignments - Discussion
Discussion Forum : Operators and Assignments - Finding the output (Q.No. 7)
7.
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);
}
}
Answer: Option
Explanation:
In the first two iterations x is incremented once and y is not because of the short circuit && operator. In the third and forth iterations x and y are each incremented, and in the fifth iteration x is doubly incremented and y is incremented.
Discussion:
30 comments Page 1 of 3.
Moha said:
5 years ago
x=1 >2 wrong
z++
z=1
x=2>2 wrong
z++
z=2
x=3>2 yes
&&
1>2? wrong
z=3
x=4
2>2 rong
z=4
x=5
3>2 yes.
x++ x=6.
So, o/p: 6 3.
z++
z=1
x=2>2 wrong
z++
z=2
x=3>2 yes
&&
1>2? wrong
z=3
x=4
2>2 rong
z=4
x=5
3>2 yes.
x++ x=6.
So, o/p: 6 3.
(5)
Keerthi said:
2 years ago
@All.
Check the first condition and till it is true it won't go to the second condition so when x becomes 3 and the condition satisfies (3>2) then only condition two comes into consideration like y=1 then x also increments by 4.
Check the first condition and till it is true it won't go to the second condition so when x becomes 3 and the condition satisfies (3>2) then only condition two comes into consideration like y=1 then x also increments by 4.
(2)
Niharika patidar said:
6 years ago
Please explain to me.
What is the reason that ++y is not executed for the first two times?
What is the reason that ++y is not executed for the first two times?
(1)
G.vivek said:
9 years ago
Short-circuit(&&):
(x&&y) -> X is false, then Y not evaluated. X is true, then Y will be evaluated.
Above problem's Solution is:
for(int z=0;0<5;z++)->if((1>2)&&(0>2))..X is false then Y not evaluated
for(int z=1;1<5;z++)->if((2>2)&&(0>2))..X is false then Y not evaluated
for(int z=2;2<5;z++)->if((3>2)&&(1>2))..X is true then Y is evaluated
for(int z=3;3<5;z++)->if((4>2)&&(2>2))..X is true then Y is evaluated
for(int z=4;4<5;z++)->if((5>2)&&(3>2))..X is true then Y is evaluated
Finally, (X and Y both as true then X value was incremented)
=> X is 6
=> Y is 3
If(X&&Y) //X and Y both true then
{
X++; // X value is incremented
}
(x&&y) -> X is false, then Y not evaluated. X is true, then Y will be evaluated.
Above problem's Solution is:
for(int z=0;0<5;z++)->if((1>2)&&(0>2))..X is false then Y not evaluated
for(int z=1;1<5;z++)->if((2>2)&&(0>2))..X is false then Y not evaluated
for(int z=2;2<5;z++)->if((3>2)&&(1>2))..X is true then Y is evaluated
for(int z=3;3<5;z++)->if((4>2)&&(2>2))..X is true then Y is evaluated
for(int z=4;4<5;z++)->if((5>2)&&(3>2))..X is true then Y is evaluated
Finally, (X and Y both as true then X value was incremented)
=> X is 6
=> Y is 3
If(X&&Y) //X and Y both true then
{
X++; // X value is incremented
}
(1)
Rajesh said:
8 years ago
Awesome, thanks @Vivek.
Sushaja said:
3 years ago
@Arnold.
Thank you for explaining the short circuit operator's operation in detail.
Thank you for explaining the short circuit operator's operation in detail.
Unknown said:
6 years ago
output of int z=2; while(z<20) if((z++)%2==0) system.out.println(z);
Anu said:
6 years ago
Thanks @Saranya and @G. Vivek.
Swapnil said:
7 years ago
Thank you so much @Arnold.
Aparna said:
7 years ago
Hi, Please explain this problem using OR.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers