What will be the output of the following program?
public class Main{ public static void main (String args [] ){ int i = 0 ; boolean bool1 = true ; boolean bool2 = false; boolean bool = false; bool = ( bool2 & m (i++) ); //1 bool = ( bool2 && m (i++) ); //2 bool = ( bool1 | m (i++) ); //3 bool = ( bool1 || m (i++) ); //4 System.out.println (i); /*from ww w . ja v a 2s. c o m*/ } public static boolean m (int i){ return i>0 ? true : false; } }
Select 1 option
Correct Option is : B
& and | do not short circuit the expression but && and || do.
As the value of all the expressions ( 1 through 4) can be determined just by looking at the first part, && and || do not evaluate the rest of the expression, so method 1() is not called for 2 and 4.
Hence the value of i is incremented only twice.