nerdexam
Oracle

1Z0-809 · Question #147

Given the code fragment: public static void main(String[] args) { LocalDate date = LocalDate.of(2012, 01, 32); date.plusDays(10); System.out.println(date); } What is the result?

The correct answer is C. A DateTimeException is thrown at runtime. LocalDate.of(2012, 01, 32) throws a DateTimeException at runtime because January has only 31 days - day 32 is invalid, and LocalDate validates field values eagerly when the object is created. The exception fires on line 2, so execution never reaches plusDays or println. Why the…

Question

Given the code fragment: public static void main(String[] args) { LocalDate date = LocalDate.of(2012, 01, 32); date.plusDays(10); System.out.println(date); } What is the result?

Options

  • A2012-02-10
  • BCompilation fails.
  • CA DateTimeException is thrown at runtime.
  • D2012-02-11

How the community answered

(24 responses)
  • A
    4% (1)
  • B
    13% (3)
  • C
    79% (19)
  • D
    4% (1)

Explanation

LocalDate.of(2012, 01, 32) throws a DateTimeException at runtime because January has only 31 days - day 32 is invalid, and LocalDate validates field values eagerly when the object is created. The exception fires on line 2, so execution never reaches plusDays or println.

Why the distractors are wrong:

  • B (Compilation fails): LocalDate.of(int, int, int) accepts any int arguments; the compiler has no way to validate calendar logic at compile time, so this compiles fine.
  • A and D (date calculations): Both assume the object was constructed successfully. Since the exception is thrown during construction, no date arithmetic ever runs. Note also that plusDays returns a new LocalDate and doesn't mutate date (LocalDate is immutable), so even if construction succeeded, date itself would be unchanged - making D wrong on two counts.

Memory tip: Think of LocalDate.of() as a strict bouncer - it checks your values at the door (object creation), not later. If any field is out of range, it throws immediately, before you can do anything else with the object.

Community Discussion

No community discussion yet for this question.

Full 1Z0-809 Practice