Java Interview Puzzler
June 18th, 2009 by hashbrownThis is a puzzler we created at my work to use as an interview problem. I had a fun time taking it myself, and got it wrong the first time! It was designed to mimic some bad legacy code you might encounter when working on a large aging system, that has passed through many hands. Terrible coding practices, no javadocs or comments, etc. You are tasked to figure out what the heck it does.
First – does it compile? If not, how do you fix it?
Second – If it compiles, what is the output?
I’ll answer questions and post the solution in the comments.
package crazy; public class Timing extends R { public static final Integer t = new Integer("4"); public static void main(String[] args) { Timing t = new Timing(11); System.out.print(t.foo); bat(t); } void fob() { fod(); } public Timing(int value) { super(); foo = foo % value; bar(); } static { System.out.print("6"); } private void bar() { new Baz().bar(5); tr = "2"; } private int foo = 25; static void r() { System.out.println("1"); } } abstract class R { String tr = "9"; static void bat(R r) { r.fob(); System.out.print(r.tr); } private int negate(int bam) { return bam * -1; } public R() { System.out.print("7"); } void fod() { System.out.print((int)Math.floor(.99)); } protected final int t() { return 5; } class Baz { void bar(int fob) { if (fob == 9) System.out.print(String.valueOf(fob++)); System.out.print(String.valueOf(negate(fob++))); if (fob == 5) System.out.print(String.valueOf(fob+=2)); } } static { System.out.print("8"); } abstract void fob(); }
Posted in geek stuff, programming | Comments Off on Java Interview Puzzler