Java OCA OCP Practice Question 2251

Question

Choose the correct option based on this code segment:.

DateTimeFormatter dateFormat = DateTimeFormatter.ISO_DATE;      // DEF
LocalDate dateOfBirth = LocalDate.of(2015, Month.FEBRUARY, 31);
System.out.println(dateFormat.format(dateOfBirth));             // USE
  • A. the program gives a compiler error in the line marked with the comment DEF
  • B. the program gives a compiler error in the line marked with the comment USE
  • C. the code segment prints: 2015-02-31
  • D. the code segment prints: 2015-02-03
  • e. this code segment throws java.time.DateTimeException with the message "Invalid date 'FEBRUARY 31'"


e.

Note

the date value 31 passed in the call LocalDate.of(2015, 2, 31); is invalid for the month February, and hence the of() method in the LocalDate class throws DateTimeException.

One of the predefined values in DateTimeFormatter is ISO_DATE.

hence, it does not result in a compiler error for the statement marked with the comment DEF.

the statement marked with the comment USE compiles without errors because it is the correct way to use the format() method in the DateTimeFormatter class.




PreviousNext

Related