Java OCA OCP Practice Question 2831

Question

Given that Calendar.MONTH starts with January == 0, and given:

import java.util.Calendar;

public class Main {
   public static void main(String[] args) {
      Calendar c = Calendar.getInstance();
      c.set(1999, 11, 25);//  w  w w.  j  a va 2  s . c  om
      c.roll(Calendar.MONTH, 3);
      c.add(Calendar.DATE, 10);
      System.out.println(c.getTime());
   }
}

And, if the program compiles, what date is represented in the output?

  • A. March 4, 1999
  • B. April 4, 1999
  • C. March 4, 2000
  • D. April 4, 2000
  • E. Compilation fails.
  • F. An exception is thrown at runtime.


B is correct.

Note

It's important to remember that roll() changes the Calendar field specified WITHOUT incrementing a date's bigger time chunks.

The add() method changes the field's value AND also increments the bigger time chunks (like adding a new day or month), when appropriate.

Note that Calendar instances are created using a factory method.




PreviousNext

Related