Question
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);
}
}
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 C
Was this answer helpful ?
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.
Was this answer helpful ?
More Questions on This Topic :
Question 6.
Which of these statements are incorrect?
....
Question 7.
Which of these statements are incorrect?
....
Question 10.
Which of these statements are incorrect?
....
Submit Solution