1Z0-809 · Question #134
Given the code fragment: public static void main(String[] args) { String date = LocalDate.parse("2014-05-04", DateTimeFormatter.ISO_DATE_TIME).toString(); System.out.println(date); } What is the…
The correct answer is A. An exception is thrown at runtime. Option A is correct because DateTimeFormatter.ISO_DATE_TIME expects a string containing both a date and time component (e.g., "2014-05-04T10:15:30"), but the input "2014-05-04" has no time portion - this mismatch causes a DateTimeParseException to be thrown at runtime before…
Question
Options
- AAn exception is thrown at runtime.
- B2014-05-04T00:00:00
- CMay 04, 2014T00:00:00.000
- D5/4/14T00:00:00.000
How the community answered
(46 responses)- A80% (37)
- B11% (5)
- C7% (3)
- D2% (1)
Explanation
Option A is correct because DateTimeFormatter.ISO_DATE_TIME expects a string containing both a date and time component (e.g., "2014-05-04T10:15:30"), but the input "2014-05-04" has no time portion - this mismatch causes a DateTimeParseException to be thrown at runtime before println is ever reached.
Options B, C, and D are wrong for two compounding reasons: the code never produces output because it crashes on the parse() call, and even if parsing somehow succeeded, LocalDate has no time component, so its toString() would only ever produce "2014-05-04" - never a T00:00:00 suffix.
Memory tip: Match the formatter to the type - LocalDate pairs with ISO_LOCAL_DATE, LocalDateTime pairs with ISO_LOCAL_DATE_TIME, and ISO_DATE_TIME requires a time portion. When you see a type/formatter mismatch on the exam, think "runtime exception."
Community Discussion
No community discussion yet for this question.