aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Java ---== operato Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Java ---== operato" Watch "Java ---== operato" New topic
Author

Java ---== operato

meena latha
Ranch Hand

Joined: Jan 24, 2005
Posts: 219


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 ]
Daniel Simpson
Ranch Hand

Joined: Sep 02, 2004
Posts: 181
This might just be the most craziest code segment I have ever seen, but for reading's sake, let's add brackets to see what's happening:




Okay, now let's systematically go through this. First the for loop. 0 % any value will always be 0. So for the first iteration, when i = 0, it would be a new Object(), when i = 1, 1 % 2 = 1. So that is false, setting the reference of obj[1] = to obj[0]. The last iteration is i = 2. 2 % 2 == 0, which results in a new Object(); Okay, so we have:

1) obj[0] is a new Object, not null
2) obj[1] holds the same reference to obj[0], so that's not null either
3) obj[2] is a new Object, not null.

Now for the if statements.
1) obj[0] and obj[1] hold the same reference, that part is true. LOOK CLOSELY at the next part. You are setting obj[1]'s reference EQUAL to obj[2]. Remember, obj[2] is not null, this results in printing out "1".
2) Remember from the last if statement, obj[1] was set = to obj[2], that part is true. The next portion, obj[2] is set = to obj[0], and obj[0] isn't null, so that prints out "2".
3) obj[1] does not hold the same reference to obj[0], then you set obj[0] = to obj[1] and obj[1] does not equal null.
4)obj[0] and obj[2] hold the same reference and set it to itself so that is true resulting in printing "4".
Lastly--- obj[0] and obj[1] do not hold the same reference now, so that is false. obj[1] and obj[2] do not hold the same reference now, so that is false. obj[0] and obj[2] do hold the same reference now, so that is true.

Hope that helps! Hope that isn't too confusing!


SCJP 1.4<br />SCJD 1.4
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
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.
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
oops, I added an extra array index.
meena latha
Ranch Hand

Joined: Jan 24, 2005
Posts: 219
million thanks to Bell and Daniel....
I got a very clear idea.....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java ---== operato
 
Similar Threads
array...
mock01a Q41
Operators
Overriding equals() and hashCode() methods
confusion in if condition