1: public class Question41 {
2:public static void main(
String[] args) {
3:Object[] obj = new Object[3];
4for (int i = 0; i < obj.length; i++)
5obj[i] = (i % 2 == 0) ? new Object() : obj[i - 1];
6if (obj[0] == obj[1] & (obj[1] = obj[2]) != null)
7System.out.print("1 ");
8if (obj[1] == obj[2] && (obj[2] = obj[0]) != null)
9System.out.print("2 ");
10if (obj[1] == obj[0] || (obj[0] = obj[1]) == null)
11System.out.print("3 ");
12if (obj[2] == obj[0] | (obj[0] = obj[2]) != null)
13System.out.print("4 ");
14System.out.println((obj[0] == obj[1]) + " " + (obj[1] == obj[2]) + " "
+ (obj[0] == obj[2]));
}
}
in line 4 and 5 all oblect will created
obj[0] and obj[1] refer to same object say obj1 in Heap
obj[2] refer to another object say obj2 in Heap
in line 6 both condition will be get evaluated even first is false it is both for & and |
here in line 6 obj[0] == obj[1] is true and now obj[1] will refer to obj2
in line 7 it will print 1
in line 8 obj[1] == obj[2] will be true and obj[2] will refer obj1
in line 9 will print 2
in line 10 obj[1] == obj[0] willbe false or obj[0] will refer obj2 and is not null so return false
as both are false 3 will not get printed
in line 12 obj[2] == obj[0] is true and for | operator it will evaluate the next condition that is
(obj[0] = obj[2]) != null) and obj[0] will refer obj1
in line 13 will print 4
so now obj[0] and obj[2] refering to obj1
and obj[1] refering to obj2
so
obj[0] == obj[1] will false
obj[1] == obj[2] will false
obj[0] == obj[2]) will true
So the answer is right , hey I know tis it little confusing but try to draw and solve it is not a tough at all