Java OCA OCP Practice Question 841

Question

What will the following code snippet print:


public class Main {
   public static void main(String[] args) throws Exception {
      Float f = null;/*from   w  ww  .j  a  v  a  2s.co  m*/
      try {
         f = Float.valueOf("12.3");
         String s = f.toString();
         int i = Integer.parseInt(s);
         System.out.println("i = " + i);
      } catch (Exception e) {
         System.out.println("trouble  : " + f);
      }
   }
}

Select 1 option

  • A. 12
  • B. 13
  • C. trouble : null
  • D. trouble : 12.3
  • E. trouble : 0.0


Correct Option is  : D

Note

f = Float.valueOf ("12.3"); executes without any problem.

int i = Integer.parseInt (s); throws a NumberFormatException because 12.3 is not an integer.

The catch block prints trouble : 12.3




PreviousNext

Related