nerdexam
Oracle

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

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 result?

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)
  • A
    80% (37)
  • B
    11% (5)
  • C
    7% (3)
  • D
    2% (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.

Full 1Z0-809 Practice