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

Fritz Guerilus

Ranch Hand

Posts: 65

posted 6 years ago

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?

Thanks in advance

-Fritz

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?

Thanks in advance

-Fritz

SCJP 6.0

Sunny X Narula

Greenhorn

Posts: 22

posted 6 years ago

"Sunny Narula X " please check your private messages. You can check them by clicking the My Private Messages link above.

SCJP, SCWCD.

|Asking Good Questions|

posted 6 years ago

NaN only when: 0/0.0

Infinity when: (something!=0)/0.0

Infinity when: (something!=0)/0.0

SCJP6, SCWCD5, OCE:EJBD6.

BLOG: http://leakfromjavaheap.blogspot.com

posted 6 years ago

zero divided by any number is zero. This once confused me too in a question as I also thought that there will be an exception ...

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links

Fritz Guerilus

Ranch Hand

Posts: 65

Anastasia Sirotenko

Ranch Hand

Posts: 64

posted 6 years ago

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.

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

Posts: 65

posted 6 years ago

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

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.

SCJP 6.0