Exam Prep Practice Questions

Question 1 The following method is designed to convert an input string to a floating-point number while detecting a bad format (assume factor is a float variable defined elsewhere):
1. public boolean strCvt( String s ){
2.   try {
3.     factor =
          Float.valueOf( s ).floatValue();
4.     return true ;
5.   }catch(NumberFormatException e){
6.    System.out.println("Bad number " + s);
7.     factor = Float.NaN ;
8.   }finally { System.out.println("Finally");
9.   }
10.  return false ;
11. }

Which descriptions of the results of various inputs to the method are correct? [Check all correct answers.]

  • A. Input = “0.234”—Result: factor = 0.234, “Finally” is printed, true is returned.

  • B. Input = “0.234”—Result: factor = 0.234, “Finally” is printed, ...

Get Java 2™ Programmer Exam Cram™ 2 (Exam CX-310-035) now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.