Forums Register Login

nit w/ chap.6 q.13 S&B

+Pie Number of slices to send: Send
Somehow this ended up on Java Porch forum. Reposting here where I thought it I had posted it originally.

Sierra & Bates SCJP 6 chap. 6, question 13 is:


One of the correct answers is:
G. The invocation of parse() must be placed within a try/catch block

My nit is you can declare to throw an Exception in the main method so "...must be placed within a try/catch block." is not true because it is not the only option. I would be upset if I had missed this by not marking this as a correct answer because the way it is worded.
+Pie Number of slices to send: Send
Hi Steven,

It could be a valid nit or not

The best way to know for sure is to post the entire question, with all the answers and explanations.

Thanks,

Bert
+Pie Number of slices to send: Send
Dear Steven,

Please post the full question and other options as well. In my opinion, this is the correct answer may be because parse() method
throws a checked exception. As main is the last thread where such things can be handled, it is better to catch it and handle it. Else
it will blow up your program. So it is better that we don't say something like



HTH,
2
+Pie Number of slices to send: Send
Here is the complete question 13 from Chap. 6 S&B

@Prithvi: I agree that throwing an Exception in main() is not usual nevertheless doing so allows the program to compile and run as normally as you would expect. Moreover had this been any other method there would be no question that declaring the Exception would be a valid option (rule "handle-or-declare") as is the case for all checked exceptions.
2
+Pie Number of slices to send: Send
Hi Steven,

I agree that instead of saying "try/catch" we should have said something like "handled or declared". We'll note that in our list of fixes - thanks.

As far as the real exam goes, remember that you'll ALWAYS be told how many correct answers there are, so in the event that you spot something like this (which I don't think you will), knowing the number of correct answers would help you out.

Thanks,

Bert
+Pie Number of slices to send: Send
Hey thank you Bert. Your book has been a great help and as you note knowing the number of correct answers will be a clue as to "which is/are the most correct".
+Pie Number of slices to send: Send


I don't understand why the first print line is 987.12346, what happened with the number 5?

Could someone explain this?

Thanks in advance!
+Pie Number of slices to send: Send
Yes Joey!!

Actually we are setting the Maximum Fraction Digit to 5 so it is will print just 5 digits after the decimal.
To do so it round off the last digits..

I hope you understand!!
+Pie Number of slices to send: Send
Hi Mansi Agarwal,
As you say, the result would be 987.12345 instead of 987.12346 with five digits after de dot.
How does it work the round off(¿if x>5 then +1 the previous digit? else?)? Does it appear in the api?


Method definition:
setMaximumFractionDigits

public void setMaximumFractionDigits(int newValue)
Sets the maximum number of digits allowed in the fraction portion of a number. maximumFractionDigits must be >= minimumFractionDigits. If the new value for maximumFractionDigits is less than the current value of minimumFractionDigits, then minimumFractionDigits will also be set to the new value.
Parameters:
newValue - the maximum number of fraction digits to be shown; if less than zero, then zero is used. The concrete subclass may enforce an upper limit to this value appropriate to the numeric type being formatted.

 
+Pie Number of slices to send: Send
I think I found it

+Pie Number of slices to send: Send
 

Joey Sanchez wrote:

I don't understand why the first print line is 987.12346, what happened with the number 5?

Could someone explain this?

Thanks in advance!


I touched on this before but no one has given a definitive answer as to which methods we are supposed to watch for when it comes to rounding off.
Squanch that. And squanch this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1349 times.
Similar Threads
NumberFormat
nit w/ chap.6 q.13 S&B
catch{} blocks
Exception Question
SCJP Study Exam Chapter 5 Question 15
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 23:16:14.