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 B
Was this answer helpful ?
The first two iterations of the for loop both x and y are incremented. On the third iteration
x is incremented, and for the first time becomes greater than 2. The short circuit or operator
|| keeps y from ever being incremented again and x is incremented twice on each of the last
three iterations.
Was this answer helpful ?
More Questions on This Topic :
Question 10.
What is the output of this program?
class bitwi....
Submit Solution