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.timeB. 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. LocalDateTimeB. 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-29B. 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. LocalDateB. 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:00B. 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. ZonedDateTimeB. 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();