Search Tutorials


Java Date and Time API MCQ Questions and Answers | JavaInUse

Java Date and Time API MCQ Questions and Answers

Q. What package contains the main classes and interfaces for the Java Date and Time API?

A. java.time
B. java.util
C. java.date
D. java.datetime

Q. Which class in the Java Date and Time API represents a human-readable form of a date, time, or date-time?

A. LocalDateTime
B. Instant
C. ZonedDateTime
D. LocalDate

Q. How do you create a LocalDate object representing the current date?

A. LocalDate today = LocalDate.now();
B. LocalDate today = LocalDate();
C. LocalDate today = new LocalDate();
D. LocalDate today = LocalDate.current();

Q. What is the result of the following code snippet? LocalDate date = LocalDate.of(2024, 2, 29); System.out.println(date);

A. 2024-02-29
B. 2024-03-01
C. Error: Invalid date
D. 2024-02-28

Q. How do you add 7 days to a LocalDate object?

A. date.plusDays(7)
B. date + 7
C. date.addDays(7)
D. date.increaseDays(7)

Q. Which class in the Java Date and Time API represents a date without a time component?

A. LocalDate
B. LocalDateTime
C. ZonedDateTime
D. Instant

Q. How do you create a LocalDateTime object representing a specific date and time?

A. LocalDateTime dateTime = LocalDateTime.of(2024, 4, 26, 10, 30);
B. LocalDateTime dateTime = new LocalDateTime(2024, 4, 26, 10, 30);
C. LocalDateTime dateTime = LocalDateTime(2024, 4, 26, 10, 30);
D. LocalDateTime dateTime = LocalDateTime.create(2024, 4, 26, 10, 30);

Q. What is the result of the following code snippet? LocalDateTime dateTime = LocalDateTime.of(2024, 6, 1, 18, 0); dateTime = dateTime.withHour(20); System.out.println(dateTime);

A. 2024-06-01T20:00
B. 2024-06-01T18:00
C. 2024-06-01T00:00
D. Error: Cannot modify LocalDateTime

Q. Which class in the Java Date and Time API represents a date-time with a time zone?

A. ZonedDateTime
B. LocalDateTime
C. Instant
D. OffsetDateTime

Q. How do you get the year, month, and day values from a LocalDate object?

A. int year = date.getYear(); int month = date.getMonth(); int day = date.getDay();
B. int year = date.get(ChronoField.YEAR); int month = date.get(ChronoField.MONTH_OF_YEAR); int day = date.get(ChronoField.DAY_OF_MONTH);
C. int year = date.getYearValue(); int month = date.getMonthValue(); int day = date.getDayOfMonth();
D. int year = date.getYearlyValue(); int month = date.getMonthlyValue(); int day = date.getDayValue();





Q. What is the result of the following code snippet? LocalDateTime dateTime = LocalDateTime.of(2024, 1, 15, 9, 0); dateTime = dateTime.with(ChronoField.MINUTE_OF_HOUR, 30); System.out.println(dateTime);

A. 2024-01-15T09:30
B. 2024-01-15T00:30
C. 2024-01-15T09:00
D. Error: Invalid field

Q. How do you get the number of days between two LocalDate objects?

A. long days = startDate.until(endDate, ChronoUnit.DAYS);
B. long days = endDate.minusDays(startDate);
C. long days = endDate.daysUntil(startDate);
D. long days = endDate.daysBetween(startDate);

Q. Which class in the Java Date and Time API represents a duration of time?

A. Duration
B. Period
C. Interval
D. Span

Q. How do you create a Duration object representing 3 hours and 30 minutes?

A. Duration duration = Duration.ofHours(3).plusMinutes(30);
B. Duration duration = Duration.of(3, 30, ChronoUnit.MINUTES);
C. Duration duration = Duration.of(3, ChronoUnit.HOURS).plus(Duration.of(30, ChronoUnit.MINUTES));
D. Duration duration = Duration.of(3, 30);

Q. Which of the following code snippets correctly creates a LocalDate for the date April 26, 2024, and prints it in the format "MM/dd/yyyy"?

A.
LocalDate date = LocalDate.of(2024, Month.APRIL, 26);
System.out.println(date.format(DateTimeFormatter.ofPattern("MM/dd/yyyy")));
B.
LocalDate date = LocalDate.now();
date = date.withYear(2024).withMonth(4).withDayOfMonth(26);
System.out.println(date.format(DateTimeFormatter.BASIC_ISO_DATE));
C.
LocalDate date = LocalDate.of(2024, 4, 26);
System.out.println(date.format(DateTimeFormatter.ISO_LOCAL_DATE));
D.
LocalDate date = LocalDate.of(2024, Month.APRIL.getValue(), 26);
System.out.println(date.format(DateTimeFormatter.ofPattern("MM-dd-yyyy")));

