Saturday, 26 September 2009

Java Certification Question 0105

What will happen when you attempt to compile and run the following code?


int Output=10;

boolean b1 = false;

if((b1==true) && ((Output+=10)==20)){

System.out.println("We are equal "+Output);

}else

{

System.out.println("Not equal! "+Output);

}


1) Compile error, attempting to peform binary comparison on logical data type
2) Compilation and output of "We are equal 10"
3) Compilation and output of "Not equal! 20"
4) Compilation and output of "Not equal! 10"





Answer :

4) Compilation and output of "Not equal! 10"

The output will be "Not equal 10". This illustrates that the Output +=10 calculation was never performed because processing stopped after the first operand was evaluated to be false. If you change the value of b1 to true processing occurs as you would expect and the output is "We are equal 20";.

1 comment:

Ann said...

i was just browsing thru the net and got struck in ur blog. in the java fundamentals post http://weekendtech.blogspot.com/2007/08/java-language-fundamentals-part-9.html i could see an exercise BigPet.Can you post the solution for this.