Running on Java 24-ea+24-2960 (Preview)
Home of The JavaSpecialists' Newsletter

083bEnd of Year Puzzle (Follow-Up)

Author: Dr. Heinz M. KabutzDate: 2003-12-26Java Version: 1.1Category: Language
 

Abstract: Another small puzzle to test your knowledge of Java.

 

Hi again,

A great number of subscribers are on holiday at the moment, but to those that are not, thanks for sending your answer to the puzzle. Approximately 70% of respondents had the correct answer, which either means that the puzzle was way too easy, or that the correct answer was also the most obvious one.

As a short follow-up, here is a variation of the puzzle for your amusement.

What is the output of the following Java snippet?

public class Puzzle3 {
  public int test() {
    int i = 0;
    try {
      i++;
      return i;
    } finally {
      i--;
      return i;
    }
  }
  public static void main(String[] args) {
    Puzzle3 p = new Puzzle3();
    System.out.println(p.test());
  }
}

And here is the multiple choice again:

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

I expect the split between correct/incorrect to be similar to the previous "puzzles", since the question is so similar.

Kind regards

Heinz

 

Comments

We are always happy to receive comments from our readers. Feel free to send me a comment via email or discuss the newsletter in our JavaSpecialists Slack Channel (Get an invite here)

When you load these comments, you'll be connected to Disqus. Privacy Statement.

Related Articles

Browse the Newsletter Archive

About the Author

Heinz Kabutz Java Conference Speaker

Java Champion, author of the Javaspecialists Newsletter, conference speaking regular... About Heinz

Superpack '23

Superpack '23 Our entire Java Specialists Training in one huge bundle more...

Free Java Book

Dynamic Proxies in Java Book
Java Training

We deliver relevant courses, by top Java developers to produce more resourceful and efficient programmers within their organisations.

Java Consulting

We can help make your Java application run faster and trouble-shoot concurrency and performance bugs...