Java Programming - Flow Control - Discussion

Discussion :: Flow Control - General Questions (Q.No.1)


public void foo( boolean a, boolean b)
    if( a ) 
        System.out.println("A"); /* Line 5 */
    else if(a && b) /* Line 7 */
        System.out.println( "A && B"); 
    else /* Line 11 */
        if ( !b ) 
            System.out.println( "notB") ;
            System.out.println( "ELSE" ) ; 

[A]. If a is true and b is true then the output is "A && B"
[B]. If a is true and b is false then the output is "notB"
[C]. If a is false and b is true then the output is "ELSE"
[D]. If a is false and b is false then the output is "ELSE"

Answer: Option C


Option C is correct. The output is "ELSE". Only when a is false do the output lines after 11 get some chance of executing.

Option A is wrong. The output is "A". When a is true, irrespective of the value of b, only the line 5 output will be executed. The condition at line 7 will never be evaluated (when a is true it will always be trapped by the line 12 condition) therefore the output will never be "A && B".

Option B is wrong. The output is "A". When a is true, irrespective of the value of b, only the line 5 output will be executed.

Option D is wrong. The output is "notB".

Priyanka said: (Nov 13, 2010)  
Nice mind boggling question..... !! i took half an hour to solve it....

Srikanth Reddy said: (Mar 6, 2011)  
Good question. !it is amazing.

Ram said: (Mar 9, 2011)  
Need more questions like this.

Aakash said: (Sep 28, 2011)  
Can anybody tell me why the condition in A will be trapped in line 12.

Naresh said: (Aug 30, 2012)  
After line 11 !b means "not true" i.e condition failed now else statement will execute.

Tharani said: (Apr 20, 2013)  
Default Values:

It's not always necessary to assign a value when a field is declared. Fields that are declared but not initialized will be set to a reasonable default by the compiler. Generally speaking, this default will be zero or null, depending on the data type. Relying on such default values, however, is generally considered bad programming style.

The following chart summarizes the default values for the above data types.

Data Type : Default Value (for fields)
byte : 0
short : 0
int : 0
long : 0L
float : 0.0f
double : 0.0d
char : '\u0000'
String (or any object) : null
boolean : false

According to this reference the default value of the boolean type was FALSE, so the output must be (** not B **) because negation of true became false(!b). so try to put correct answer otherwise user may feel that this site was unreliable.

Divya said: (Dec 22, 2013)  
Still now I didn't get this question how the output will be else can any one explain me in step by step manner?

Chinmoy said: (May 13, 2014)  

Step 1: If a is true,irrespective of any value of b, first if condition will satisfy so, - output will be "A" ie given 1st two options are incorrect.

Step 2: If a is false and b is true then, if(a) and if(a && b) won't execute, and control comes directly to else part,now here if(!b) means (if not true) ie false, then the control will go to else within else part and print "ELSE". Thus option 3 is correct.

Raghu said: (Sep 15, 2014)  
See @Divya.

We have to pass Boolean value in if() statement.

But we are giving boolean b in if() stmt... Actually for Boolean the default value is FALSE so after checking all conditions else block is executed.

Naresh Goud said: (Oct 30, 2014)  
@Tharani you are right why because,

Default Boolean value is false, so "if (a) " and "if (a&&b) " both are not going to be execute. Next "if (!b) " i.e., if (!false) it means !false = true so condition is true so it will be executed.

So output is not B.

Matthew said: (Feb 10, 2015)  
Agreed, @Naresh & @Tharani.

The program is taking in two boolean values, "a" and "b". Since they were not set in the program (E.g. boolean a = TRUE/FALSE) they will take the default value, which is FALSE.

An && statement both variable statements have to be TRUE and an | statement one of the variables must be TRUE.

The logic follows:

a is FALSE, next statement.
a & b are FALSE, next statement.
Not b is TRUE, stop program.

Here is a C++ equivalent I quickly wrote: a = 0 and b = 0 means FALSE.

#include <iostream>
#include <stdlib.h>

using namespace std;

int main(bool a, bool b){
a = 0;
b = 0;
if( a )
cout << ("A"); /* Line 5 */
else if(a && b) /* Line 7 */
cout <<( "A && B");
else /* Line 11 */
if ( !b )
cout << ( "notB") ;
cout << ( "ELSE" ) ;
} ;
} ;


OUTPUT: not B.

Umer Hurrah said: (Mar 25, 2015)  
Please explain your answer?

Angelo said: (Nov 27, 2015)  
The "else" statement on line 11 does not seem to refer to any "if".

How is that code correct?

Nikita said: (Dec 16, 2015)  
Can anyone please explain?

If (!b) i.e. b is false, default value Boolean b is false there by making condition true so answer should b "not b"?

Aarti Gupta said: (Feb 27, 2016)  
What "If (a)" will compares to? Whether it will compare to false or true?

Nagaraj said: (Jan 27, 2017)  
How do you know a=false and b= true?

Default value boolean is 'false' for both a and b.

Sri said: (Feb 14, 2017)  
How to know the default value of b is true?

Ashok Steve said: (Jul 19, 2017)  
In if() true means if block and false means else block that's why if you give false (!B) than it takes true so if block will be executed and you give true(!B) than it takes as false so else block will be executed.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.