|
The Java Specialists' Newsletter
Issue 083 2003-12-26
Category:
Language
Java version: End of Year Puzzleby Dr. Heinz M. Kabutz
Welcome to the 83rd edition of The Java(tm) Specialists' Newsletter. The year 2003 is now
almost over, and before we say "goodbye" completely, I
thought to slip in one more quick newsletter to test your
Java skills :-)
Translations: We started converting The Java(tm) Specialists' Newsletter into some other
languages besides English [although someone suggested that we
should translate it into English as well ;-] We now have
translations into Portuguese (Rafael Steil and Vanessa
Sabino), Zulu (Mondli Mabaso), Polish (Daniel Kwiecinski),
Romanian (Dikran Seropian), Catalan (Horaci Macias), Spanish
(Horaci Macias) and German (Heinz Kabutz). The translations
are available on our archive page.
End of Year Puzzle
This time of year is called the silly season in South
Africa. In Cape Town, as soon as December starts, and all
the holiday makers arrive, the road works spring up all over
the city. This exacerbates the traffic situation and
gridlocks the roads. Maybe it takes 11 months to decide on
the contractor, and then the work has to be finished before
the end of the year?
So, seeing that it is "silly season", here is a little Java
puzzle for your amusement, based on an idea that was sent to
me by Steve Mabbort and Ben Halton. It is almost like a
question you would find in the Sun Certified Java Programmer
examination...
What is the output of the following Java snippet?
public class Puzzle1 {
public int test() {
int i = 1;
try {
return i;
} finally {
i = 2;
}
}
public static void main(String[] args) {
Puzzle1 p = new Puzzle1();
System.out.println(p.test());
}
}
There are several possibilities, so let's make a multiple
choice out of it. To make it interesting, why don't you
click on the [Select] link next to the answer that you would
think to be correct? I will then tally the answers and post
the result in the next newsletter.
- The code does not compile
- The code compiles, but throws an exception
- The code prints "0"
- The code prints "1"
- The code prints "2"
- None of the above
- All of the above
After you have sent me your answer, you should try it
out and see whether you were correct.
Let's change the puzzle a bit. Now it is not so obvious
anymore.
public class Puzzle2 {
private int i = 1;
public int test () {
try {
return i;
} finally {
i = 2;
}
}
public static void main(String[] args) {
Puzzle2 p = new Puzzle2();
System.out.println(p.test());
}
}
- The code does not compile
- The code compiles, but throws an exception
- The code prints "0"
- The code prints "1"
- The code prints "2"
- None of the above
- All of the above
To understand how and why this is so, I would like to suggest
that you disassemble the java classes and read the byte code.
You can do that with the command:
javap -c Puzzle1 Puzzle2
That's all for this year, see you again in 2004 :-) I wish
you a prosperous 2004, both physically and spiritually :-)
Kind regards
Heinz
Language Articles
Related Java Course
Discuss at The Java Specialist Club
|