Java OCA OCP Practice Question 2206

Question

What is a possible output of the following?

LocalDate trainDay = LocalDate.of(2017, 5, 13); 
LocalTime time = LocalTime.of(10, 0); 
ZoneId zone = ZoneId.of("America/Los_Angeles"); 
ZonedDateTime zdt = ZonedDateTime.of(trainDay, time, zone); 
Instant instant = zdt.toInstant(); 
instant = instant.plus(1, ChronoUnit.YEARS); 
System.out.println(instant); 
  • A. 2017-05-13T10:00-07:00[America/Los_Angeles]
  • B. 2017-05-13T17:00:00Z
  • C. 2018-05-13T10:00-07:00[America/Los_Angeles]
  • D. 2018-05-13T17:00:00Z
  • E. The code does not compile.
  • F. The code compiles but throws an exception at runtime.


F.

Note

While an Instant represents a specific moment in time using GMT, Java only allows adding or removing units of DAYS or smaller.

This code throws an UnsupportedTemporalTypeException because of the attempt to add YEARS.

Therefore, Option F is correct.




PreviousNext

Related