Shaikh Sakib
Shaikh Sakib

Reputation: 109

Boolean example return unexpected answer

I was going through java test given on indiabix and there a boolean question went something like -

public class If2 
{
    static boolean b1, b2;
    public static void main(String [] args) 
    {
        int x = 0;
        if ( !b1 ) /* Line 7 */
        {
            if ( !b2 ) /* Line 9 */
            {
                b1 = true;
                x++;
                if ( 5 > 6 ) 
                {
                    x++;
                }
                if ( !b1 ) /* Line 17 */
                    x = x + 10;
                else if ( b2 = true ) /* Line 19 */
                    x = x + 100;
                else if ( b1 | b2 ) /* Line 21 */
                    x = x + 1000;
            }
        }
        System.out.println(x);
    }
}

Now the explnation says - As instance variables, b1 and b2 are initialized to false. The if tests on lines 7 and 9 are successful so b1 is set to true and x is incremented. The next if test to succeed is on line 19 (note that the code is not testing to see if b2 is true, it is setting b2 to be true). Since line 19 was successful, subsequent else-if's (line 21) will be skipped. Thus the answer comes to 101 and not 111. But why would the if ( !b1 ) on Line 17 be ignored. Is it that the if ( !b1 ) on Line 7 is only considered true and later appearance of if ( !b1 ) on Line 17 is ignored. My understanding says answer should be 111 and not 101.

Upvotes: 2

Views: 256

Answers (4)

Vipul Gulhane
Vipul Gulhane

Reputation: 823

Hi This working fine check your if conditions combinations!!!

public class If2 {
    static boolean b1, b2;

    public static void main(String[] args) {
        int x = 0;
        if (!b1) /* Line 7 */
        {
            System.out.println("1"+b1);
            if (!b2) /* Line 9 */
            {   System.out.println("2"+b2);
                b1 = true;
                System.out.println("2.1 b1 is"+b1);
                x++;
                if (5 > 6) {
                    x++;
                }
                System.out.println("3"+b1);
                System.out.println("4"+b2);
                if (!b1) /* Line 17 */  //Vipul:- as    b1 is true this will not invoke         
                x = x + 10;             
                else if (b2 = true) /* Line 19 */ //Vipul:- as  b2 is true this will  invoke    
                    x = x + 100;
                else if (b1 | b2) /* Line 21 */
                    x = x + 1000;
                System.out.println("X"+x);
            }
        }
        System.out.println(x);
    }
}

Upvotes: 0

Mostch Romi
Mostch Romi

Reputation: 541

It is very easy to understand. at line no.11 b1 is set to true.and after x is increment by 1. so x=1. Then at line no.17 if condition getting false !b1=>!true, so add not perform.

Then line no.19 ,condition getting true and add 100 to x i.e x=x+100. so result is 101.

Upvotes: 1

Sweeper
Sweeper

Reputation: 270890

The if statement on line 17 is indeed evaluated. At that point, b1 is true because it is assigned true in line 11. This means that !b1 evaluates to false.

Because the if at line 17 failed to execute, the else ifs get a chance to run. In this case, b2 = true will always evaluate to true because an assignment expression always evaluates to the value being assigned. As a result, x, which was 1, is now 101, with the 100 added onto it.

Let's suppose !b1 were true at line 17, then else if (b2 = true) will not run, and you would get 11 as output.

Upvotes: 0

Eran
Eran

Reputation: 393781

In line 17 b1 is already true, since it was set to true on line 11. Therefore if ( !b1 ) evaluates to false.

Therefore only else if ( b2 = true ) is evaluated to true (and also assigns true to b2), and adds 100 to x.

x was already incremented once before that (line 12), so it ends up with the value 101.

Note that 111 would have been an impossible output even if if ( !b1 ) evaluated to true, since in that case else if ( b2 = true ) wouldn't be evaluated and x would end up with the value 11.

Upvotes: 7

Related Questions