Skip to content
Related Articles

Related Articles

Java | Operators | Question 9
  • Difficulty Level : Expert
  • Last Updated : 12 Jan, 2016
GeeksforGeeks - Summer Carnival Banner

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :