Java OCA OCP Practice Question 2179

Question

Fill in the blank so this code prints -1.

LocalDate xmas = LocalDate.of(2020,  12, 25); 
LocalDate blackFriday = LocalDate.of(2020, 11, 24); 
long monthsLeft = ChronoUnit.MONTHS.____; 
System.out.println(monthsLeft); 
A.    between(blackFriday, xmas)
B.    between(xmas, blackFriday)
C.    minus(blackFriday, xmas)
D.    minus(xmas, blackFriday)
E.    plus(blackFriday, xmas)
F.    plus(xmas, blackFriday)


B.

Note

There are not minus() or plus() methods on ChronoUnit making Options C, D, E and F incorrect.

Both Options A and B compile; however, they differ in the output.

Option A prints 1 because you can add 1 to get from November to December.

Option B prints -1 because the first date is larger, and therefore Option B is the correct answer.




PreviousNext

Related