• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java ---== operato

 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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 ]
 
Ranch Hand
Posts: 181
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oops, I added an extra array index.
 
meena latha
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
million thanks to Bell and Daniel....
I got a very clear idea.....
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic