Analyze the following code using The java.util.Calendar and java.util.GregorianCalendar classes :

Analyze the following code using The java.util.Calendar and java.util.GregorianCalendar classes :

1. import java.util.*;
2. public class Test {
3.   public static void main(String[] args) {
4.     Calendar[] calendars = new Calendar[10];
5.     calendars[0] = new Calendar();
6.     calendars[1] = new GregorianCalendar();
7.   }
8. }


A. The program has a compile error on Line 4 because java.util.Calendar is an abstract class.
B. The program has a compile error on Line 5 because java.util.Calendar is an abstract class.
C. The program has a compile error on Line 6 because Calendar[1] is not of a GregorianCalendar type.
D. The program has no compile errors.

The correct answer is B

Explanation: (A) is incorrect since it is OK to use abstract class as data type for arrays. new Calendar[10] does not create Calendar objects. It just creates an array with 10 elements, each of which can reference to a Calendar object. (B) is correct since you cannot create an object from an abstract class. (C) is incorrect since it is fine to create a GregorianCalendar object and assign its reference to a variable of its superclass type.


Java

Learn More Multiple Choice Question :