Java OCA OCP Practice Question 1863

Question

What is the output of the following?

LocalDate date1 = LocalDate.of(2020, Month.MARCH, 3); 
LocalDate date2 = date1.plusDays(2).minusDays(1).minusDays(1); 
System.out.println(date1.equals(date2)); 
  • A. false
  • B. true
  • C. The code does not compile.
  • D. The code compiles but throws an exception at runtime.


B.

Note

The first line of code correctly creates a LocalDate object representing March 3, 2020.

The second line adds two days to it, making it March 5.

It then subtracts a day, making it March 4.

Finally, it subtracts yet another day ending at March 3.

The outcome of all this is that we have two dates that have the same value, and Option B is correct.




PreviousNext

Related