Java OCA OCP Practice Question 2278

Question

In most of the United States, daylight savings time ends on November 5, 2020 at 02:00 a.m., and we repeat that hour. What is the output of the following?

import java.time.*; 

public class Main { 

   public static void main(String[] args) { 
      LocalDate localDate = LocalDate.of(2020, Month.NOVEMBER, 5); 
      LocalTime localTime = LocalTime.of(1, 0); 
      ZoneId zone = ZoneId.of("America/New_York"); 
      ZonedDateTime z = ZonedDateTime.of(localDate, localTime, zone); 
      for (int i = 0; i < 6; i++) 
         z = z.plusHours(1); /*from   www .  j  a  v a  2 s.  co  m*/


      System.out.println(z.getHour()); 
   } 
} 
  • A. 5
  • B. 6
  • C. 7
  • D. The code does not compile.
  • E. The code compiles but throws an exception at runtime.


B.

Note

This code runs the loop six times, adding an hour to z each time.

However, the first time is the repeated hour from daylight savings time.

The time zone offset changes, but not the hour.

This means the hour only increments five times.

Adding that to 01:00 gives us 06:00 and makes Option B correct.




PreviousNext

Related