Java OCA OCP Practice Question 2690

Question

Consider the following program and predict the output:

import java.text.DateFormat;
import java.util.*;

class Main {/* ww  w  .  j av a2s . c  om*/
        public static void main(String[] args) {
                DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.US);
                Calendar c = Calendar.getInstance();
                c.set(Calendar.YEAR, 2012);
                c.set(Calendar.MONTH, 12);
                c.set(Calendar.DAY_OF_MONTH, 1);
                System.out.println(df.format(c.getTime()));
        }
}
  • a) The program will produce a runtime exception.
  • b) The program will produce a compiler error.
  • c) It will print the following: December 1, 2012.
  • d) It will print the following: January 1, 2013.


d)

Note

The month index starts from 0; thus, if you give the month an index of 12, it will increase the year by one and start counting from January.




PreviousNext

Related