Java OCA OCP Practice Question 1814

Question

Most of the United States observes daylight savings time on March 12, 2017,

by moving the clocks forward an hour at 2 a.m.

What does the following code output?

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class Main {

   public static void main(String[] args) {
      LocalDate localDate = LocalDate.of(2017, 3, 12);
      LocalTime localTime = LocalTime.of(1, 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());
   }//ww  w.j a va2  s.com
}
  • A. 4
  • B. 5
  • C. 6
  • D. None of the above


B.

Note

On a normal night, adding three hours to 1 a.m. makes it 4 a.m.

However, this date begins daylight savings time.

This means we add an extra hour to skip the 2 a.m. hour.

This makes later contain 05:00 instead of 04:00.

The code prints 5, and Option B is correct.




PreviousNext

Related