Question 1
What is the output of the following program? ```java import java.text.NumberFormat; import java.util.Locale; public class Main { public static void main(String[] args) throws Exception { NumberFormat nf = NumberFormat.getInstance(Locale.US); System.out.println(nf.parse("40.45abc")); } } ```
A. 40.45Correct answer
parse consumes as many characters as form a valid number and stops at the first invalid one, so it reads 40.45 and silently ignores the trailing "abc".
B. 40
parse does not truncate to a whole number; it reads the entire valid numeric prefix including the decimal portion before it stops, yielding 40.45 rather than 40.
C. Compilation fails because parse does not throw a checked exception
parse does declare the checked ParseException, which is exactly why main carries the throws clause; the code compiles cleanly.
D. A ParseException is thrown because of the trailing letters
Trailing garbage after a valid number does not trigger an exception; ParseException fires only when the string does not even begin with a parseable number.
Explanation
NumberFormat.parse reads as far as it can and stops at the first character that cannot extend the number, so a valid numeric prefix is returned and any trailing text is simply ignored. An exception is raised only when the input does not begin with a parseable number at all. Because parse declares the checked ParseException, main must declare or handle it, and the program compiles and runs.