OCP Date, Time, and Localization 1 — Questions and Answers
Question 1: What is the result of executing `LocalDate.of(2024, Month.FEBRUARY, 29)`?
- Returns 2024-02-29 because 2024 is a leap year (Correct answer)
- Returns 2024-03-01 by rolling over
- Throws a DateTimeException
- Returns 2024-02-28
Correct answer: Returns 2024-02-29 because 2024 is a leap year
2024 is a leap year, making February 29 a valid date, so LocalDate.of(2024, Month.FEBRUARY, 29) successfully creates 2024-02-29.
Question 2: Which Java class represents a date and time without any time-zone information?
- ZonedDateTime
- LocalDateTime (Correct answer)
- Instant
- OffsetDateTime
Correct answer: LocalDateTime
LocalDateTime stores a date-time without any zone or offset, while ZonedDateTime includes a zone, Instant is epoch-based, and OffsetDateTime carries a UTC offset.
Question 3: What does `Period.between(LocalDate.of(2023, 1, 1), LocalDate.of(2024, 3, 15))` return when printed?
- P1Y2M14D (Correct answer)
- P14M14D
- P440D
- P1Y14D
Correct answer: P1Y2M14D
Period stores years, months, and days separately; from January 1 2023 to March 15 2024 is exactly 1 year, 2 months, and 14 days.
Question 4: Which method correctly obtains the current date using the UTC time zone?
- LocalDate.now()
- LocalDate.now(ZoneId.of("UTC")) (Correct answer)
- ZonedDateTime.now().toLocalDate()
- Instant.now().toLocalDate()
Correct answer: LocalDate.now(ZoneId.of("UTC"))
LocalDate.now(ZoneId.of("UTC")) explicitly passes the UTC zone to get the current date in that zone, whereas the no-arg now() uses the JVM default zone.
Question 5: Which of the following correctly creates a Duration of 2 hours and 30 minutes?
- Duration.of(2, ChronoUnit.HOURS).plusMinutes(30) only
- Duration.ofHours(2).plus(30, ChronoUnit.MINUTES) only
- Both options produce the same valid Duration (Correct answer)
- Duration.create(2, 30) with HOURS unit
Correct answer: Both options produce the same valid Duration
Both Duration.of(2, ChronoUnit.HOURS).plusMinutes(30) and Duration.ofHours(2).plus(30, ChronoUnit.MINUTES) are valid fluent ways to build a 2h30m Duration.
Question 6: Which of the following correctly parses the string "2024-06-15" into a LocalDate?
- LocalDate.parse("2024-06-15") only
- LocalDate.parse("2024-06-15", DateTimeFormatter.ISO_LOCAL_DATE) only
- Both options parse the string correctly (Correct answer)
- LocalDate.of("2024-06-15")
Correct answer: Both options parse the string correctly
LocalDate.parse(String) defaults to ISO_LOCAL_DATE format, so both the no-formatter overload and the explicit ISO_LOCAL_DATE formatter parse the same string identically.
Question 7: What does `LocalDate.now().with(TemporalAdjusters.firstDayOfMonth())` return?
- The last day of the current month
- The first day of the current month (Correct answer)
- The first day of the current year
- The first day of next month
Correct answer: The first day of the current month
TemporalAdjusters.firstDayOfMonth() adjusts any date to day 1 of its month, so the result is the first day of the current month.
What is the result of executing `LocalDate.of(2024, Month.FEBRUARY, 29)`?