Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Java | Operators | Question 9

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Predict the output of the following program. 

Java




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

My Personal Notes arrow_drop_up
Last Updated : 28 Jun, 2021
Like Article
Save Article
Similar Reads