Java Programming - Flow Control - Discussion

Discussion Forum : Flow Control - General Questions (Q.No. 1)
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") ;
        } 
        else 
        {
            System.out.println( "ELSE" ) ; 
        } 
    } 
}
If a is true and b is true then the output is "A && B"
If a is true and b is false then the output is "notB"
If a is false and b is true then the output is "ELSE"
If a is false and b is false then the output is "ELSE"
Answer: Option
Explanation:

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".

Discussion:
19 comments Page 1 of 2.

Priyanka said:   1 decade ago
Nice mind boggling question..... !! i took half an hour to solve it....

Srikanth Reddy said:   1 decade ago
Good question. !it is amazing.

Ram said:   1 decade ago
Need more questions like this.

Aakash said:   1 decade ago
Can anybody tell me why the condition in A will be trapped in line 12.

Naresh said:   1 decade ago
After line 11 !b means "not true" i.e false.so condition failed now else statement will execute.

Tharani said:   1 decade ago
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:   1 decade ago
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:   1 decade ago
@Divya.

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:   1 decade ago
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:   1 decade ago
@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.


Post your comments here:

Your comments will be displayed after verification.