Additional Blogs by Members
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Former Member
0 Kudos

I guess a week is a long enough time-frame for a small quiz. So today I've decided to post list of the winners and correct answers to my Micro Quiz Series: The funniest Java quiz questions. questions. Unfortunately, no one provided correct answer to second question, so I reordered questions a bit, pay attention to numbering.

Question 1: Magic Output (20 points)

Consider the following Java class:

package com.sap.sdn.jquiz; public class MagicOutput { public static void main(final String argv[]) { System.out.println("Hello, World!"); } }

 
While preserving both method main signature (name, number and type of parameters, return type) and implementation (body of method), make this program to output to standard console message "Hello, SDN!" Again, you may not alter a single character within the method main!!! The resulted class must be executable with JRE 1.4; you may not use classes besides public API of JDK 1.4

Obviously, you have to solve 2 problems here.

First, you need some code executed either instead of method main or before method main. This problem has one simple solution: place static initializer in class. Any static initializer is executed when class is loaded, so JVM will first execute code in static block and call method main afterwards.

The second task is to hide output from method main. Well, there are many ways to skin a cat. Here is what SDN-ers suggested:

a. Exit application

The simplest and the most efficient solution! Just call System.exit() inside of static initializer and JVM will terminate program without invoking method main at all:

package com.sap.sdn.jquiz; public class MagicOutput { static { System.out.println("Hello, SDN!"); System.exit(0); } public static void main(final String argv[]) { System.out.println("Hello, World!"); } }

 
The solution was submitted by Rich Heilman, Pran Bhas and Guru Subramanian .

b. Redirect output

Funny, only a day after I had announced the quiz, YiNing Mao posted his blog entry Handle Standard JAVA Output Using Log File. This is exactly the answer I have myself! So I invited him to participate, and he provided this option (a bit corrected by me just to not touch file system):

package com.sap.sdn.jquiz; public class MagicOutput { static { System.out.println("Hello, SDN!"); System.setOut( new java.io.PrintStream( new java.io.ByteArrayOutputStream() ) ); } public static void main(final String argv[]) { System.out.println("Hello, World!"); } }

 
Right after printing "Hello, SDN!" we are redirecting system output stream to in-memory stream and all output in method main goes south.

c. Closing console output stream

I never expect that this will work without errors and stack traces. The code closes stream and then writes to it. There are should be errors! But there is no one! Java has specific implementation of java.io.PrintStream for console output: it collects I/O errors but not throws exception to outer code! Thanks to Maksim Rashchynski for exploring this:

package com.sap.sdn.jquiz; public class MagicOutput { static { System.out.println("Hello, SDN!"); System.out.close(); } public static void main(final String argv[]) { System.out.println("Hello, World!"); } }

Question 3: Green Java (60 points)

You are probably noticed, that popular Java IDE-s like Eclipse-based (Eclipse itself and NetWeaver IDE for example), NetBeans and IntelliJ IDEA tend to display commented out code in green. Ok, IDEA starting from version 5.0 prefers to use grey by default. Anyway, the question is: write a classical program that outputs "Hello, SDN World!" on console but its source is shown by IDE totally in green. Every line and every character is green. Or grey ๐Ÿ˜‰ Like commented out code.

The key to solution is the fact that you may use any Unicode symbols in Java source. You may even create complete application in your own language, even if it is Chinese or Russian or Greek! Obviously, there are could a problem with source file encodings (I mean native OS-specific single-byte encodings), so Java allows to use escaped Unicode character sequences anywhere in code (not only in string/character literals).

For example, you may write the following:

u0063u006Cu0061u0073u0073 Abc{}

 
...and this would be equivalent to

class Abc{}

 
So the solution was to play with comments characters and Unicode character codes to get IDE code highlighter shocked. It is possible to escape closing slash character in mutliline comments:

/* *u002F package com.sap.sdn.jquiz; public class GreenJava { public static void main(final String argv[]) { System.out.println("Hello, SDN World!"); } }

 
...as it was suggested by Igor Nys; or just escape carriage return characters with single line comments:

//U000Dpackage com.sap.sdn.jquiz; //U000D //U000Dpublic class GreenJava { //U000D public static void main(final String argv[]) { //U000D System.out.println("Hello, SDN World!"); //U000D } //U000D}

 
...as in answer from Maksim Rashchynski

Try to copy code above in your NetWeaver IDE (or any other mentioned). You should see commented out code. But it runs and shows output. Moreover, in Eclipse (and hence in NetWeaver) even code completion works!

Question 2: As short as possible but not shorter (40 points)

You have the following Java class:

package com.sap.sdn.jquiz; public class Minimalizmo { //START //END }

 
Between START and END comments, add the shortest (in number of characters) possible method with return type other then void. Resulted class must be syntactically correct, i.e. it must compiles successfully on JDK1.4; you may not use classes besides public API of JDK 1.4. That was a fortress! I get no correct answers to this code even I provided several hints in comments.

First, minimal signature of method means that we must use package private visibility, i.e. no visibility modifier in source code. Method name is one character. Shorter simply impossible ๐Ÿ™‚ Next, we need a return type that eats as small number of characters as possible. Among standard API classes and primitives you hardly find anything shorter then 3 characters. So we use int. Now it goes to method body. The first attempt is to write the following:

int m(){return 0;}

 
Ok, that takes 18 characters in total. However, I've commented that a) method must be only syntactically correct and b) there are other ways to make compiler happy besides return statement:

int m(){throw new RuntimeException();}

 
Oops! Even longer then before! However, this is just an example of the fact that method with non-void return type may have no return statement at all. Still curious? Look at the answer:

package com.sap.sdn.jquiz; public class Minimalizmo { //STARTint m(){for(;;);} //END }

 
17 characters! I will eat my hat if someone find shorter ๐Ÿ™‚

If you still do not understand what happens in method body, then here is an explanation. This is just infinite for loop. First semicolon means empty initializer, second one means empty condition (hence infinite loop). The final semicolon is... guess what... method body. The Java compiler is smart enough to understand flow inside method body, namely, that flow started with loop never ends so it is ok to omit return statement.

Below is list of the quiz winners. Congratulations to everyone who submitted correct answers! I will notify Craig immediately, so points are on the go!

Rich Heilman, 20 points (Q1)
Pran Bhas, 20 points (Q1)
Guru Subramanian, 20 points (Q1)
YiNing Mao, 20 points (Q1)
Maksim Rashchynski, 80 points (Q1 + Q3)
Igor Nys, 60 points (Q3)

1 Comment