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. LocalTime
B. LocalDate
C. ZoneId
D. Instant

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

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

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

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

4 What does LocalDate.now() return?

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

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

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

6 Which method adds minutes to a LocalTime object?

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

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

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

8 What does LocalTime.now() return?

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

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. ZonedDateTime
C. Duration
D. DateTimeFormatter

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

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

11 Which method extracts the date part from a LocalDateTime?

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

12 Which method extracts the time part from a LocalDateTime?

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

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

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

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

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

15 Which method attaches a time zone to a LocalDateTime?

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

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

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

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. Instant
B. Period
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. Duration
B. Period
C. ZoneId
D. LocalDate

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. FormatBuilder
B. DateTimeFormatter
C. DateFormatter
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.fromPattern()
B. DateTimeFormatter.ofPattern()
C. DateTimeFormatter.withPattern()
D. DateTimeFormatter.createPattern()

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(2026, Month.MARCH, 16)
B. LocalDate.of(2026, 3, 15)
C. LocalDate.of(15, 3, 2026)
D. LocalDate.parse("15-03-2026")

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-02-28
B. 2025-02-31
C. 2025-03-01
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.isBefore(startDate) || event.isAfter(endDate)
B. !event.isBefore(startDate) && !event.isAfter(endDate)
C. event.isAfter(startDate) && event.isBefore(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.plusHours(2).minusMinutes(30)
C. start.plusMinutes(2).plusHours(30)
D. start.minusHours(2).plusMinutes(30)

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

Creating and managing time-based events Medium
A. 00:30
B. 23:00
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.isAfter(second)
C. first.isBefore(second)
D. first.compareTo(second) == 1

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. Duration.ofHours(6)
D. MonthDay.of(6, 22)

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. date.atZone(time)
B. time.atDate(date).toLocalDate()
C. LocalDateTime.of(date, time)
D. LocalDateTime.parse(date + 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.plusHours(72)
C. deadline.plusDays(3)
D. deadline.plus(Period.ofHours(72))

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 stores the UTC offset used when it was created.
B. It always represents the same instant worldwide.
C. It automatically adjusts for daylight-saving changes.
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-20
B. 2025-08-20T10:00
C. 10:00
D. 2025-08-21

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. ZonedDateTime.of(LocalDate.of(2026, 7, 1), ZoneId.of("America/New_York"))
B. ZonedDateTime.of(2026, 7, 1, 9, 0, 0, 0, ZoneId.of("America/New_York"))
C. LocalDateTime.of(2026, 7, 1, 9, 0).atZone(ZoneId.of("UTC"))
D. OffsetDateTime.of(2026, 7, 1, 9, 0, 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.atZone(ZoneId.of("Europe/London"))
C. event.toLocalDateTime().atZone(ZoneId.of("Europe/London"))
D. event.withZoneSameLocal(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 applies regional daylight-saving and offset rules.
C. It stores only the current offset permanently.
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. Instant
B. LocalDate
C. MonthDay
D. Period

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

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

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 represents an instant, while Duration represents a calendar date.
C. Period stores a time zone, while Duration stores an offset.
D. Period uses date units, while Duration uses time-based units.

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-02-29
B. 2024-01-31
C. 2024-02-31
D. 2024-03-02

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. yyyy/MM/dd HH:mm
B. yyyy-mm-dd HH:MM
C. dd/MM/yyyy hh:mm
D. MM/dd/yy HH:ss

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-31 | 2024-03-31
B. 2024-04-01 | 2024-03-31
C. 2024-03-29 | 2024-03-31
D. 2024-03-29 | 2024-03-29

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-03-10
B. 2025-03-03
C. 2025-02-24
D. 2025-02-28

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-03-01
B. 2023-02-28
C. 2023-02-29
D. It throws DateTimeException

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. 0
B. 2
C. 1
D. 3

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

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

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.MICROS)
B. t.truncatedTo(ChronoUnit.MILLIS)
C. t.truncatedTo(ChronoUnit.SECONDS).plusNanos(987_654_321)
D. t.withNano(987_000_000).plusNanos(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-31T01:30+01:00[Europe/Berlin]
C. 2024-03-31T02:30+01:00[Europe/Berlin]
D. 2024-03-31T03:30+02: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 sufficient when its seconds and nanoseconds are both nonzero
B. It is sufficient because every local date-time maps to one instant
C. It is insufficient only for dates occurring during leap years
D. It is insufficient because it has neither an offset nor a zone

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. PT-1H
B. PT0S
C. PT2H
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.toLocalDateTime().atZone(ZoneId.of("Europe/Paris"))
B. z.withFixedOffsetZone().withZoneSameLocal(ZoneId.of("Europe/Paris"))
C. z.withZoneSameInstant(ZoneId.of("Europe/Paris"))
D. z.withZoneSameLocal(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) and a.isEqual(b) are both false
B. a.equals(b) and a.isEqual(b) are both true
C. a.equals(b) is true and a.isEqual(b) is false
D. a.equals(b) is false and a.isEqual(b) is 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. 2024-01-01 and 2024-01-01
B. 2024-01-01 and 2024-01-02
C. 2023-12-31 and 2024-01-02
D. 2023-12-31 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. 1970-01-01T00:00:01.500Z
B. It throws DateTimeException
C. 1970-01-01T00:00:02.000Z
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. P2Y3M40D
B. P2Y4M10D
C. P3Y3M10D
D. P1Y15M40D

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 throws UnsupportedTemporalTypeException
C. It returns PT0S
D. It returns PT24H

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. 1 day and 24 hours
B. 0 days and 23 hours
C. 0 days and 24 hours
D. 1 day and 23 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 throws DateTimeParseException
B. It returns 2024-03-01
C. It returns 2024-02-28
D. It returns 2024-02-29

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 03:35 Z Asia/Kolkata
B. 2024-07-01 09:05 +05:30 Asia/Kolkata
C. 2024-07-01 09:05 +0530 Asia/Kolkata
D. 2024-07-01 09:05 IST Asia/Kolkata