OCJP Practice Papers - Date/Time API
OCJP Practice Papers Date/Time API include following topics
- Create and manage date-based and time-based events including a combination ofdate and time into a single object using LocalDate, LocalTime, LocalDateTime,Instant, Period, and Duration
- Work with dates and times across timezones and manage changes resulting fromdaylight savings including Format date and times values
- Define and create and manage date-based and time-based events using Instant,Period, Duration, and TemporalUnit
See the complete syllabus for OCJP here
1. What package is the LocalTime class in?
A. java.date
B. java.lang
C. java.time
D. java.util
2. How many of the classes Duration, LocalDateTime, and LocalTime have the concept ofa time zone?
A. None
B. One
C. Two
D. Three
3. Which class has a getSeconds() method?
A. Only the Duration class
B. Only the Period class
C. Both the Duration and Period classes
D. Neither class
4. Which of these represents the earliest date/time?
A. 2017-02-15T03:00+01:00[Europe/Berlin]
B. 2017-02-15T04:00+02:00[Europe/Helsinki]
C. 2017-02-15T05:00+01:00[Europe/Warsaw]
D. None of the above. We have a tie.
5. Most of the United States observes daylight savings time on March 12, 2017, bymoving the clocks forward an hour at 2 a.m. What does the following code output?
LocalDate localDate = LocalDate.of(2017, 3, 12);
LocalTime localTime = LocalTime.of(1, 0);
ZoneId zone = ZoneId.of("America/New_York");
ZonedDateTime z = ZonedDateTime.of(localDate, localTime, zone);
Duration duration = Duration.ofHours(3);
ZonedDateTime later = z.plus(duration);
System.out.println(later.getHour());
A. 4
B. 5
C. 6
D. None of the above
6. What does the following output?
int year = 1874;
int month = Month.MARCH;
int day = 24;
LocalDate date = LocalDate.of(year, month, day);
System.out.println(date.isBefore(LocalDate.now()));
A. false
B. true
C. The code does not compile.
D. The code compiles but throws an exception at runtime.
7. Which correctly fills in the blank to print 2017-01-15?
LocalDate hatDay = LocalDate.of(2017, Month.JANUARY, 15);
DateTimeFormatter f = DateTimeFormatter.ISO_DATE;
System.out.println(_______________________);
I. f.format(hatDay)
II. f.formatDate(hatDay)
III. hatDay.format(f)
A. I
B. III
C. I and III
D. II and III
8. Which of the answer choices is true given the following?
2017-01-07T10:00-07:00[America/Phoenix]
2017-01-07T08:00-08:00[America/Vancouver]
A. The first date/time is one hour earlier than the second.
B. The first date/time is three hours earlier than the second.
C. The first date/time is one hour later than the second.
D. The first date/time is three hours later than the second.
9. Given that daylight savings time starts on March 12, 2017, at 2 a.m. and clocks jump
from 1:59 a.m. to 03:00 a.m., which of the following can fill in the blank so the code
doesnt throw an exception?
LocalDate localDate = LocalDate.of(2017, 3, 12);
LocalTime localTime = LocalTime.of(__________);
ZoneId zone = ZoneId.of("America/New_York");
ZonedDateTime z = ZonedDateTime.of(localDate, localTime, zone);
A. 2, 0
B. 3, 0
C. Either of the above will run without throwing an exception.
D. Both of these will cause an exception to be thrown.
10. What is the result of the following?
11: LocalDate waffleDay = LocalDate.of(2017, Month.MARCH, 25);
12: Period period = Period.of(1, 6, 3);
13: LocalDate later = waffleDay.plus(period);
14: later.plusDays(1);
15: LocalDate thisOne = LocalDate.of(2018, Month.SEPTEMBER, 28);
16: LocalDate thatOne = LocalDate.of(2018, Month.SEPTEMBER, 29);
17: System.out.println(later.isBefore(thisOne) + " "
18: + later.isBefore(thatOne));
A. false false
B. false true
C. true true
D. The code does not compile.
11. What is a possible result of the following?
LocalDate montyPythonDay = LocalDate.of(2017, Month.MAY, 10);
LocalDate aprilFools = LocalDate.of(2018, Month.APRIL, 1);
Duration duration = Duration.ofDays(1);
LocalDate result = montyPythonDay.minus(duration);
System.out.println(result + " " + aprilFools.isBefore(result));
A. 2017-05-09 false
B. 2017-05-09 true
C. The code does not compile.
D. None of the above
12. What is the result of running this code?
12: LocalDate pieDay = LocalDate.of(2017, Month.JANUARY, 23);
13: LocalTime midnight = LocalTime.of(0, 0);
14: LocalDateTime pieTime = LocalDateTime.of(pieDay, midnight);
15:
16: DateTimeFormatter f = DateTimeFormatter
17: .ofLocalizedDate(FormatStyle.SHORT);
18: f.format(pieDay);
19: f.format(pieTime);
20: f.format(midnight);
A. The code runs successfully.
B. The code throws an exception on line 19.
C. The code throws an exception on line 20.
D. The code does not compile.
13. In the United States, daylight savings time ends on November 5th, 2017 at 02:00 a.m.
and we repeat the previous hour. What is the output of the following?
import java.time.*;
public class FallBack {
public static void main(String[] args) {
LocalDate localDate = LocalDate.of(2017, Month.NOVEMBER, 5);
LocalTime localTime = LocalTime.of(1, 0);
ZoneId zone = ZoneId.of("America/New_York");
ZonedDateTime z = ZonedDateTime.of(localDate, localTime, zone);
for (int i = 0; i 6; i++)
z.plusHours(1);
System.out.println(z.getHour());
}
}
A. 5
B. 6
C. 7
D. None of the above
14. What format pattern would you pass to a DateTimeFormatter so it creates hour and
minute output such as 02:33?
A. HH:MM
B. HH:mm
C. hh:MM
D. hh:mm
15. LocalTime.of() has a number of overloads. Which of the following is not one of
them?
A. LocalTime.of(int hour, int minute)
B. LocalTime.of(int hour, int minute, int second)
C. LocalTime.of(int hour, int minute, int second, int nanoOfSecond)
D. LocalTime.of(int hour, int minute, int second, int nanoOfSecond, int
picoSeconds)
16. How many of the classes LocalDate, Period, and ZonedDate have a method to get
the year?
A. None
B. One
C. Two
D. Three
17. Which statement is not true about these two variables?
Duration duration = Duration.ofDays(1);
Period period = Period.ofDays(1);
A. Both output the same value when calling toString().
B. The Duration object compiles because durations are for smaller units of time.
C. The Period object compiles because periods are for larger units of time.
D. None of the above
18. What is a possible output of this code?
LocalTime time = LocalTime.of(1,2,3,4);
System.out.println(time);
A. 01:02:03.4
B. 01:02:03.000000004
C. 01/01/1970 01:02:03.4
D. 01/01/1970 01:02:03.000000004
19. What does the following print?
import java.time.*;
import java.time.format.*;
public class PolarBear {
public static void main(String[] args) {
LocalDate polarBearDay = LocalDate.of(2017, 2, 27);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy dd
MMM");
System.out.println(polarBearDay.format(formatter));
}
}
A. 2017 27 Jan
B. 2017 27 Feb
C. 2017 Jan 27
D. 2017 Feb 27
20. Which contains a constant named HOURS?
A. ChronoUnit
B. Duration
C. Instant
D. Period
21. The United States observes daylight savings time on March 12, 2017, by moving the
clocks forward an hour at 2 a.m. What does the following code output?
LocalDate localDate = LocalDate.of(2017, 3, 12);
LocalTime localTime = LocalTime.of(13, 0);
ZoneId zone = ZoneId.of("America/New_York");
ZonedDateTime z = ZonedDateTime.of(localDate, localTime, zone);
Duration duration = Duration.ofHours(3);
ZonedDateTime later = z.plus(duration);
System.out.println(later.getHour());
A. 13
B. 16
C. 17
D. None of the above
22. What is a possible result of the following?
LocalDate montyPythonDay = LocalDate.of(2017, Month.MAY, 10);
LocalTime time = LocalTime.of(5, 40);
LocalDateTime dateTime = LocalDateTime.of(montyPythonDay, time);
Duration duration = Duration.ofDays(1);
LocalDateTime result = dateTime.minus(duration);
System.out.println(result);
A. 2017-05-09
B. 2017-05-09T05:40
C. 2017-05-10T05:40
D. None of the above
23. Which correctly fills in the blank to print 2017-01-15?
LocalDate hatDay = LocalDate.of(2017, Month.JANUARY, 15);
DateFormatter f = DateFormatter.ISO_DATE;
System.out.println(______________________________);
A. f.format(hatDate)
B. hatDay.format(f)
C. Both of the above
D. Neither of the above
24. LocalDateTime.of() has a number of overloads. Which of the following is not one
of them?
A. LocalDateTime.of(LocalDate date, LocalTime time)
B. LocalDateTime.of(LocalDate date, int hour, int minute)
C. LocalDateTime.of(int year, int month, int day, int hour, int minute)
D. LocalDateTime.of(int year, Month month, int day, int hour, int minute)
25. In the United States, daylight savings time for 2017 starts at 2 a.m. on March 12th and
ends at 2 a.m. on November 5th. Given the sequence in the following image, what
time comes next on March 12th, July 4th, and November 5th, respectively?
A. 01:00, 02:00, 01:00
B. 01:00, 02:00, 03:00
C. 03:00, 02:00, 01:00
D. 03:00, 02:00, 03:00
26. What is the output of the following?
LocalDate date1 = LocalDate.of(2017, Month.MARCH, 3);
LocalDate date2 = LocalDate.of(2017, Month.FEBRUARY, 31);
System.out.println(date1.equals(date2));
A. false
B. true
C. The code does not compile.
D. The code compiles but throws an exception at runtime.
27. Given this date/time and time zone offset, what time is it in GMT?
2017-03-09T16:00-10:00[US/Hawaii]
A. 02:00
B. 04:00
C. 06:00
D. 10:00
28. What is a possible output of the following?
LocalDate trainDay = LocalDate.of(2017, 5, 13);
LocalTime time = LocalTime.of(10, 0);
ZoneId zone = ZoneId.of("America/Los_Angeles");
ZonedDateTime zdt = ZonedDateTime.of(trainDay, time, zone);
Instant instant = zdt.toInstant();
instant = instant.plus(1, ChronoUnit.DAYS);
System.out.println(instant);
A. 2017-05-13T10:00-07:00[America/Los_Angeles]
B. 2017-05-13T17:00:00Z
C. 2017-05-14T10:00-07:00[America/Los_Angeles]
D. 2017-05-14T17:00:00Z
29. What is the output of the following?
LocalDate date = LocalDate.of(2017, Month.JULY, 17);
LocalTime time = LocalTime.of(10, 0);
ZoneId zone = ZoneId.of("America/New_York");
ZonedDateTime iceCreamDay = ZonedDateTime.of(date, time, zone);
time = time.plusMonths(1);
System.out.println(iceCreamDay.getMonthValue());
A. 6
B. 7
C. 8
D. The code does not compile.
30. What does the following print?
import java.time.*;
import java.time.format.*;
public class PolarBear {
public static void main(String[] args) {
LocalDate polarBearDay = LocalDate.of(2017, 2, 27);
DateTimeFormatter formatter = DateTimeFormatter
.ofPattern("Holiday: yyyy dd MMM");
System.out.println(polarBearDay.format(formatter));
}
}
A. Holiday: 2017 27 Jan
B. Holiday: 2017 27 Feb
C. The code does not compile.
D. The code compiles but throws an exception at runtime.
31. Which of these represents the earliest date/time?
A. 2017-02-15T16:00+07:00[Asia/Bangkok]
B. 2017-02-15T18:00+04:00[Asia/Dubai]
C. 2017-02-15T20:00+08:00[Asia/Kuala_Lumpur]
D. None of the above. We have a tie.
32. What is the result of the following?
11: LocalDate waffleDay = LocalDate.of(2017, Month.MARCH, 25);
12: Period period = Period.ofYears(1).ofMonths(6).ofDays(3);
13: LocalDate later = waffleDay.plus(period);
14: later.plusDays(1); 15: LocalDate thisOne = LocalDate.of(2018,
Month.SEPTEMBER, 28);
16: LocalDate thatOne = LocalDate.of(2018, Month.SEPTEMBER, 29);
17: System.out.println(later.isBefore(thisOne) + " "
18: + later.isBefore(thatOne));
A. false false
B. false true
C. true true
D. The code does not compile.
33. How many of the following can fill in the blank so this code compiles and prints 31?
LocalDate xmas = LocalDate.of(2017, 12, 25);
LocalDate blackFriday = LocalDate.of(2017, 11, 24);
long shoppingDaysLeft =____________________ ;
System.out.println(shoppingDaysLeft);
I. blackFriday.until(xmas, ChronoUnit.DAYS)
II. blackFriday.until(xmas, TemporalUnit.DAYS)
III. ChronoUnit.DAYS.between(blackFriday, xmas)
IV. TemporalUnit.DAYS.between(blackFriday, xmas)
A. One
B. Two
C. Three
D. Four
34. How many of these classes cause a compiler error when filling in the blank:
LocalDate, LocalDateTime, LocalTime, ZonedDateTime?
private static String formatMe(__________ obj) {
DateTimeFormatter f =
DateTimeFormatter.ofLocalizedTime(FormatStyle.MEDIUM);
return f.format(obj);
}
A. None
B. One
C. Two
D. Three
35. What is the output of the following?
LocalDate date = LocalDate.of(2017, Month.JULY, 17);
LocalTime time = LocalTime.of(10, 0);
ZoneId zone = ZoneId.of("America/New_York");
ZonedDateTime iceCreamDay = ZonedDateTime.of(date, time, zone);
date = date.plusMonths(1);
System.out.println(iceCreamDay.getMonthValue());
A. 6
B. 7
C. 8
D. The code does not compile.
36. Which of the following can fill in the blank to make this code compile?
public boolean isItMyBirthday(LocalDateTime dateTime) {
______________________________
return now.getMonth() == dateTime.getMonth()
&& now.getDayOfMonth() == dateTime.getDayOfMonth();
}
A. LocalDate now = LocalDate.now();
B. LocalDate now = new LocalDate();
C. ZonedDate now = ZonedDate.now();
D. ZonedDate now = new ZonedDate();
37. What is the output of the following?
LocalDate date1 = LocalDate.of(2017, Month.MARCH, 3);
LocalDate date2 = date1.plusDays(2).minusDays(1).minusDays(1);
System.out.println(date1.equals(date2));
A. false
B. true
C. The code does not compile.
D. The code compiles but throws an exception at runtime.
38. What is a possible output of the following?
LocalDate date = LocalDate.of(2017, 5, 13);
LocalTime time = LocalTime.of(10, 0);
LocalDateTime trainDay = LocalDateTime.of(date, time);
Instant instant = trainDay.toInstant();
instant = instant.plus(1, ChronoUnit.DAYS);
System.out.println(instant);
A. 2017-05-14T10:00-07:00[America/Los_Angeles]
B. 2017-05-14T17:00:00Z
C. The code does not compile.
D. The code compiles but throws an exception at runtime.
39. What is the result of the following?
public class PiDay {
public static void main(String[] args) {
LocalDateTime pi = LocalDateTime.of(2017, 3, 14, 1, 59);
DateTimeFormatter formatter = DateTimeFormatter
.ofPattern("M.ddhhmm");
System.out.println(formatter.format(pi));
}
}
A. 3.140159
B. 59.140103
C. The code does not compile.
D. The code compiles but throws an exception at runtime.
40. Daylight savings time ends on November 5, 2017 at 2 a.m. when we repeat the hour.
Suppose we have a ZonedDateTime that outputs 2017-11-05T01:00-04:00[America/
New_York] when calling toString(). What is a possible value of the ZonedDateTime
obtained by adding an hour to this value?
A. 2017-11-05T01:00-04:00[America/New_York]
B. 2017-11-05T02:00-04:00[America/New_York]
C. 2017-11-05T01:00-05:00[America/New_York]
D. 2017-11-05T02:00-05:00[America/New_York]
OCJP Practice Test, Certification Path, Tips/Tricks
- Oracle (OCJP) Java Certification Exam
- Java Certification Tips And Tricks
- OCJP Practice Papers - Java Basics
- OCJP Practice Papers - Operators And Decision Constructs
- OCJP Practice Papers-A Advanced Java Class Design
- OCJP Practice Papers Creating And Using Arrays
- OCJP Practice Papers Using Loop Constructs
- OCJP Practice Papers - Generics And Collections
- OCJP Practice Papers - Lambda Built-In Functional Interfaces
- OCJP Practice Papers Java Class Design
- OCJP Practice Papers - Java Stream API
- OCJP Practice Papers - Exceptions And Assertions
- OCJP Practice Papers - Date/Time API
- OCJP Practice Papers - Java I/O Fundamentals
- OCJP Practice Papers - Working With Methods And Encapsulation
- OCJP Practice Papers - Working With Inheritance
- OCJP Practice Papers - Handling Exceptions
- OCJP Practice Papers - Selected Classes From Java API
- OCJP Practice Papers - Java File I/O Nio.2
- OCJP Practice Papers - Java Concurrency