Java Date and Time API MCQ Questions and Answers

1. Which package contains the Java Date and Time API?

a) java.util
b) java.time
c) java.date
d) java.datetime

Answer:

b) java.time

Explanation:

The Java Date and Time API is located in the java.time package, introduced in Java 8.

2. What class represents a date in the Java Date and Time API?

a) Date
b) Calendar
c) LocalDate
d) DateTime

Answer:

c) LocalDate

Explanation:

LocalDate represents a date in the Java Date and Time API without time or timezone information.

3. How do you obtain the current date using the Java Date and Time API?

a) LocalDate.now()
b) new LocalDate()
c) Date.currentDate()
d) Calendar.getInstance()

Answer:

a) LocalDate.now()

Explanation:

LocalDate.now() is used to obtain the current date according to the system clock and default time-zone.

4. Which class represents time in the Java Date and Time API?

a) Time
b) LocalTime
c) Clock
d) Timer

Answer:

b) LocalTime

Explanation:

LocalTime represents a time without date or timezone information.

5. What is the purpose of the LocalDateTime class?

a) To represent date and time for database usage
b) To represent a specific time of day
c) To represent a combination of date and time
d) To schedule future tasks

Answer:

c) To represent a combination of date and time

Explanation:

LocalDateTime combines date and time information but does not include time-zone data.

6. How do you format dates and times in the new API?

a) SimpleDateFormat
b) DateFormat
c) DateTimeFormatter
d) DateFormatter

Answer:

c) DateTimeFormatter

Explanation:

DateTimeFormatter is used for formatting and parsing dates and times in the new API.

7. Which class in the Date and Time API represents a specific instant in time?

a) Instant
b) Epoch
c) TimeStamp
d) Moment

Answer:

a) Instant

Explanation:

Instant represents a specific moment on the timeline in UTC.

8. What is the purpose of the ZonedDateTime class?

a) To handle daylight saving time changes
b) To represent a date and time with timezone
c) To convert dates between different timezones
d) Both b and c

Answer:

d) Both b and c

Explanation:

ZonedDateTime handles date and time with associated timezone, including daylight saving time and timezone conversion.

9. How do you add or subtract time from a date or time object?

a) Using add() and subtract() methods
b) Using the plus() and minus() methods
c) With simple arithmetic operations
d) By setting new values

Answer:

b) Using the plus() and minus() methods

Explanation:

The plus() and minus() methods are used to add or subtract time from date or time objects.

10. What class would you use to represent a duration of time?

a) Period
b) Duration
c) TimeSpan
d) Interval

Answer:

b) Duration

Explanation:

Duration is used to represent a time-based amount of time, such as seconds, minutes, and hours.

11. How do you obtain the current time based on the system clock?

a) LocalTime.now()
b) System.currentTimeMillis()
c) Clock.systemDefaultZone().instant()
d) new Time()

Answer:

a) LocalTime.now()

Explanation:

LocalTime.now() obtains the current time based on the system clock in the default time-zone.

12. Which class represents a date-based amount of time, such as '2 years, 3 months and 4 days'?

a) Duration
b) Period
c) Interval
d) TimeRange

Answer:

b) Period

Explanation:

Period is used to represent a date-based amount of time in terms of years, months, and days.

13. How do you compare two LocalDate objects?

a) Using the equals() method
b) With the compareTo() method
c) By using relational operators
d) Both a and b

Answer:

d) Both a and b

Explanation:

LocalDate objects can be compared using the equals() method for equality and compareTo() for ordering.

14. What is the primary use of the Clock class in the Date and Time API?

a) To get the current time as a string
b) To schedule future events
c) To provide access to the current date and time
d) To handle timezones

Answer:

c) To provide access to the current date and time

Explanation:

The Clock class is used to provide access to the current instant, date, and time using a time-zone.

15. How do you convert a String to a LocalDate in Java?

a) LocalDate.parse()
b) LocalDate.fromString()
c) LocalDate.getDate()
d) LocalDate.of()

Answer:

a) LocalDate.parse()

Explanation:

LocalDate.parse() is used to parse a string and return a


Comments