Unit 5: Java Date and Time API - Practice Quiz

CSE406 — Advanced Java Programming 60 Questions
0 Correct 0 Wrong 60 Left
0/60

1 Which Java class represents a date without a time or time zone?

Creating and managing date-based events Easy
A. ZoneId
B. LocalDate
C. Instant
D. LocalTime

2 Which method adds a specified number of days to a LocalDate?

Creating and managing date-based events Easy
A. plusDays()
B. addDays()
C. increaseDays()
D. appendDays()

3 Which method creates a LocalDate from a specific year, month, and day?

Creating and managing date-based events Easy
A. LocalDate.of()
B. LocalDate.create()
C. LocalDate.make()
D. LocalDate.build()

4 What does LocalDate.now() return?

Creating and managing date-based events Easy
A. The current duration
B. The current time zone
C. The current time
D. The current date

5 Which Java class represents a time without a date or time zone?

Creating and managing time-based events Easy
A. ZonedDateTime
B. Period
C. LocalDate
D. LocalTime

6 Which method adds minutes to a LocalTime object?

Creating and managing time-based events Easy
A. plusMinutes()
B. addMinutes()
C. appendMinutes()
D. increaseMinutes()

7 Which method creates a LocalTime from hour and minute values?

Creating and managing time-based events Easy
A. LocalTime.make()
B. LocalTime.build()
C. LocalTime.create()
D. LocalTime.of()

8 What does LocalTime.now() return?

Creating and managing time-based events Easy
A. The current calendar year
B. The current local time
C. The current time zone
D. The current date

9 Which class combines a date and a time without storing a time zone?

Combining date and time into a single object Easy
A. LocalDateTime
B. DateTimeFormatter
C. Duration
D. ZonedDateTime

10 Which method combines a LocalDate and a LocalTime into a LocalDateTime?

Combining date and time into a single object Easy
A. join()
B. combine()
C. mergeTime()
D. atTime()

11 Which method extracts the date part from a LocalDateTime?

Combining date and time into a single object Easy
A. getDate()
B. toLocalDate()
C. toDate()
D. datePart()

12 Which method extracts the time part from a LocalDateTime?

Combining date and time into a single object Easy
A. toTimeOnly()
B. toLocalTime()
C. timePart()
D. getTime()

13 Which Java class represents a date and time with a time-zone ID?

Working with dates and times across time zones Easy
A. ZonedDateTime
B. Period
C. LocalTime
D. LocalDateTime

14 Which class represents a time-zone identifier such as Europe/Paris?

Working with dates and times across time zones Easy
A. TimeRegion
B. ZoneId
C. ZoneName
D. ZoneTime

15 Which method attaches a time zone to a LocalDateTime?

Working with dates and times across time zones Easy
A. atZone()
B. addZone()
C. setZone()
D. withZone()

16 Which Java class represents a point on the global timeline?

Defining and creating timestamps, periods, and durations Easy
A. ZoneId
B. Period
C. Instant
D. LocalDate

17 Which class is used to represent an amount of date-based time such as years, months, and days?

Defining and creating timestamps, periods, and durations Easy
A. Period
B. Instant
C. Duration
D. LocalTime

18 Which class represents a time-based amount such as hours, minutes, and seconds?

Defining and creating timestamps, periods, and durations Easy
A. LocalDate
B. Duration
C. Period
D. ZoneId

19 Which class is used to format and parse Java date and time objects?

Applying formatting to local and zoned dates and times Easy
A. DateTimeFormatter
B. DateFormatter
C. FormatBuilder
D. TimeParser

20 Which method creates a DateTimeFormatter from a pattern such as dd-MM-yyyy?

Applying formatting to local and zoned dates and times Easy
A. DateTimeFormatter.createPattern()
B. DateTimeFormatter.fromPattern()
C. DateTimeFormatter.withPattern()
D. DateTimeFormatter.ofPattern()

21 A conference occurs annually on March 15. Which expression creates the date for the 2026 conference?

Creating and managing date-based events Medium
A. LocalDate.of(15, 3, 2026)
B. LocalDate.parse("15-03-2026")
C. LocalDate.of(2026, Month.MARCH, 16)
D. LocalDate.of(2026, 3, 15)

22 A subscription starts on 2025-01-31 and must be renewed on the same day of the month whenever possible. What is the result of start.plusMonths(1)?

Creating and managing date-based events Medium
A. 2025-03-01
B. 2025-02-28
C. 2025-02-31
D. 2025-01-31

