Java OCA OCP Practice Question 3228

Question

Consider the following program:

import java.math.BigDecimal;

public class Main {
     public static void main(String []args) {
             Number [] numbers = new Number[4];
             numbers[0] = new Number(0);                  // NUM
             numbers[1] = new Integer(1);
             numbers[2] = new Float(2.0f);
             numbers[3] = new BigDecimal(3.0);                      // BIG
             for(Number num : numbers) {
                     System.out.print(num + " ");
             }//from  www.j a  v a  2  s.  c  om
     }
}

Which one of the following options correctly describes the behavior of this program?

  • a) Compiler error in line marked with comment NUM because Number cannot be instantiated.
  • b) Compiler error in line marked with comment BIG because BigDecimal does not inherit from Number.
  • c) When executed, this program prints the following: 0 1 2.0 3.
  • d) When executed, this program prints the following: 0.0 1.0 2.0 3.0.


a)

Note

Number is an abstract class, hence you cannot instantiate it using new operator.

Many classes including Integer, Float, and BigDecimal derive from the Number class.




PreviousNext

Related