Java Programming - Operators and Assignments - Discussion


What will be the output of the program?

class BoolArray 
    boolean [] b = new boolean[3];
    int count = 0;

    void set(boolean [] x, int i) 
        x[i] = true;

    public static void main(String [] args) 
        BoolArray ba = new BoolArray();
        ba.set(ba.b, 0);
        ba.set(ba.b, 2);

    void test() 
        if ( b[0] && b[1] | b[2] )
        if ( b[1] && b[(++count - 2)] )
            count += 7;
        System.out.println("count = " + count);

[A]. count = 0
[B]. count = 2
[C]. count = 3
[D]. count = 4

Answer: Option C


The reference variables b and x both refer to the same boolean array. count is incremented for each call to the set() method, and once again when the first if test is true. Because of the && short circuit operator, count is not incremented during the second if test.

Biswa said: (Apr 13, 2011)  
Initially count is zero. The set method is call two times, so it 2. How we get 3 please explain.

Samir said: (Jul 8, 2011)  
When test method call at that time count is increment at one more time.

Gopi said: (Sep 13, 2011)  
But why it is incremented only in first if in test method ?

Jndlsnl@Gmail.Com said: (Dec 2, 2011)  
Bcz in if ( b[0] && b[1] | b[2] ) ---> if(true && false | true) ----> if(true) so count++ =3.

but if ( b[1] && b[(++count - 2)] ) ----> if(false && b[(3-2)])---> if(false && false) ----> if(false) ----> so it will not execute.

Correct me if I am wrong.

Thanvir513@Yahoo.Com said: (Aug 7, 2012)  
Why b[0]=true, b[1]=false and b[2]=true ? please explain clearly.

Shipra said: (Aug 22, 2012)  
ba.set(ba.b, 0); will set b[0]=true and ba.set(ba.b, 2); will set b[2]=true.. n b[a]=0 so we take it as false.
now in test method first if statement will return true because (true & false) || true = true;
now in second if statement first statement b[1] is false so next statement will not be executed ..because there is AND operator does not check second condition if first is count value will not be incremented n will remain 3.

Gourav Soni said: (Aug 26, 2012)  
@[] :
in second if condition we have used ++count...which should increase count by 1 count shud be 4 ..ryt??

Varsha said: (Aug 31, 2012)  
@gourav : evn i had the same doubt but after reading shipras explanation i am convinced and my doubt is resolved.....

Ashi said: (Sep 7, 2012)  

Nitish said: (Oct 4, 2012)  
Ashi:-because by default the value for boolean will be false.
Gourav Soni and Varsha:-its working like:-
if ( b[1] && b[(++count - 2)] )
count += 7;
if(false && ----- )
here b[1] is false.So.. if the value for left operand is false then it won't check for right operand and will go to next line.that's y count won't be incremented further.
i hope it will be helpful for you.

Jigar said: (Nov 5, 2012)  
Two set() method incremented count,
Now when test() called,
1st condition is true,
Hence, count is incremented! (post incremented).

Vishumunjal said: (Jul 6, 2013)  
Why only the first statement is true not second ?

Rahul Srivastava said: (Oct 11, 2013)  
The answer is wrong because the count is instance variable so in each block it will take the memory in local stack and destroy after the compilation of the function. Answer will be 1 and to make it 3 count should be static.

Javeed said: (Dec 10, 2014)  
I didn't understand.

Suresh said: (Mar 17, 2015)  

Answer is correct because instance variables are stored in heap.not in stack.stack will have only local variables.

Pravin said: (Mar 24, 2015)  
if (b[0] && b[1] | b[2])
if (b[1] && b[(++count - 2)])
count += 7;
System.out.println("count = " + count);

Sanju said: (Jul 26, 2015)  
Please explain whole program.

Huy said: (Oct 6, 2015)  
ba.set(ba.b, 0); // b[0] = true, count = 1.
ba.set(ba.b, 2); // b[2] = true, count = 2.

First if fails since b[1] is false,

Second if fails for same reason, but while evaluating, ++count increases it to 3.

Theoretically, it shouldn't because the program uses lazy approach and would not evaluate the rest of the if statement at all since it fails at the start and it is a && case, or so I read it somewhere.

Siri said: (Dec 5, 2015)  
Count must be 1 because count is not a static declare. Count will be increment to 2 but after end of the block the value will back to 0. If it is static then only count is 3. Correct me if I am wrong.

Rishi said: (Mar 19, 2016)  
All the static and non static variables retain its value until the program is terminated it doesn't matter whether it is local or global.

I think you got it @Siri.

Ravindra Singh said: (Sep 7, 2016)  
Nice, got it clearly. Thanks.

Ravi said: (Dec 5, 2016)  
Thanks @Huy.

Swagatika said: (Dec 8, 2016)  
Please anyone explain the compleat program.

Bittu said: (May 2, 2017)  
Yes, @Jndlsnl.

You are right. Thanks a lot for writing down if values.

Sainath said: (Jun 20, 2017)  
You are absolutely right Thanks, @Jndlsnl.

Parth Soni said: (Nov 15, 2017)  
Can anyone explain how b and x refer to the same array because x's scope is in set method only and it will not update the array b right ?

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.