23 Which condition correctly checks whether an event date occurs within the inclusive range from startDate through endDate?

Creating and managing date-based events Medium
A. event.isAfter(startDate) && event.isBefore(endDate)
B. !event.isBefore(startDate) && !event.isAfter(endDate)
C. event.isBefore(startDate) || event.isAfter(endDate)
D. event.equals(startDate) || event.equals(endDate)

24 A project begins on 2024-02-28 and ends on 2024-03-05. Which value represents the number of elapsed days between the two dates?

Creating and managing date-based events Medium
A. 8
B. 6
C. 7
D. 5

25 A task starts at 09:45 and lasts 2 hours and 30 minutes. Which expression calculates its ending time?

Creating and managing time-based events Medium
A. start.plusHours(2).plusMinutes(30)
B. start.plusMinutes(2).plusHours(30)
C. start.minusHours(2).plusMinutes(30)
D. start.plusHours(2).minusMinutes(30)

26 What is the result of evaluating LocalTime.of(23, 30).plusMinutes(90)?

Creating and managing time-based events Medium
A. 23:00
B. 00:30
C. 01:00
D. 24:60

27 Which method is most appropriate for checking whether a meeting time is before another meeting time?

Creating and managing time-based events Medium
A. first.equals(second)
B. first.compareTo(second) == 1
C. first.isAfter(second)
D. first.isBefore(second)

28 A system records a daily backup at 22:15 and needs to run it every 6 hours. Which object best represents this repeating time interval?

Creating and managing time-based events Medium
A. Period.ofDays(6)
B. LocalDate.of(6, 22, 15)
C. MonthDay.of(6, 22)
D. Duration.ofHours(6)

29 Which expression combines the date 2026-04-10 and the time 14:30 into a LocalDateTime?

Combining date and time into a single object Medium
A. LocalDateTime.parse(date + time)
B. LocalDateTime.of(date, time)
C. time.atDate(date).toLocalDate()
D. date.atZone(time)

30 A deadline is stored as LocalDateTime deadline. Which expression adds three calendar days while preserving the local time?

Combining date and time into a single object Medium
A. deadline.withDayOfMonth(3)
B. deadline.plus(Period.ofHours(72))
C. deadline.plusHours(72)
D. deadline.plusDays(3)

31 Which statement about a LocalDateTime is correct when scheduling an event for users in different regions?

Combining date and time into a single object Medium
A. It always represents the same instant worldwide.
B. It automatically adjusts for daylight-saving changes.
C. It stores the UTC offset used when it was created.
D. It contains no time-zone or offset information.

32 What is the result of LocalDateTime.of(2025, 8, 20, 10, 0).toLocalDate()?

Combining date and time into a single object Medium
A. 2025-08-21
B. 2025-08-20T10:00
C. 2025-08-20
D. 10:00

33 A meeting is scheduled at 09:00 in America/New_York. Which expression creates a zoned date-time for July 1, 2026?

Working with dates and times across time zones Medium
A. OffsetDateTime.of(2026, 7, 1, 9, 0, ZoneId.of("America/New_York"))
B. LocalDateTime.of(2026, 7, 1, 9, 0).atZone(ZoneId.of("UTC"))
C. ZonedDateTime.of(2026, 7, 1, 9, 0, 0, 0, ZoneId.of("America/New_York"))
D. ZonedDateTime.of(LocalDate.of(2026, 7, 1), ZoneId.of("America/New_York"))

34 A ZonedDateTime represents an event in Tokyo. Which operation converts it to the equivalent local time in London while preserving the same instant?

Working with dates and times across time zones Medium
A. event.withZoneSameInstant(ZoneId.of("Europe/London"))
B. event.withZoneSameLocal(ZoneId.of("Europe/London"))
C. event.toLocalDateTime().atZone(ZoneId.of("Europe/London"))
D. event.atZone(ZoneId.of("Europe/London"))

35 Why is ZoneId generally preferred over a fixed offset such as +02:00 for recurring events tied to a region?

Working with dates and times across time zones Medium
A. It prevents the date from changing during conversion.
B. It stores only the current offset permanently.
C. It applies regional daylight-saving and offset rules.
D. It always uses UTC regardless of the region.

36 Which object is most appropriate for recording the exact moment when a transaction was received by a server?

Defining and creating timestamps, periods, and durations Medium
A. MonthDay
B. Period
C. LocalDate
D. Instant

