Java Programming - Operators and Assignments - Discussion

Discussion Forum : Operators and Assignments - Finding the output (Q.No. 14)
14.
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;
        ++count;
    }

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

    void test() 
    {
        if ( b[0] && b[1] | b[2] )
            count++;
        if ( b[1] && b[(++count - 2)] )
            count += 7;
        System.out.println("count = " + count);
    }
}
count = 0
count = 2
count = 3
count = 4
Answer: Option
Explanation:

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.

Discussion:
31 comments Page 1 of 4.

Sanjai Krishnan said:   7 years ago
In the main method, we pass values for the index for 0 and 2.

Hence X[0]=true ,[2]= true. Default X[1]= false... Hence by changing the X[0], X[2] to true the count will become as count=2.

Next when the test method is called from the main method. First, if the condition is like (T&&F|T) which is false and hence count value is not incremented and remains as Count=2.

Second if condition is like (T && b[(++ count-2)])... Ie b[3-2] = b[1 ].
Since it is ++Count ... count = 3.... Where b[1] is false.

It looks like (T&&F ) which is False and hence count+=7 is not executed.
Thus the count=3 is to be displayed.
(1)

Sanjai Krishnan said:   7 years ago
In main method , we pass values for index for 0 and 2.

Hence X[0]=true ,[2]= true. Default X[1]= false... Hence by changing the X[0], X[2] to true the count will become as count=2.

Next when test method is called from main method.

First ,if condition is like (T&&F|T) which is false and hence count value is not incremented and remains as Count=2.

Second,if condition is like (T && b[(++ count-2)])... Ie b[3-2] = b[1 ].
Since it is ++Count ... count = 3.... Where b[1] is false.
It looks like (T&&F ) which is False and hence count+=7.
Thus the count=3 is to be displayed.

Shipra said:   1 decade ago
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 false..so count value will not be incremented n will remain 3.

Huy said:   10 years ago
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.

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

Jndlsnl@gmail.com said:   1 decade ago
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.

Harshit Rochiramani said:   7 years ago
When the second call is made i.e. if ( b[1] && b[(++count - 2)] ) => if ( FALSE && b[(++count - 2)]).

Another part is never executed because of compiler optimisation in this case JVM hence it outputs 3!

if you write if (b[(++count - 2)] && b[1]).
It will output 4.
(2)

RAHUL SRIVASTAVA said:   1 decade ago
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.

Siri said:   10 years ago
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:   9 years ago
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.


Post your comments here:

Your comments will be displayed after verification.