by Manu (modified: 2013 Dec 26)

Last week I was at the LASER 2009 summer school on Software Testing. It was pretty interesting and one brief talk captured my attention. The speaker, Martin Nordio, asked the audience if they knew Eiffel and only a few hands were raised. So he asked if they knew Java instead. Everyone raised his hand. To show to the audience they were wrong in assuming they knew about Java, he showed 6 samples of source code written in Java and asked the audience what the program did and bet that no one would have the right 6 answers.

Let's do the same here. I'm going to give you the exercises and will post the answers in my next blog entry.

1. What is the value of b at the end of this code: foo () { int b = 1; b++; } ?

2. What is the value of b at the end of this code: foo () { int b = 1; while (true) { b++; break; } } ?

3. What is the value of b at the end of this code and tell us if this executes normally or with an exception: foo () { int b = 1; try { throw new Exception(); } finally { b++; } } ?

4. What is the value of b at the end of this code and tell us if this executes normally or with an exception: foo () { int b = 1; while (true) { try { b++; throw new Exception(); } finally { b++; break; } } b++; } ?

5. The previous sample was too long, what does this return: int foo () { try { return 1; } finally { return 2; } } ?

6. If you have answered properly to the above questions, then you are definitely quite an expert. To prove it, tell us the value of b at the end of the call as well as the return value: int b; int foo () { b = 0; try { b++; return b; } finally { b++; } } ?

PS: Feel free to post your answers as comments to this blog entry.