Java SE 8 Programmer II — Question 10
Given the code fragment:
LocalDate valentinesDay =LocalDate.of(2015, Month.FEBRUARY, 14);
LocalDate nextYear = valentinesDay.plusYears(1);
nextYear.plusDays(15); //line n1
System.out.println(nextYear);
What is the result?
Answer options
- A. 2016-02-14 DateTimeException is thrown.
- B. A
- C. 2016-02-29 line n1.
- D. A compilation error occurs at
Correct answer: A
Explanation
The code attempts to add 15 days to the date of February 14, 2016, which results in February 29, 2016. However, since the 'nextYear' variable is not updated with this new date after adding days, the print statement outputs the initial value of 'nextYear', which is still February 14, 2016. Since the code is executed without errors or exceptions, the correct answer indicates that the DateTimeException is thrown.