37 What does Duration.between(LocalTime.of(8, 15), LocalTime.of(10, 45)).toMinutes() return?

Defining and creating timestamps, periods, and durations Medium
A. 180
B. 120
C. 135
D. 150

38 Which statement correctly distinguishes Period from Duration?

Defining and creating timestamps, periods, and durations Medium
A. Period always measures seconds, while Duration measures years.
B. Period uses date units, while Duration uses time-based units.
C. Period stores a time zone, while Duration stores an offset.
D. Period represents an instant, while Duration represents a calendar date.

39 What is the result of adding Period.ofMonths(1) to LocalDate.of(2024, 1, 31)?

Defining and creating timestamps, periods, and durations Medium
A. 2024-03-02
B. 2024-02-31
C. 2024-01-31
D. 2024-02-29

40 Which formatter pattern produces output such as 2026/07/04 09:05 for a LocalDateTime?

Applying formatting to local and zoned dates and times Medium
A. MM/dd/yy HH:ss
B. yyyy-mm-dd HH:MM
C. yyyy/MM/dd HH:mm
D. dd/MM/yyyy hh:mm

41 What does the following code print?

LocalDate d = LocalDate.of(2024, 1, 31);

System.out.println(d.plusMonths(1).plusMonths(1) + " | " + d.plusMonths(2));

Creating and managing date-based events Hard
A. 2024-03-29 | 2024-03-31
B. 2024-04-01 | 2024-03-31
C. 2024-03-29 | 2024-03-29
D. 2024-03-31 | 2024-03-31

42 Given LocalDate date = LocalDate.of(2025, 2, 10);, what is produced by date.with(TemporalAdjusters.lastDayOfMonth()).with(TemporalAdjusters.nextOrSame(DayOfWeek.MONDAY))?

Creating and managing date-based events Hard
A. 2025-02-28
B. 2025-03-03
C. 2025-03-10
D. 2025-02-24

43 A yearly event is represented by MonthDay.of(2, 29). What does event.atYear(2023) return?

Creating and managing date-based events Hard
A. 2023-02-28
B. 2023-03-01
C. It throws DateTimeException
D. 2023-02-29

44 What is the result of ChronoUnit.MONTHS.between(LocalDate.of(2024, 1, 31), LocalDate.of(2024, 3, 30))?

Creating and managing date-based events Hard
A. 3
B. 0
C. 1
D. 2

45 What does Duration.between(LocalTime.of(23, 30), LocalTime.of(0, 15)) produce?

Creating and managing time-based events Hard
A. PT-45M
B. PT45M
C. PT23H15M
D. PT-23H-15M

46 Given LocalTime t = LocalTime.of(12, 34, 56, 987_654_321);, which expression evaluates to 12:34:56.987?

Creating and managing time-based events Hard
A. t.truncatedTo(ChronoUnit.MILLIS)
B. t.withNano(987_000_000).plusNanos(654_321)
C. t.truncatedTo(ChronoUnit.MICROS)
D. t.truncatedTo(ChronoUnit.SECONDS).plusNanos(987_654_321)

47 What is the result of the following expression?

LocalDateTime.of(LocalDate.of(2024, 12, 31), LocalTime.MAX).plusNanos(1)

Combining date and time into a single object Hard
A. 2025-01-01T00:00
B. 2024-12-31T23:59:59.999999999
C. 2025-01-01T00:00:00.000000001
D. It throws DateTimeException

48 In Europe/Berlin, the local time 2024-03-31T02:30 lies inside a daylight-saving gap. What does LocalDateTime.of(2024, 3, 31, 2, 30).atZone(ZoneId.of("Europe/Berlin")) produce?

Combining date and time into a single object Hard
A. It throws DateTimeException
B. 2024-03-31T03:30+02:00[Europe/Berlin]
C. 2024-03-31T02:30+01:00[Europe/Berlin]
D. 2024-03-31T01:30+01:00[Europe/Berlin]

49 Which statement about LocalDateTime is correct when modeling a globally unique scheduled execution?

Combining date and time into a single object Hard
A. It is insufficient only for dates occurring during leap years
B. It is insufficient because it has neither an offset nor a zone
C. It is sufficient because every local date-time maps to one instant
D. It is sufficient when its seconds and nanoseconds are both nonzero

50 Consider the two occurrences of 2024-11-03T01:30 in America/New_York during the DST overlap. If earlier uses the earlier offset and later uses the later offset, what is Duration.between(earlier, later)?

