Java OCA OCP Practice Question 2073

Question

Which statement is true about the program? Assume that the decimal sign is a dot(.) and the grouping character is a comma ( ,) for the US locale.

public class Main {
  public static void main(String[] args) {
    // (1) DECLARATION INSERTED HERE ...
    System.out.println(parseNumber(inputStr));
  }/*  w  w  w.  j a v  a  2  s.c  o  m*/

  public static Number parseNumber(String inputString) {
    NumberFormat nfUS = NumberFormat.getNumberInstance(Locale.US);
    Double num = nfUS.parse(inputString);
    return num;
  }
}

    

Select the one correct answer.


(a)  The  following  declaration,  when  inserted  at  (1),  will  result  in  the  program
     compiling without errors and executing normally:

     String inputStr = "1234.567";

(b)  The  following  declaration,  when  inserted  at  (1),  will  result  in  the  program
     compiling without errors and executing normally:

     String inputStr = "0.567";

(c)  The  following  declaration,  when  inserted  at  (1),  will  result  in  the  program
     compiling without errors and executing normally:

     String inputStr = "1234..";

(d)  The  following  declaration,  when  inserted  at  (1),  will  result  in  the  program
     compiling without errors and executing normally:

     String inputStr = "1,234.567";

(e)  The  following  declaration,  when  inserted  at  (1),  will  result  in  the  program
     compiling without errors and executing normally:

      String inputStr = "1 234.567";

(f)  Regardless of which declaration from (a) to (e) is inserted for the input reference at (1), the program will not compile.

(g)  Regardless of which declaration from (a) to (e) is inserted for the input reference at (1), the program will compile, but result in an exception at runtime.


(f)

Note

(f) The method parseNumber() does not catch the ParseException that can be thrown by the parse() method.

The parse() method returns a Number, which is not assignment compatible to a reference of type Double.

If these errors are corrected, the alternatives from (a) to (e) give the following outputs, respectively:

1234.567
0.567
1234
1234.567
1



PreviousNext

Related