Originally posted by Ramya JP:
Can anybody explain the answer for me
Answer is 1 2 4 false false true
(PLEASE use tags)
[ January 24, 2005: Message edited by: Barry Gaunt ]
I numbered the lines to help me explain.
1) is a for loop that initializes the array.
2) this creates two Objects. The first one is stored in obj[0]. That same one is stored in obj[1] (the ? being a trinary operator and on 1 and 3, i%2==0, is false) A second Object is created and stored in obj[2] and that same one is stored in obj[3] so the array looks like:
obj[0] = firstObject;
obj[1] = firstObject;
obj[2] = secondObject;
obj[3] = secondObject;
3) line three tests if obj[0] and obj[1] are the same object. that is true. Then it assigns obj[2] to obj[1] and checks if obj[1] is not null which is true. The array now looks like:
obj[0] = firstObject;
obj[1] = secondObject;
obj[2] = secondObject;
obj[3] = secondObject;
4) because both conditions in 3 were true '1' is printed.
5) the same as 3, but it checks if obj[1] and obj[2] are the same object (true) and then assigns obj[0] to obj[2]. Array is now:
obj[0] = firstObject;
obj[1] = secondObject;
obj[2] = firstObject;
obj[3] = secondObject;
6) prints '2'
7) here the
test obj[1] == obj[0] fails, from here I'd have to look up what the #0124 is. Hopefully somebody can finish this off, but I think you have some idea now.