Working with dates and times across time zones Hard
A. PT0S
B. PT2H
C. PT-1H
D. PT1H

51 Let z be 2024-01-15T10:00+09:00[Asia/Tokyo]. Which operation creates a Paris value representing the same instant?

Working with dates and times across time zones Hard
A. z.withZoneSameLocal(ZoneId.of("Europe/Paris"))
B. z.withFixedOffsetZone().withZoneSameLocal(ZoneId.of("Europe/Paris"))
C. z.toLocalDateTime().atZone(ZoneId.of("Europe/Paris"))
D. z.withZoneSameInstant(ZoneId.of("Europe/Paris"))

52 Given

OffsetDateTime a = OffsetDateTime.parse("2024-01-01T10:00+02:00");

OffsetDateTime b = OffsetDateTime.parse("2024-01-01T08:00Z");

which statement is true?

Working with dates and times across time zones Hard
A. a.equals(b) is true and a.isEqual(b) is false
B. a.equals(b) is false and a.isEqual(b) is true
C. a.equals(b) and a.isEqual(b) are both false
D. a.equals(b) and a.isEqual(b) are both true

53 A fixed clock is created as follows:

Clock c = Clock.fixed(Instant.parse("2024-01-01T00:30:00Z"), ZoneOffset.UTC);

Which pair is returned by LocalDate.now(c.withZone(ZoneId.of("America/Los_Angeles"))) and LocalDate.now(c.withZone(ZoneId.of("Asia/Tokyo"))), respectively?

Working with dates and times across time zones Hard
A. 2023-12-31 and 2024-01-01
B. 2024-01-01 and 2024-01-02
C. 2023-12-31 and 2024-01-02
D. 2024-01-01 and 2024-01-01

54 In America/New_York, let start be 2024-03-09T12:00-05:00. Which result correctly compares start.plus(Period.ofDays(1)) with start.plus(Duration.ofDays(1))?

Working with dates and times across time zones Hard
A. Both end at 2024-03-10T12:00-04:00
B. Both end at 2024-03-10T13:00-04:00
C. The period ends at 12:00, while the duration ends at 13:00
D. The period ends at 13:00, while the duration ends at 12:00

55 What does Instant.ofEpochSecond(1, 1_500_000_000) return?

Defining and creating timestamps, periods, and durations Hard
A. It throws DateTimeException
B. 1970-01-01T00:00:02.000Z
C. 1970-01-01T00:00:01.500Z
D. 1970-01-01T00:00:02.500Z

56 What is the result of Period.of(1, 15, 40).normalized()?

Defining and creating timestamps, periods, and durations Hard
A. P3Y3M10D
B. P1Y15M40D
C. P2Y3M40D
D. P2Y4M10D

57 What happens when Duration.between(LocalDate.of(2024, 1, 1), LocalDate.of(2024, 1, 2)) is evaluated?

Defining and creating timestamps, periods, and durations Hard
A. It returns P1D
B. It returns PT0S
C. It returns PT24H
D. It throws UnsupportedTemporalTypeException

58 Two ZonedDateTime values in America/New_York represent noon on March 9 and noon on March 10, 2024. What are the results of ChronoUnit.DAYS.between(start, end) and Duration.between(start, end).toHours()?

Defining and creating timestamps, periods, and durations Hard
A. 0 days and 23 hours
B. 0 days and 24 hours
C. 1 day and 23 hours
D. 1 day and 24 hours

59 What happens when the following code is executed?

DateTimeFormatter f = DateTimeFormatter.ofPattern("yyyy-MM-dd").withResolverStyle(ResolverStyle.STRICT);

LocalDate.parse("2024-02-29", f);

Applying formatting to local and zoned dates and times Hard
A. It returns 2024-02-29
B. It returns 2024-02-28
C. It returns 2024-03-01
D. It throws DateTimeParseException

60 A formatter is defined by DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm XXX VV"). What does it produce for ZonedDateTime.of(2024, 7, 1, 9, 5, 0, 0, ZoneId.of("Asia/Kolkata"))?

Applying formatting to local and zoned dates and times Hard
A. 2024-07-01 09:05 +05:30 Asia/Kolkata
B. 2024-07-01 03:35 Z Asia/Kolkata
C. 2024-07-01 09:05 IST Asia/Kolkata
D. 2024-07-01 09:05 +0530 Asia/Kolkata