Java OCA OCP Practice Question 1828

Question

Consider the following method -

public float parseFloat ( String s ){
   float f = 0.0f;
   try{//from   w  w w . j  av a2s . c o m
      f = Float.valueOf ( s ).floatValue ();
      return f ;
   }
   catch (NumberFormatException nfe){
      f = Float.NaN ;
      return f;
   }
   finally{
      f = 10.0f;
      return f;
   }
}

What will it return if the method is called with the input "0.0" ?

Select 1 option

  • A. It will not compile.
  • B. It will return 10.0
  • C. It will return Float.Nan
  • D. It will return 0.0
  • E. None of the above.


Correct Option is  : B

Note

finally block will always execute (except when there is a System.exit() in try or catch).

And inside the finally block, it is setting f to 10.0.

So no matter what the input is, this method will always return 10.0.




PreviousNext

Related