6,231 views
5 5 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
22 22 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:
Position:
Show:

Related questions

3 3 votes
1 answers 1 answer
7.4k
7.4k views
Misbah Ghaya asked Jun 5, 2016
7,379 views
The built-in base class in java, which is used to handle all exceptions isRaiseExceptionErrorThrowable
0 0 votes
2 2 answers
1.2k
1.2k views
admin asked Sep 28, 2024
1,241 views
A standard Java API for monitoring and managing applications isJVMJVNJMXJMY 
3 3 votes
3 answers 3 answers
8.3k
8.3k views
shivanisrivarshini asked Jun 27, 2016
8,300 views
Consider the following Java code fragment:public class While { public void loop() { int x = 0; while(1) { System.out.println("x plus one is" +(x+1)); } } }There is syntax...
8 8 votes
2 answers 2 answers
5.9k
5.9k views
go_editor asked Jun 23, 2016
5,914 views
Find the output of the following Java code line System.out.printIn(math.floor(-7.4))-7-8-7.4-7.0