Java OCA OCP Practice Question 1827

Question

What is a possible result of the following?

LocalDate montyPythonDay = LocalDate.of(2017, Month.MAY, 10); 
LocalDate aprilFools = LocalDate.of(2018,  Month.APRIL, 1); 
Duration duration = Duration.ofDays(1); 
LocalDate result = montyPythonDay.minus(duration); 
System.out.println(result + " " + aprilFools.isBefore(result)); 
  • A. 2017-05-09 false
  • B. 2017-05-09 true
  • C. The code does not compile.
  • D. None of the above


D.

Note

Duration is supposed to be used with objects that contain times.

While it has an ofDays() method, this is a convenience method to represent a large number of seconds.

This means that calling Duration.ofDays(1) is fine.

This code throws an UnsupportedTemporalTypeException when you try to pass it the minus() method on LocalDate, making Option D correct.




PreviousNext

Related