Q. What will be the output of the following code snippet?

LocalDateTime dateTime = LocalDateTime.of(2023, Month.JANUARY, 15, 10, 30);
System.out.println(dateTime.getHour());
A.
10
B.
0
C.
24
D.
The code will throw an exception.

Q. Which of the following code snippets correctly calculates the number of days between two LocalDate objects?

A.
LocalDate date1 = LocalDate.of(2023, Month.JANUARY, 1);
LocalDate date2 = LocalDate.of(2023, Month.FEBRUARY, 1);
long days = date2.toEpochDay() - date1.toEpochDay();
System.out.println("Days: " + days);
B.
LocalDate date1 = LocalDate.of(2023, Month.JANUARY, 1);
LocalDate date2 = LocalDate.of(2023, Month.FEBRUARY, 1);
Period period = Period.between(date1, date2);
System.out.println("Days: " + period.getDays());
C.
LocalDate date1 = LocalDate.of(2023, Month.JANUARY, 1);
LocalDate date2 = LocalDate.of(2023, Month.FEBRUARY, 1);
long days = ChronoUnit.DAYS.between(date1, date2);
System.out.println("Days: " + days);
D.
LocalDate date1 = LocalDate.of(2023, Month.JANUARY, 1);
LocalDate date2 = LocalDate.of(2023, Month.FEBRUARY, 1);
Duration duration = Duration.between(date1.atStartOfDay(), date2.atStartOfDay());
long days = duration.toDays();
System.out.println("Days: " + days);

Q. What will be the output of the following code snippet?

LocalDate date = LocalDate.of(2023, Month.FEBRUARY, 28);
System.out.println("Day of Year: " + date.getDayOfYear());
A.
Day of Year: 60
B.
Day of Year: 59
C.
Day of Year: 365
D.
The code will throw an exception.

Q. Which of the following code snippets correctly creates a LocalDateTime for the current date and time and formats it as "yyyy-MM-dd HH:mm:ss"?

A.
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
System.out.println(now.format(formatter));
B.
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ISO_DATE_TIME;
System.out.println(formatter.format(now));
C.
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);
System.out.println(formatter.format(now));
D.
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
System.out.println(now.format(formatter));

Q. What will be the output of the following code snippet?

LocalDate date = LocalDate.of(2023, Month.APRIL, 1);
System.out.println(date.plusMonths(2).getYear());
A.
2023
B.
2024
C.
The code will throw an exception.
D.
2025

Q. Which of the following code snippets correctly creates a ZonedDateTime for the specified date and time in the UTC time zone?

A.
ZonedDateTime zonedDateTime = ZonedDateTime.of(2023, Month.JUNE, 21, 15, 30, 0, 0, ZoneId.of("UTC"));
System.out.println(zonedDateTime);
B.
ZonedDateTime zonedDateTime = ZonedDateTime.now(ZoneId.of("UTC"));
zonedDateTime = zonedDateTime.withYear(2023).withMonth(6).withDayOfMonth(21).withHour(15).withMinute(30);
System.out.println(zonedDateTime);
C.
ZonedDateTime zonedDateTime = ZonedDateTime.of(LocalDate.of(2023, Month.JUNE, 21), LocalTime.of(15, 30), ZoneId.UTC);
System.out.println(zonedDateTime);
D.
ZonedDateTime zonedDateTime = ZonedDateTime.now(ZoneId.systemDefault());
zonedDateTime = zonedDateTime.withZoneSameInstant(ZoneId.of("UTC"));
zonedDateTime = zonedDateTime.withYear(2023).withMonth(6).withDayOfMonth(21).withHour(15).withMinute(30);
System.out.println(zonedDateTime);

Q. What will be the output of the following code snippet?

LocalDate date = LocalDate.of(2023, Month.MARCH, 1);
System.out.println(date.lengthOfMonth());
A.
31
B.
28
C.
30
D.
The code will throw an exception.

Q. Which of the following code snippets correctly creates a Duration object representing a duration of 2 hours and 30 minutes?

A.
Duration duration = Duration.ofHours(2).plusMinutes(30);
System.out.println(duration);
B.
Duration duration = Duration.of(2, 30, ChronoUnit.MINUTES);
System.out.println(duration);
C.
Duration duration = Duration.of(2, ChronoUnit.HOURS).plus(Duration.of(30, ChronoUnit.MINUTES));
System.out.println(duration);
D.
Duration duration = Duration.of(2, 30, ChronoUnit.HOURS);
System.out.println(duration);

Q. What will be the output of the following code snippet?

LocalDateTime dateTime = LocalDateTime.of(2023, Month.DECEMBER, 31, 23, 59);
System.out.println(dateTime.plusSeconds(1));
A.
2024-01-01T00:00
B.
2023-12-31T23:59:01
C.
2023-12-31T23:00:00
D.
The code will throw an exception.