3,474 views
4 votes
4 votes

What is the output of the following 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.printIn (x+ "" + y); 
    }
}
  1. 8 2
  2. 8 5
  3. 8 3
  4. 5 3

1 Answer

Best answer
20 votes
20 votes

Before solving the questions, if we think why they asked this particular question then probability of getting correct answer increased. For example this question is an example of Short-Circuit Evaluation

Here is the Answer:

$z = 0$  : $x = 1$ and $y = 1$, Because $x++;$ will not be executed because condition is false

$z = 1$  : $x = 2$ and $y = 2$, Because $x++;$ will not be executed because condition is false

$z = 2$  : $x = 4$ and $y = 2$, Here $x++;$ will be executed, but $++y$ will not be executed due to short circuit evaluation

$z = 3$  : $x = 6$ and $y = 2$, Here $x++;$ will be executed, but $++y$ will not be executed due to short circuit evaluation

$z = 4$  : $x = 8$ and $y = 2$, Here $x++;$ will be executed, but $++y$ will not be executed due to short circuit evaluation

Hence at the end, It will print 8 2.

selected by
Answer:

Related questions

3 votes
3 votes
1 answer
1
makhdoom ghaya asked Jun 5, 2016
6,285 views
The built-in base class in java, which is used to handle all exceptions isRaiseExceptionErrorThrowable
0 votes
0 votes
1 answer
2
admin asked Apr 1, 2020
945 views
Is null an object?YesNoSometimes yesNone of these
0 votes
0 votes
1 answer
3
admin asked Mar 31, 2020
683 views
Earlier name of Java programming language was:OAKDNetbeanEclipse
0 votes
0 votes
1 answer
4
admin asked Mar 31, 2020
998 views
Which of the following is a platform free language?JAVACAssemblyFortran