I've seen this question many times in the forums but no one can defintely answer why the answer is 'null null'.
Here is the code:

The command line invocation is:
java Miner diamond.

The choices are:
A.-null
B.-null null
C.-A ClassCastException is thrown
D.-A NullPointerException is thrown
E.- A NoClassDefFoundErrorr is thrown
F.-An ArithmeticException is thrown
G.-An IllegalArgumentException is thrown
H.-An ArrayIndexOutofBoundsException is thrown

The result is:
B-"null null".

Here are my questions:
1. Why is the answer 'null null'?
2. Shouldn't the answer be F.-An ArithmeticException is thrown becuase in the method getWeight(), you're dividing x by zero?
3. Why is there a 'diamond' in the command line line invocation 'java Miner diamond'? Is it there just to confuse you?

i mean't here y is assigned 0 so its a valid arithmetic assignment so no exceptional behavior involved. I guess i needed to rephrase my sentence. And yeah floating point numbers give a NaN which is Not a Number.

Thanks for the replies.
I still don't understand the 'math' logic difference btwn x/0 AND 0/x.
I'll just commit to memory that x/0 throws the exception.

Fritz Guerilus wrote:I still don't understand the 'math' logic difference btwn x/0 AND 0/x.
I'll just commit to memory that x/0 throws the exception.

0/x gives 0 in all circumstances (if x!=0 of course). 0 is a good result, so no exception is thrown ever for that .

But x/0 gives infinity, and when you divide integer numbers (say, 6 apples divide among 0 ppl) - it makes no sence. So Java throws an exception.
When you divide floating point numbers (x/0.0) you are getting result Infinity or NaN (if 0/0.0). You then can compare your result by Double.isInfinite(1/0.0) or Double.isNaN(0/0.0).
With floating point numbers it can make some sence as mathematical issue to have an infinity as a result of operations.

[SCJP 6.0]

Fritz Guerilus
Ranch Hand

Joined: Jun 20, 2009
Posts: 65

posted

0

Anastasia Sirotenko wrote:
0/x gives 0 in all circumstances (if x!=0 of course). 0 is a good result, so no exception is thrown ever for that .

But x/0 gives infinity, and when you divide integer numbers (say, 6 apples divide among 0 ppl) - it makes no sence. So Java throws an exception.
When you divide floating point numbers (x/0.0) you are getting result Infinity or NaN (if 0/0.0). You then can compare your result by Double.isInfinite(1/0.0) or Double.isNaN(0/0.0).
With floating point numbers it can make some sence as mathematical issue to have an infinity as a result of operations.

Thanks, that is great explaination, and makes perfect sense.

subject: K&B Master Exam SCJP6 class mineral question: why is the answer 'null null'