new java puzzlers (As presented in Javapolis –2007)

Just to flex your java muscles here are some java puzzlers — taken from the javapolis 2007 session by neal gafter and josh bloch

1) Set of Shorts (hehehe)

public class ShortSet {
public static void main(String[] args) {
SetshortSet=new HashSet();
for(short i=0;i<100;i++){
shortSet.add(i);
shortSet.remove(i-1);
}
System.out.println(“Size of Set =” + shortSet.size()) ;
}
}
The choices are
1)1
2)100
3) Throws an exception
4) None of the above

2)Racy little number

public class Test extends TestCase {
int number;
public void test()throws InterruptedException{
number=0;
Thread t=new Thread(new Runnable(){
public void run(){
assertEquals(2, number);
}
});
number=1;
t.start();
number++;
t.join();
}

}
What is the outcome of this test
1) Passes Sometimes
2)Fails Sometimes
3)Always passes
4) Always hangs

Advertisements

2 Responses to new java puzzlers (As presented in Javapolis –2007)

  1. Sanj says:

    1) Had to look up google to see that hashset removve only returns false and dosen’t throw an exception for an element not found, so answer to first one would be 1 because the last element 99 would never get removed.

    2) Made me wonder how junit works, and found an interesting link: how junit works.

    So the second question is a very fundamental java question: if a thread is started, and it throws an exception, does that get caught in the invoking thread?

    I think it should else where else would it end up?

    So my answer is that it always fails because the assertfailure error is going to be caught in the invoking thread.

  2. maneesh chaturvedi says:

    Ok time for the solutions. Honestly, i had experienced similar scenario’s hence although at that time, i was foxed, i knew the solutions when i looked at the puzzles.
    1) The answer for this one surprisingly is 100. Now for the rational. Although the Set contains Short values , one thing to keep in mind is that arithmetic operations on Shorts return Integers hence the calls to remove are actually being invoked on Integers. Now as Sanjay has pointed out, the remove returns false if the element is not found, all the calls to remove return false, hence only the add operations are contributing to the size of the Set, hence 100 is what the size of the Set remains. Another thing of interest is this implies that the remove operation in the Set interface is not generified, it still deals with the raw Object type. I could explain why, but as pointed out, remove returns false if there is no such object in the Set, hence the solution should be obvious.
    2)This one is a little freaky. The way JUnit works is by catching exceptions which are thrown by the asserts. Now the important thing to remember here is that the exception thrown in this case is never caught by JUnit since it runs in a separate thread. Now this test is essentially like a test which has no assertions. Another point of interest is the number itself. Since the variable is non volatile, what value its going to have is something which we wont know.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: