Java OCA OCP Practice Question 977

Question

Which one statement is true about the following code fragment?

1. import java.lang.Math; 
2. Math myMath = new Math(); 
3. System.out.println("cosine of 0.123 = " + 
4.          myMath.cos(0.123)); 
  • A. Compilation fails at line 2.
  • B. Compilation fails at line 3 or 4.
  • C. Compilation succeeds, although the import on line 1 is not necessary. During execution, an exception is thrown at line 3 or 4.
  • D. Compilation succeeds. The import on line 1 is necessary. During execution, an exception is thrown at line 3 or 4.
  • E. Compilation succeeds and no exception is thrown during execution.


A.

Note

The constructor for the Math class is private, so it cannot be called.

The Math class methods are static, so it is never necessary to construct an instance.

The import at line 1 is not required, because all classes of the java.lang package are automatically imported.




PreviousNext

Related