Java OCA OCP Practice Question 739

Question

What is the result of the following?

import java.time.*; 
import java.time.format.*; 
? 
public class Main { 
   public static void main(String[] args) { 
      LocalDate newYears = LocalDate.of(2020, 1, 1); 
      Period period = Period.ofDays(1); 
      DateTimeFormatter format = DateTimeFormatter.ofPattern("MM-dd-yyyy"); 
      System.out.print(format.format(newYears.minus(period))); 
   } //w w  w  . java  2 s. c  o  m
} 
  • A. 01-01-2020
  • B. 12-31-2019
  • C. The code does not compile.
  • D. The code compiles but throws an exception at runtime.


B.

Note

The code starts by correctly creating a date representing January 1, 2020, and a period representing one day.

It then explicitly defines the format as month followed by day followed by year.

The code subtracts a day, giving us the formatted version of December 31, 2019.




PreviousNext

Related