This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Grokking Bitcoin and have Kalle Rosenbaum on-line!
See this thread for details.
Win a copy of Grokking Bitcoin this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

Java ---== operato  RSS feed

 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
oops, I added an extra array index.
 
meena latha
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
million thanks to Bell and Daniel....
I got a very clear idea.....
 
He's my best friend. Not yours. Mine. You can have this tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!