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.

Matthew said:   1 decade ago
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") ;
}
else
{
cout << ( "ELSE" ) ;
} ;
} ;
};

};


OUTPUT: not B.

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.

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.

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.

Ashok steve said:   8 years ago
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.

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.

Nikita said:   10 years ago
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"?

Osama said:   5 years ago
By default a =false and b=false.
So the control will got to not b.
Because !b means true. that is !false
So, option B is correct.

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?

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


Post your comments here:

Your comments will be displayed after verification.