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:
31 comments Page 2 of 4.
Aparna said:
8 years ago
Hi, Please explain this problem using OR.
Preethi said:
8 years ago
Thank you good explanation @G.Vivekananda.
Aayushi jain said:
8 years ago
Why y is not get executed?
Avni said:
8 years ago
Why y =0 in first case, why not 1?
Udaya said:
8 years ago
Good explanation, Thanks @G. Vivek.
Sai kiran said:
9 years ago
Awesome explanation @Vivek.
Gajendra chouriya said:
9 years ago
Thanks for the explanation @G.vivek.
Magesh said:
2 decades ago
Can any one please mention what is short circuit operator?
Sunny deol said:
9 years ago
Now I understand this, thank you @Nataliia.
Aashish said:
9 years ago
Thanks for your explanation @Arnold Villasanta.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers