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(); } } |
chevron_right
filter_none
(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
Recommended Posts:
- Java | Operators | Question 3
- Java | Operators | Question 2
- Java | Operators | Question 8
- Java | Operators | Question 1
- Java | Operators | Question 6
- Java | Operators | Question 5
- Java | Operators | Question 4
- Java | Operators | Question 7
- Operators in Java
- Bitwise operators in Java
- Bitwise right shift operators in Java
- Compound assignment operators in Java
- Operators in C | Set 2 (Relational and Logical Operators)
- C | Operators | Question 12
- C | Operators | Question 13



