Java OCA OCP Practice Question 2879

Question

Given the proper imports, and given:

24.  Date d1 = new Date();  
25.  Date d2 = d1;  
26.  System.out.println(d1);  
27.  d2.setTime(d1.getTime() + (7 * 24 * 60 * 60));  
28.  System.out.println(d2); 

Which are true? (Choose all that apply.)

  • A. Compilation fails.
  • B. An exception is thrown at runtime.
  • C. Some of the output will be today's date.
  • D. Some of the output will be next week's date.
  • E. Some of the output will represent the Java "epoch" (i.e., January 1, 1970).


C is correct.

Note

When a Date is constructed with no arguments, the system's current time is used, not the Java "epoch.

" The code is correct, and the second date displayed is about 10 minutes after the first date because Date objects and setTime() both use millisecond precision.

In order to add a week, we would have also had to multiply by 1000 in line 27.




PreviousNext

Related