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
Options
- A2012-02-10
- BCompilation fails.
- CA DateTimeException is thrown at runtime.
- D2012-02-11
How the community answered
(24 responses)- A4% (1)
- B13% (3)
- C79% (19)
- D4% (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 anyintarguments; 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
plusDaysreturns a newLocalDateand doesn't mutatedate(LocalDate is immutable), so even if construction succeeded,dateitself 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.