The Java Quiz

I found such a thread in another forum and it might be fun. Also because i guessed wrong in a few examples:

What is the result of following function (Trying it out is like cheating):

[java]
private static int call()
{
try
{
return i++;
}
finally
{
return ++i;
}

}

[/java]

1 Like

An NPE… i isn’t defined.

And… hello again all. Been bed bound for a few days =(

Oh, damn, thats when you write code without testing. It should be

[java]
private static int call()
{
int i=0;
try
{
return i++;
}
finally
{
return ++i;
}

}

[/java]

I don’t understand why it would be anything but 2.

My guess was, since:
[java]
int i=0;
return i++;
[/java]

returns 0 of course, the finally would get executed instead of the return command, but before the increment command. i was wrong

Maybe if you do
[java]
int i = 0;
return ++i;
[/java]

You will get 1.

Well since its a postincrement, you return before it will be incremented or?
however in case of the finally it would be a preincrement, so it would be before or?

i++
…is the same as…
push i
i = i + 1
pop value

So return i++
push i
i = i + 1
pop value
return value

…thus i is already 1 before the finally block runs. It can’t be otherwise as the whole return expression must be 100% evaluated before you return.

As another thought experiment illustrating the exact same principle I described above…

    int i = 0;
    int j = i++ + ++i;

What do you suppose j will be?

@zzuegg where’s this thread? it sounds interesting :slight_smile:

@pspeed said: i++ ...is the same as... push i i = i + 1 pop value

So return i++
push i
i = i + 1
pop value
return value

…thus i is already 1 before the finally block runs. It can’t be otherwise as the whole return expression must be 100% evaluated before you return.

Actually i think you are wrong,

return 0++ //returns 0

since the increment gets executed after all other operations in that expression. That was the part that made me belive the above code would return 1.

As stack:

return i++;

is like:

push i
i=i+1
return i
pop i

//Edit: actually it probably is:
return i
push i
i=i+1
pop i

but then i don’t see why finally executes the expressions after the return

Something else:
[java]
public static void main(String args[]){
??? x = ???
if(x!=x){
System.out.println(“OK”);
}
}

[/java]

replace the ??? so that OK gets printed

From java puzzlers, a book I strongly recommend buying if you like this.

http://www.amazon.de/Java-Puzzlers-Traps-Pitfalls-Corner/dp/032133678X

Who can guess what will happen if I try to use this?
[java]
/**

  • Generated by the IBM IDL-to-Java compiler, version 1.0
  • from F:\TestRoot\apps\a1\units\include\PolicyHome.idl
  • Wednesday, June 17, 1998 6:44:40 o’clock AM GMT+00:00
    */
    public class Test {
    public static void main(String[] args) {
    System.out.print(“Hell”);
    System.out.println(“o world”);
    }
    }
    [/java]
@zzuegg said: Actually i think you are wrong,

return 0++ //returns 0

since the increment gets executed after all other operations in that expression. That was the part that made me belive the above code would return 1.

As stack:

return i++;

is like:

push i
i=i+1
return i
pop i

//Edit: actually it probably is:
return i
push i
i=i+1
pop i

but then i don’t see why finally executes the expressions after the return

You are confused.

I wrote:
return i++
is like:
push i
i = i + 1;
pop value
return value

So,
push 0
i = 0 + 1
pop 0
return 0

Return is LAST… after everything. You have to have done everything before returning because no code is executed after the turn.

The return causes the finally block to execute and since you have a different return it is not returning a value based on the NEW value of i… because that code already ran before the return. For a block, return is always last. Nothing can run after it because control flow has already gone elsewhere.

1 Like
@zzuegg said: Something else: [java] public static void main(String args[]){ ??? x = ??? if(x!=x){ System.out.println("OK"); } }

[/java]

replace the ??? so that OK gets printed

I’m not going to try it and I’m going to assume that the ??? can be two different things (unclear but the alternative is impossible)…

but I think the answer is:
float x = Float.NaN;

1 Like

@Empire, for some reason I cannot quote your post… but that one is too easy so I will let someone else do it. :slight_smile:

You are right about both

My fault

@zzuegg said: You are right about both

My fault

No worries… it helps to have written simple compilers before. :slight_smile: Things seem much less magic for these sorts of problems.

The Java brain twisters that deal with threading + statics + classloading, etc. all in combination often give me fits.

What is the output?

[java]
public class CompareTest
{
public static void main(String args[])
{
float a = 0;
float b = 0;
a += 0.7;
b += 0.5;
if (a < 0.7) {
if (b < 0.5){
System.out.println(“Both smaller”);
}else{
System.out.println(“One smaller”);
}
}else{
System.out.println(“None smaller”);
}
}
}

[/java]

And a bonus point if you can say why…

I suspect the trap is where a+=0.7 instead of a+= 0.7f, but since we not allowed to test I’ll let it there :D.