Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML


 

Search Host

Monthly Fee($)
Disk Space (MB)
Register With us for Newsletter!
Visit Forum! Post Questions!
Jobs At RoseIndia.net!

Have tutorials?
Add your tutorial to our Java Resource and get tons of hits.

We offer free hosting for your tutorials. and exposure for thousands of readers. drop a mail
roseindia_net@yahoo.com
 
   

Tutorials

Java Server Pages

JAXB

Java Beans

JDBC

MySQL

Java Servlets

Struts

Bioinformatics

Java Code Examples

Interview Questions

 
Join For Newsletter

Powered by groups.yahoo.com
Visit Group! Post Questions!

Web Promotion

Web Submission

Submit Sites

Manual Submission?

Web Promotion Guide

Hosting Companies

Web Hosting Guide

Web Hosting

Linux

Beginner Guide to Linux Server

Frameworks

Persistence Framework

Web Frameworks

Free EAI Tools

Web Servers

Aspect Oriented Programming

Free Proxy Servers

Softwares

Adware & Spyware Remover

Open Source Softwares

End of Year Puzzle

       

2003-12-26 The Java Specialists' Newsletter [Issue 083] - End of Year Puzzle

Author: Dr. Heinz M. Kabutz

If you are reading this, and have not subscribed, please consider doing it now by going to our subscribe page. You can subscribe either via email or RSS.


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 :-)

Some good news: Sun has made available a beta version of JDK 1.5. This has been negotiated by my friends over at JavaLobby, and all you have to do is register for free at their website. The URL to go to is http://www.javalobby.org/members/j2se15.jsp.

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.

  1. The code does not compile [Select]
  2. The code compiles, but throws an exception [Select]
  3. The code prints "0" [Select]
  4. The code prints "1" [Select]
  5. The code prints "2" [Select]
  6. None of the above [Select]
  7. All of the above [Select]

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());
  }
}
  
  1. The code does not compile [Select]
  2. The code compiles, but throws an exception [Select]
  3. The code prints "0" [Select]
  4. The code prints "1" [Select]
  5. The code prints "2" [Select]
  6. None of the above [Select]
  7. All of the above [Select]

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


This material from The Java(tm) Specialists' Newsletter by Maximum Solutions (South Africa). Please contact Maximum Solutions for more information.

       

Useful Links
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright 2007. All rights reserved.