This is a collection of 4 programming brain teasers in C and Java. Some require a sudden flash of insight or knowledge of good coding style to solve, others demand intimate knowledge of the compilation process. The problems range from easy to insanely tricky. The C brain teasers come from The C Puzzle Book and the Obfuscated C Contest. The Java problems are from the Java Puzzlers book. Answers to all problems are at the bottom of the page.

1. Magic Java URLs

Why does the following Java code compile?

public class Oddity {
    public static void main(String[] args) {
        http://grokcode.com
        System.out.println("Why is the URL allowed above?");
    }
}

source: New Adventures in Software

2. WTF Coding Style In C

Improve the following C code fragment. The new fragment should be easier to grok and use principles of good coding style.

if (A)
    if(B)
        if(C) D;
        else;
    else;
else
    if (B)
        if(C) E;
        else F;
    else;

source: The C Puzzle Book

3. Tricky Linefeeds in Java

On Windows a line separator is a CR followed by an LF. On *nix it is an LF character. What does the following code print on both platforms?

public class LinePrinter {
    public static void main(String[] args) {
        // Note: \u000A is Unicode representation of linefeed (LF)
        char c = 0x000A;
        System.out.println(c);
    }
}

source: Java Puzzlers

4. The Impossible C One Liner

What does the following C code print? This one of the best one line submissions ever entered in the Obfuscated C Contest. It was written by David Korn, and won the Best One Liner in 1987. Very few people can determine the output by visual inspection. It should be compiled on Unix or Cygwin.

main() { printf(&unix["\021%six\012\0"],(unix)["have"]+"fun"-0x60);}

source: Obfuscated C Code Contest


Answers

1. Magic Java URLs

The syntax highlighting might have made this one pretty clear. The http: is parsed as a label, which is followed by the comment //grokcode.com, so the compiler won’t choke on the code snippet.

2. WTF Coding Style In C

Deeply nested statements are against the principles of code good design. The code should be rewritten to collapse the nesting of the if statements. Here is one possible solution that fully qualifies the conditions required to execute each statement:

if (A && B && C) D;
else if (!A && B && C) E;
else if (!A && B && !C) F;

Here is another good solution that uses some nesting in order to shorten the conditions in the if statements:

if (B) {
    if (A && C) D;
    else if (!A && C) E;
    else if (!A && !C) F;
}

3. Tricky Linefeeds in Java

OK this was a bit of a trick question. The code snippet won’t even compile. The problem is that the compiler translates Unicode escapes into their equivalents before parsing the code into tokens, and before stripping comments and whitespace. So the program is translated into this:

public class LinePrinter {
    public static void main(String[] args) {
        // Note:
 is Unicode representation of linefeed (LF)
        char c = 0x000A;
        System.out.println(c);
    }
}

Which we can see isn’t going to compile.

So what if the comment is removed? Is the result platform dependent? It turns out that on *nix two complete line separators are printed, and on Windows only one is printed.

4. The Impossible C One Liner

Did you get this one? If so you did better than the vast majority of C programmers. It prints “unix”. But why? Here is an explanation by David Ireland. He uses 109 lines to explain 1 line of code. Thats obfuscation for you.