Friday, May 30, 2008

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

The emperor and me beaching

The Devil next door

Kaiser The Emperor