Programming Syntax Brain Teasers

by Jess Johnson in Programming Languages

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円%six012円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.

# October 5, 2008 5 Comments

Discussion on
Programming Syntax Brain Teasers

by Jess Johnson in Programming Languages

5 Comments

  1. gravatar
    Tom Gerhard
    7:43 am UTC, 2008年10月06日
    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
  2. gravatar
    Jess Johnson
    9:31 am UTC, 2008年10月06日
    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.
  3. gravatar
    Uncle Tim
    8:04 pm UTC, 2008年11月11日
    Hmm! Is this not REBOL code? Just kidding.... An unquoted URL is a URL! datatype in rebol.
  4. gravatar
    Small Business Ireland
    1:25 pm UTC, 2009年03月12日
    Great post, thanks for the info
  5. gravatar
    Dustin Dorroh
    7:26 am UTC, 2011年05月04日
    Here is a real brain teaser! This program is written in three languages (ie. polyglot). It has Shell, Make, and C syntax. The one line of C in the main just executes executes Make, thus calling it's self. Based off of the implicit variables used in the Make syntax it can guess it's name which after the linking it does to itself. There is even more possibility for one to some add assembly commands in the preprocessor macro portion. #include #include #define MAKE_C_SHELLS TRUE TRUE /*:all CC=cc PROG=polyglot false : make -f 0ドル 1ドル exit 0 all: $(PROG) $(PROG).c: ln -T Makefile $(PROG).c %:%.c $(CC) $< -o $@ clean: rm $(PROG) clean-src: rm $(PROG).c .PHONY: /* true clean */ #undef true int main(int argc, char ** argv) {execv("/usr/bin/make",argv);return argc;} Dustin Dorroh

Leave a reply

AltStyle によって変換されたページ (->オリジナル) /