aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K&B Master Exam SCJP6 class mineral question: why is the answer 'null null' Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "K&B Master Exam SCJP6 class mineral question: why is the answer Watch "K&B Master Exam SCJP6 class mineral question: why is the answer New topic
Author

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

Fritz Guerilus
Ranch Hand

Joined: Jun 20, 2009
Posts: 65
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


SCJP 6.0
Sunny X Narula
Greenhorn

Joined: Dec 07, 2008
Posts: 22
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

Joined: Jun 06, 2007
Posts: 2712
    
    5

"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|
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

Why should it be an Exception?y = 0 is a valid arithmetic number assignment. There is no exceptional behavior involved here.


[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
karthick chinnathambi
Ranch Hand

Joined: Jul 06, 2009
Posts: 196

Nitish Bangera wrote:Why should it be an Exception? 0 is a valid arithmetic number. There is no exceptional behavior involved here.




KARTHICK.C , SCJP6-93%
(Born to Win)
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

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

Joined: Apr 20, 2009
Posts: 804
    
    1

NaN only when: 0/0.0
Infinity when: (something!=0)/0.0


SCJP6, SCWCD5, OCE:EJBD6.
BLOG: http://leakfromjavaheap.blogspot.com
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

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

Joined: Jun 20, 2009
Posts: 65
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

Joined: Jul 20, 2009
Posts: 64
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
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 agree. Here's the link: http://aspose.com/file-tools
 
subject: K&B Master Exam SCJP6 class mineral question: why is the answer 'null null'
 
Similar Threads
K&B Self Test Chap 5 Question 4 - arguments
Question from K&B Chapter 5
object reference
Why null null?
KB question pg 409