Java SE 8 Programmer II — Question 83
Given the code fragment:
LocalDate valentinesDay =LocalDate.of(2015, Month.FEBRUARY, 14);
LocalDate next15days = valentinesDay.plusDays (15);
LocalDate nextYear = next15days.plusYears(1); // line n1
System.out.println(nextYear);
What is the result?
Answer options
- A. 2016-03-01
- B. A DateTimeException is thrown.
- C. 2016-02-29
- D. A compilation error occurs at line n1.
Correct answer: D
Explanation
The correct answer is D because the code attempts to add one year to a date that does not exist (March 1, 2016, is not a leap year). As a result, this leads to a compilation error at line n1. The other options are incorrect as they either suggest valid dates or exceptions that do not occur.