Java OCA OCP Practice Question 297

Question

Given:

 3. import java.text.*;
 4. public class Main {
 5.   public static void main(String[] args) {
 6.     String s = "987.123456";
 7.     double d  = 987.123456d;
 8.     NumberFormat nf = NumberFormat.getInstance();
 9.     nf.setMaximumFractionDigits(5);//w ww.  j ava2s .c  om
10.     System.out.println(nf.format(d) + " ");
11.     try {
12.       System.out.println(nf.parse(s));
13.     } catch (Exception e) { System.out.println("got exc"); }
14.   }
15. }

Which are true?

Choose all that apply.

  • A. The output is 987.12345 987.12345
  • B. The output is 987.12346 987.12345
  • C. The output is 987.12345 987.123456
  • D. The output is 987.12346 987.123456
  • E. The try/catch block is unnecessary
  • F. The code compiles and runs without exception
  • G. The invocation of parse() must be placed within a try/catch block


D, F, and G are correct.

Note

The setMaximumFractionDigits() applies to the formatting, but not the parsing.

The try/catch block is placed appropriately.

This one might scare you into thinking that you'll need to memorize more than you really do.

If you can remember that you're formatting the number and parsing the string, you should be fine for the exam.




PreviousNext

Related