Popular Posts

Wednesday, September 21, 2011

Java Puzzles: Expressive Puzzlers


public class Tweedledee {
    public static void main(String[] args) {
        // Put your declarations for x and i here

        x = x + i;  // Must be LEGAL
        x += i;     // Must be ILLEGAL
    }
}


public class Elementary {
    public static void main(String[] args) {
        System.out.println(12345 + 5432l);
    }
}


public class JoyOfHex {
    public static void main(String[] args) {
        System.out.println(
            Long.toHexString(0x100000000L + 0xcafebabe));
    }
}


public class Multicast {
    public static void main(String[] args) {
        System.out.println((int) (char) (byte) -1);
    }
}


public class CleverSwap {
    public static void main(String[] args) {
        int x = 1984;
        int y = 2001;
        x ^= y ^= x ^= y;
        System.out.println("x = " + x + "; y = " + y);
    }
}


public class DosEquis {
    public static void main(String[] args) {
        char x = 'X';
        int i = 0;
        System.out.print(true  ? x : 0);
        System.out.print(false ? i : x); 
    }
}


public class Tweedledum {
    public static void main(String[] args) {
        // Put your declarations for x and i here

        x += i;     // Must be LEGAL
        x = x + i;  // Must be ILLEGAL
    }
}


No comments: