Java OCA OCP Practice Question 1427

Question

Following is a supposedly robust method to parse an input for a float:

public float parseFloat (String s){ 
   float f = 0.0f; 
   try{ /*  w ww . j ava 2  s.c o  m*/
      f = Float.valueOf (s).floatValue (); 
      return f ; 
    } 
   catch (NumberFormatException nfe){ 
      System.out.println ("Invalid input " + s); 
      f = Float.NaN ; 
      return f; 
    } 
   finally  { System.out.println ("finally");   } 
   return f ; 
} 

Which of the following statements about the above method are true?

Select 1 option

  • A. If input is "0.1" then it will return 0.1 and print finally.
  • B. If input is "0x.1" then it will return Float.Nan and print Invalid Input 0x .1 and finally.
  • C. If input is "1" then it will return 1.0 and print finally.
  • D. If input is "0x1" then it will return 0.0 and print Invalid Input 0x1 and finally.
  • E. The code will not compile.


Correct Option is  : E

Note

Note that the return statement after finally block is unreachable.

Otherwise, if this line were not there, choices 1, 2, 3 are valid.




PreviousNext

Related