Java OCA OCP Practice Question 1829

Question

What is the result of running this code?

12:  LocalDate pieDay = LocalDate.of(2017, Month.JANUARY, 23); 
13:  LocalTime midnight = LocalTime.of(0, 0); 
14:  LocalDateTime pieTime = LocalDateTime.of(pieDay, midnight); 
15: 
16:  DateTimeFormatter f = DateTimeFormatter 
17:     .ofLocalizedDate(FormatStyle.SHORT); 
18:  f.format(pieDay); 
19:  f.format(pieTime); 
20:  f.format(midnight); 
  • A. The code runs successfully.
  • B. The code throws an exception on line 19.
  • C. The code throws an exception on line 20.
  • D. The code does not compile.


C.

Note

The DateTimeFormatter is created with ofLocalizedDate().

It knows how to format date fields but not time fields.

Line 18 is fine because a LocalDate clearly has date fields.

Line 19 is also fine.

Since a LocalDateTime has both date and time fields, the formatter just looks at the date fields.

Line 20 is a problem.

A LocalTime object does not have any date fields so the formatter throws an UnsupportedTemporalTypeException, making Option C the answer.




PreviousNext

Related