aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Trick or Treat? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Trick or Treat?" Watch "Trick or Treat?" New topic
Author

Trick or Treat?

Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi ranchers,

special question for today (Java 5):

What happens if you try to compile and run the following code today?


today: see when posted




A output: Trick
B output: Treat
C compiles but does not give an output
D throws an Exception
E does not compile









Yours,
Bu.

---
Don't serialize cats tonight.


all events occur in real time
Gareth Western
Ranch Hand

Joined: Apr 07, 2004
Posts: 45
trick?
Sreekanth Chinthalapalli
Greenhorn

Joined: Oct 30, 2006
Posts: 3
It prints Trick. The main Idea of the question is to check Throwable is instanceof Serializable interface or not.
Naveen Zed
Ranch Hand

Joined: Aug 11, 2006
Posts: 61
I think it will print Nothing.Because Calender.MONTH value is 2 always and not equal to Calender.OCTOBER which is 9,So the method isHalloween(Calendar cal) will always return false ,So the result.

If I am wrong suggest me.
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi Naveen,

indeed you're wrong
the line of code was:
if (cal.get(Calendar.MONTH) != Calendar.OCTOBER)
and the method called, get has the signature

int java.util.Calendar( int field)

and if you don't want to look up in the Calendar API what integer now actually stands for the field "month", use a constant instead.
The method does not return the number of the constant, but what is written in the field "month".

And I compared it also not with an integer (would be nine for october, as the months start with zero. Also the Maya used zero based dates by the way).

And as I don't want to confuse somebody, I'd rather use the constant Calendar.OCTOBER than the integer 9 directly. Sooner or later somebody will think, Halloween is in September then.


To all the others:
At least nobody stepped into the trap:"Hey! He's instantianting Throwable! Just by the name ending in "-able" you can guess it is an interface!"

Output yesterday: Trick
today, no output.

class Throwable is Serializable. So you can serialize a RuntimeException, store it to disk and send it by mail to everyone who doesn't have one.
;-)

Bu.
 
jQuery in Action, 2nd edition
 
subject: Trick or Treat?