Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Fritz Guerilus
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sunny X Narula
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code says 0/x not x/0 so its not an arithmetic exception

The answer should be:
null<Space><Newline>
null<Space><Newline>

not "null null" since this is a println

And yes the diamond is just to distract the miner
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Sunny Narula X " please check your private messages. You can check them by clicking the My Private Messages link above.
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why should it be an Exception?y = 0 is a valid arithmetic number assignment. There is no exceptional behavior involved here.
 
karthick chinnathambi
Ranch Hand
Posts: 196
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nitish Bangera wrote:Why should it be an Exception? 0 is a valid arithmetic number. There is no exceptional behavior involved here.


 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int y=0/x;


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.
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NaN only when: 0/0.0
Infinity when: (something!=0)/0.0
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ...
 
Fritz Guerilus
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Anastasia Sirotenko
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Fritz Guerilus
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic