1

Found this website to presumably test wannabe developers...

http://www.devchallenge.co.uk/challenge-2

The question is this...

Based on the given code, which of the following tests will return a ‘true’ answer and pass, and which will return a ‘false’ answer and fail?

ArrayList array1 = new ArrayList();
ArrayList array2 = new ArrayList();

array1.add(1);
array1.add(2);
array1.add("Aviva");

array2.add(1);
array2.add(2.0);
array2.add("Aviva");

Asserts
   Equality
      (array1[0],array2[0]);

Asserts
   Equality
      (array1[1],array2[1]);

Asserts
   Equality
      (array1[2],array2[2]);

Apparently the answer is 'Fail', 'Fail', 'Pass'.

I'm not a Java developer - and I am presuming this challenge is in Java (though it isn't stated).

What exactly is Equality doing? Is it checking for the same object or the same value? I know that some objects are interned into the String/Integer pool in Java and so I can understand why the last one is true. But why is the first one not true?

El Ronnoco
  • 11,753
  • 5
  • 38
  • 65
  • These asserts are pseudo-code so there's no definite answer. But I'm quite sure "equality" means "the result of calling `equals`". – Marko Topolnik Apr 30 '12 at 09:34
  • @Marko Yes that's what I thought - and so therefore would you expect to see `Equality( array1[0], array2[0] )` produce true? As their values are equal... – El Ronnoco Apr 30 '12 at 09:38
  • 1
    Yes. I went to that site, it looks like some crap. If they don't give specific, compilable code, they can claim any answer is correct. – Marko Topolnik Apr 30 '12 at 09:41
  • This should not be tagged with Java. This is not Java code. In Java ArrayList entries cannot be retrieved by a []-operator. You'd need to use array1.get(0), for example. Also, there cannot be whitespace within method names, so `Asserts Equality` is not valid Java. Lastly, the answer would be different if it was java. If it was java and you'd use the standard junit.framework.Assert.assertEquals, the answers would be `Pass, Fail, Pass`. – Alderath May 02 '12 at 13:32
  • @Alderath Well I know that this company is in the business of employing Java devs and so I thought this must be Java code - but it isn't stated. I just wanted some Java-brains to give me their opinion. I'm a C# / JS dev really - I have very limited Java knowledge. Thanks for the comment, sorry for misleading you!! – El Ronnoco May 02 '12 at 13:55
  • @ElRonnoco I am sorry if I came across as being rude. It was not my intention. But I realize looking back at my comment that it sounds a bit harsh. – Alderath May 02 '12 at 13:59
  • @Alderath Not at all! I've encountered much ruder :) But I didn't think you were being rude anyway... I understand your point. I put the tag there to 'fish' for Java people. Thanks for your comments. – El Ronnoco May 02 '12 at 15:40

2 Answers2

1

This is not valid Java syntax. You cannot call Asserts Equality ().

As an assert in a JUnit test this has to be Assert.assertEquals(array[0], array2[0]) which would cause comparing two Integers. So this should pass.

So I don't understand your proposed results of that code also. I would say pass, fail, pass is right.

Kai
  • 38,985
  • 14
  • 88
  • 103
  • 1
    @Miquel: You don't have primitive types here. The values are auto-boxes into an `Integer` and `Double` because a Collection can only hold objects. That's why the assert looks like this: `Assert.assertEquals(Integer.valueOf(1), Double.valueOf(1.0));` (which returns false). – Kai May 06 '12 at 07:25
1

If the scalars are being "auto-boxed" then they will have different object holders, so a tests of == will be false, but the strings will pass the == test since the compiler makes sure the same exact string as a constant is used. If you are considering an .equals() test, then they will all be equal.

Francis Upton IV
  • 19,322
  • 3
  • 53
  • 57
  • I suppose this could be C# (like), it has had autoboxing since the beginning I think. – Francis Upton IV Apr 30 '12 at 09:46
  • In your opinion is the question clear enough for someone with good Java knowledge to answer accurately? – El Ronnoco Apr 30 '12 at 10:02
  • Also, will the integers not be interned into the integer pool and so `==` would yield `true` anyway? – El Ronnoco Apr 30 '12 at 10:03
  • In Java only strings are interned (dynamically), integer values would not be. And I have pretty good Java knowledge and was able to work it out, in the sense I could explain what they there talking about, but I would not say it was that clear. – Francis Upton IV Apr 30 '12 at 10:05
  • I would have thought that an Equality assertion would use `.equals` or some such *value* type comparison though... Thanks for your answer. – El Ronnoco Apr 30 '12 at 10:11
  • I agree with you, which is why it makes it confusing. I was only offering an explanation that fit the facts. – Francis Upton IV Apr 30 '12 at 10:18
  • @ElRonnoco Only small integers are cached. So, for the values in the given example, `array1.get(0) == array2.get(0)` would return true, because the value (1) is small and has a cached Integer object. If e.g. 1000 was stored as the value instead of 1, then the == comparison would return false, because no Integer object would be cached. (This is all assuming that we are using Java) – Alderath May 02 '12 at 13:40