Join ExamsbookAnswer : 2. "Compilation and output of "Not equal! 10""
What is the output after compile and run the following code ?
int Output = 10;boolean b = false;if((b == true) && ((Output += 10) == 20)){ System.out.println("We are equal " + Output);}else{ System.out.println("Not equal! " + Output);}5
Q: What is the output after compile and run the following code ? int Output = 10;boolean b = false;if((b == true) && ((Output += 10) == 20)){ System.out.println("We are equal " + Output);}else{ System.out.println("Not equal! " + Output);}
- 1Compilation and output of "We are equal 10"false
- 2Compilation and output of "Not equal! 10"true
- 3Compilation error, attempting to perform binary comparison on logical data typefalse
- 4Compilation and output of "Not equal! 20"false
- Show AnswerHide Answer
- Workspace
Answer : 2. "Compilation and output of "Not equal! 10""
Explanation :
Answer: B) Compilation and output of "Not equal! 10" Explanation: The output will be "Not equal! 10". Please note that && is logical AND operator. If first operand before (&&) is false then the other operand will not be evaluated. 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 would be "We are equal 20".