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?
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?
The given code creates a LocalDate object representing February 14, 2015, then increments it by one year to get a new LocalDate object representing February 14, 2016. The method call nextYear.plusDays(15) generates a new object but doesn't reassign the new object back to a variable, because LocalDate is immutable. Therefore, nextYear remains February 14, 2016, and the output is 2016-02-14.
Answer is A because of immutable nature, to get C it should be: nextYear = nextYear.plusDays(15);
Answer is A .. LocalDate is immutable
A is the correct answer
A is correct. Date and time classes are immutable so nextYear.plusDays(15); //line n1 is ignored since it is not assigned to variable.
Answer is A.