Programming Syntax Brain Teasers |
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://grok-code.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 = 0×000A;
-
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.
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 //grok-code.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 = 0×000A;
-
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.
Related |
|





Hi Jesse,
Number 2 just hurts to look at
Like dealing with a poor sentence, sometimes it is best to just rewrite the whole thing. This would probably be clearer if a switch() statement was used.
Nice Monday-morning brain teaser — thanks!
Tom
Yeah, 2 is pretty ugly. I once worked on some code structured like this, but nested 6 or 7 levels deep and pages long. It was controlling some AI for a NPC in a little demo game. As we tried to debug it, we added continue statements (the whole thing was in a loop) everywhere and the AI just got dumber and dumber…I’m not sure if we actually found the bug before the deadline.
That was back in University when we didn’t know any better. It would have been better to just rewrite it, but our sleep deprived brains just didn’t hit on that idea.
Hmm! Is this not REBOL code?
Just kidding….
An unquoted URL is a URL! datatype in rebol.
Great post, thanks for the info