Skip to main content

A Few Questions To Test Yourself

Posted by paranoiabla on February 1, 2007 at 2:27 AM PST

Hello everybody,

recently I was sent a few interesting questions by my friend Angel Evrov, but being busy enough I had no
time to look at them. Anyway, I finally found a few spare hours, and the problems turned out to be very
interesting, so here they come.

Question I:
Consider the following Java class:

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, Java Guru!" 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

Solution I:

The solution I immediately got was the one that is most common. As we know static blocks are executed before
the main method, so the only thing we have to make sure is that we insert a static block in our class and also ensure that
we never execute the main method. Here is the solution:

public class MyDraft {

static {
System.out.println("Hello World JavaGuru!");

public static void main(String[] args) {
System.out.println("Hello World!");

Solution II:

This is the solution of Radoslav Nikolov, who I would like to thank for pointing it to me:
public class MagicOutput {

    static String HELLO_WORLD = "Hello, World!";

    static char[] HELLO_GURU_CHARS = "Hello, Java Guru!".toCharArray();

    static {

        try {

            Field f;

            f = String.class.getDeclaredField("value");


            f.set(HELLO_WORLD, HELLO_GURU_CHARS);

            f = String.class.getDeclaredField("count");


            f.setInt(HELLO_WORLD, HELLO_GURU_CHARS.length);

        } catch (Exception e) {




    public static void main(final String[] args) {

        System.out.println("Hello, World!");



The idea here is basically this: we again use the static block to start the solution before the jvm executes
the main method, but this time we declare the "real" string and the "fake" string as instance variables of the class.
Then we use a simple reflection trick to change the value of the "real" string with the "fake" one, and later when
executing the main method the changed string value is displayed. Simple and clean :-).

Question II:


public String makinStrings() {

String s = "Fred";

s = s + "47";

s = s.substring(2, 5);

s = s.toUpperCase();

return s.toString();

How many String objects will be created when this method is invoked?


One of the most interesting things is that the real ansewr is 3. Because the string objects
that correspond to values of "Fred" and "47" already exist in the pool
and four of this methods return new objects(the .toString() method does not produce a new object) so i think that there are
2 in the pool and 3 in the heap.

Question III:
Consider the following code:

public static int testMethod() {
for(;;) {

Does the code compile?

Answer: Interesting enough, but the code compiles. Now a next question arises: "Why does the code copmpile?".
Is this a bug in the java compiler? Or maybe a feature, considering the fact that the for cycle never ends? And if
it is a feature, shouldn't it mark a warning?

I would like to hear your comments. What do you think?

Related Topics >>