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. YEARB. 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?
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).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? 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_WEEKB. 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
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
Related Posts
- Java String Quiz
- Java Arrays Quiz
- Java Loops Quiz
- Java OOPS Quiz
- Java OOPS Quiz - Part 1
- Java OOPS Quiz - Part 2
- Java Exception Handling Quiz
- Java Collections Quiz
- Java Generics Quiz
- Java Multithreading Quiz
- JDBC Quiz
- Java Lambda Expressions Quiz
- Java Functional Interfaces Quiz
- Java Streams API Quiz
- Java Date Time Quiz
- Java 8 Quiz
Comments
Post a Comment