Open In App

Java | Operators | Question 9

Predict the output of the following program. 




class Test
{
    boolean[] array = new boolean[3];
    int count = 0;
   
    void set(boolean[] arr, int x)
    {
        arr[x] = true;
        count++;
    }
   
    void func()
    {
        if(array[0] && array[++count - 2] | array [count - 1])
            count++;
   
        System.out.println("count = " + count);
    }
   
   
    public static void main(String[] args)
    {
        Test object = new Test();
        object.set(object.array, 0);
        object.set(object.array, 1);
        object.func();
    }
}

(A)



2

(B)



3

(C)

4

Answer: (C)
Explanation:

First call to function set(), sets array[0] = true, array[1] = false and array[2] = false. Second call to function set(), sets array[0] = true, array[1] = true and array[2] = false. In function func(),if statement evaluates to be true. So, count = 4.

Quiz of this Question
Please comment below if you find anything wrong in the above post

Article Tags :