Java Date Time Quiz - MCQ - Multiple Choice Questions



This post contains a few useful Java date time multiple-choice questions to self-test your Java 8 date time API's knowledge.

The answer and explanation have been given for each MCQ. 

1. Which of the following are valid ways to create a LocalDate object?

A.
LocalDate.of(2014);

B.

LocalDate.with(2014, 1, 30);

C.

LocalDate.of(2014, 0, 30);

D.

LocalDate.now().plusDays(5);

Answer

The correct answer is D.

Explanation

Option D is valid. The methods now() and plusDays() are valid and can be chained.

2. Which of the following are valid ChronoUnit values for LocalTime?

A. YEAR

B. NANOS

C. DAY

D. HALF_DAYS

Answer

The correct answers are B and D.

Explanation

LocalTime doesn't store information about years and (complete) days.

3. Which one of the following classes is best suited for storing timestamp values of application events in a file? 

A. java.time.ZoneId class 

B. java.time.ZoneOffset class 

C. java.time.Instant class 

D. java.time.Duration class 

E. java.time.Period class

Answer

C. Instant class

Explanation

The Instant class stores the number of seconds elapsed since the start of the Unix epoch (1970-01-01T00:00:00Z). 

The Instant class is suitable for storing a log of application events in a file as timestamp values. 

The ZoneId and ZoneOffset classes are related to time zones and hence are unrelated to storing timestamp values. 

The Duration class is for time-based values in terms of quantity of time (such as seconds, minutes, and hours). 

The Period class is for date-based values such as years, months, and days

4. Consider this code segment?

DateTimeFormatter formatter =
DateTimeFormatter.ofPattern("EEEE", Locale.US);
System.out.println(formatter.format(LocalDateTime.now()));
Which of the following outputs matches the string pattern "EEEE" given in this code segment? 

A. F 

B. Friday 

C. Sept 

D. September

Answer

B. Friday

Explanation

E is the day name in the week; the pattern "EEEE" prints the name of the day in its full format. 

“Fri” is a short form that would be printed by the pattern "E", but "EEEE" prints the day of the week in full form: for example, “Friday”. Because the locale is Locale.US, the result is printed in English. 

The output “Sept” or “September” is impossible because E refers to the name in the week, not in a month.

5. Which of the following are valid ChronoField values for LocalDate?

A. DAY_OF_WEEK

B. HOUR_OF_DAY

C. DAY_OF_MONTH

D. MILLI_OF_SECOND

Answer

The correct answers are A and C.

Explanation

A LocalDate stores the year, month, days, and related information. It doesn't store hours or milliseconds.

6. Which one of the following statements will compile without any errors?

a)

Supplier<LocalDate> now = LocalDate::now();
b)
Supplier<LocalDate> now = () -> LocalDate::now;

c)

String now = LocalDate::now::toString;

d)

Supplier<LocalDate> now = LocalDate::now;

Answer

d)
Supplier<LocalDate> now = LocalDate::now;

Explanation

The now() method defined in LocalDate does not take any arguments and returns a LocalDate object. Hence, the signature of the now() method matches that of the only abstract method in the Supplier interface: T get(). Hence, the method reference Local::now can be assigned to Supplier and the statement compiles without any errors. 

Other options show improper use of method reference and they will result in compiler error(s). 

Related Posts

  1. Java String Quiz
  2. Java Arrays Quiz
  3. Java Loops Quiz
  4. Java OOPS Quiz
  5. Java OOPS Quiz - Part 1
  6. Java OOPS Quiz - Part 2
  7. Java Exception Handling Quiz
  8. Java Collections Quiz
  9. Java Generics Quiz
  10. Java Multithreading Quiz
  11. JDBC Quiz
  12. Java Lambda Expressions Quiz
  13. Java Functional Interfaces Quiz
  14. Java Streams API Quiz
  15. Java Date Time Quiz
  16. Java 8 Quiz

Free Spring Boot Tutorial - 5 Hours Full Course


Watch this course on YouTube at Spring Boot Tutorial | Fee 5 Hours Full Course

Comments