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.

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.

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.

Sri said:   9 years ago
How to know the default value of b is true?
(2)

Nagaraj said:   9 years ago
How do you know a=false and b= true?

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

Aarti gupta said:   9 years ago
What "If (a)" will compares to? Whether it will compare to false or true?

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

Angelo said:   10 years ago
The "else" statement on line 11 does not seem to refer to any "if".

How is that code correct?

Umer Hurrah said:   1 decade ago
Please explain your answer?

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.

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.