Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Steven Elliott
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bert Bates
author
Sheriff
Posts: 8900
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
Steven Elliott
Greenhorn
Posts: 14
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bert Bates
author
Sheriff
Posts: 8900
5
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Steven Elliott
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
Joey Sanchez
Ranch Hand
Posts: 88
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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!
 
Mansi Agarwal
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!
 
Joey Sanchez
Ranch Hand
Posts: 88
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joey Sanchez
Ranch Hand
Posts: 88
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I found it

 
Yalvin Duha
Ranch Hand
Posts: 41
Eclipse IDE Java Slackware
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic