Exam Prep Practice Questions

Question 1You need a method to take a double primitive value and return the square root, ignoring the sign of the input. Which one of these options is correct?
  • A.

    double mySqrt( double val ){
          return Math.sqrt( val );
    }
    
  • B.

    double mySqrt( double val ){
          return Math.sqrt( Math.abs( val ) ) ;
       }
    
  • C.

    double mySqrt( double val ){
          Math myM = new Math();
          return myM.sqrt( myM.abs( val )) ;
       }
    
  • D.

    public double mySqrt(double val){
      return Math.sqrt(Math.round(val));
    
      }
    
A1: Answer B is correct. It uses the Math static methods to take the absolute value of the input before passing it to the square root function. Answer A is incorrect because it does not take into account the possibility of a negative input. Answer C is incorrect because ...

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.