Oracle Certified Professional Java Se 11 Developer · Free Practice Question Easy
Question 93
Question ID: UKOCP45559
Given code:
- package com.udayankhattry.ocp;
- import java.time.*;
- import java.time.format.DateTimeFormatter;
- public class Test {
- public static void main(String [] args) {
- var date = LocalDate.of(2023, Month.FEBRUARY, 1);
- var formatter = DateTimeFormatter.ofPattern("DD'nd day of' uuuu");
- System.out.println(formatter.format(date));
- }
- }
What is the result?
- A Runtime Exception
- B 01nd day of 2023
- C 02nd day of 2023
- D 32nd day of 2023
Reveal correct answer
Correct answer: D
Explanation
UKOCP45559:
Variable 'date' infers to LocalDate type and variable 'formatter' infers to DateTimeFormatter type.
In the format string, anything between opening and closing quote [''] is displayed as it is.
DD -> is for representing single digit number as 2 digits (4 as 04).
'nd day of' -> nd day of is displayed.
uuuu -> 4 digit representation for the year.
Output is: 32nd day of 2023.
NOTE: To display single quote, escape it with one more single quote. For example,
DateTimeFormatter.ofPattern("'It''s' DD'nd day of' uuuu"); this will help to print: [It's 32nd day of 2018] where as DateTimeFormatter.ofPattern("'It's' DD'nd day of' uuuu"); causes runtime exception.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
