• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 196
Android Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
NaN only when: 0/0.0
Infinity when: (something!=0)/0.0
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I AM MIGHTY! Especially when I hold this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic