Java OCA OCP Practice Question 1845

Question

The United States observes daylight savings time on March 12, 2020, by moving the clocks forward an hour at 2 a.m. What does the following code output?

LocalDate localDate = LocalDate.of(2020, 3, 12); 
LocalTime localTime = LocalTime.of(13, 0); 
ZoneId zone = ZoneId.of("America/New_York"); 
ZonedDateTime z = ZonedDateTime.of(localDate, localTime, zone); 
Duration duration = Duration.ofHours(3); 
ZonedDateTime later = z.plus(duration); 
System.out.println(later.getHour()); 
  • A. 13
  • B. 16
  • C. 17
  • D. None of the above


B.

Note

Adding three hours to 13:00 makes it 16:00.

While this date happens to be the start of daylight savings time, the change occurs at 2 a.m.

This code uses 13:00, which is 1 p.m.

Since the calculation does not cross 2 a.m., the fact that it is the date that starts daylight savings time is irrelevant.

Option B is correct because the hour is 16 and the time is 16:00.




PreviousNext

Related