Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Beginning Java with NetBeans: Chapter 15 How to work with dates and times MULTIP

ID: 3706271 • Letter: B

Question

Beginning Java with NetBeans: Chapter 15

How to work with dates and times

MULTIPLE CHOICE [Answers are in tables – delete all but the correct answer’s cell]

1. When compared with the new date/time API that was introduced with Java 8, what is an advantage of the old date/time API?

a. It’s thread-safe.

b. It’s intuitive.

c. It’s easier to localize for parts of the world that don’t use the Gregorian calendar.

d. It works with versions of Java prior to Java 8.

2. What package stores the LocalDateTime class?

a. java.util

b. java.text

c. java.time

d. java.date

3. Which of the following is true about a LocalDate object?

a. It represents a date only.

b. It works from the observer’s perspective.

c. It represents represents a time only

d. both a and b

4. The following statement creates a LocalDateTime object that’s set to what date and time?

LocalDateTime startTime = LocalDateTime.of(1969, 2, 18, 5, 10);

a. January 18, 1969 5:10 PM

b. January 17, 1969 5:10 AM

c. February 17, 1969 5:10 AM

d. February 18, 1969 5:10 AM

e. February 18, 1969 5:10 PM

5. If the current time is 5pm, what is printed to the console?

a. early

b. late

c. on time

d. nothing

6. Which of the following statements gets the month from a LocalDate object named now?

a. int month = now.getMonth();

b. int month = now.getMonthInt();

c. Month month = now.getMonth();

d. Month month = now.getMonthInt();

7. Given a LocalDate object named date, which of the following statements changes the day of the month to the 12th?

a. LocalDate newDate = date.withDayOfMonth(12);

b. LocalDate newDate = date.setDayOfMonth(12);

c. LocalDate newDate = date.withDay(12);

d. LocalDate newDate = date.setDay(12);

8. Given a LocalDateTime object named now, which of the following statements gets a new LocalDateTime object that’s four weeks in the future?

a. LocalDateTime newDateTime = now.incrementMonth();

b. LocalDateTime newDateTime = now.decrementMonth();

c. LocalDateTime newDateTime = now.plusMonths(1);

d. LocalDateTime newDateTime = now.minusMonths(1);

9. What are the month and year for date2 after the statements that follow are executed?

a. January 1997

b. January 1998

c. February 1997

d. February 1998

10. What does the following code print to the console?

LocalDate date = LocalDate.of(1976, Month.JANUARY, 1); DateTimeFormatter dtf = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM); System.out.println(dtf.format(date));

a. 01/01/76

b. 01/01/1976

c. Jan 1, 1976

d. 1976-01-01

11. What does the following code print to the console?

LocalDateTime dateTime = LocalDateTime.of(2002, 3, 2, 23, 15, 30); DateTimeFormatter dtf =

DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT); System.out.println(dtf.format(dateTime));

a. 3/2/02

b. 3/2/2002

c. 11:15 PM

d. 11:15:30 PM

e. 23:15:30

COMPLETION

The _______________ class is a combination of the LocalDate and LocalTime classes.

The _____________ enumeration contains the months in the year (JANUARY through DECEMBER).

You can use the methods of the _____________ class to get a formatted string for a LocalDate or LocalTime object.

Explanation / Answer

Below are your answers. Most of these are pretty straight forwards, So no explanation is needed for them. I’ll mention the explanation wherever applicable.

Explanation: - New Java API is thread safe and it is easier to localize. But to use it with Prior versions we still need the old API methods

2. c. java.time

3. d. both a and b

Explanation: - LocalDate represents only date while for time we can use LocalTime. Both shows the date and time as per context of the observer.

4. d. February 18, 1969 5:10 AM

              Explanation: - the method signature of LocalDateTime.of is

public static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute) and hour is 24 hr clock.

5. b. late

            Explanation: - 5 pm is after 3.30 pm as specified by starttime, So compareTo will return value greater than 0. So it will print late.

6. c. Month month = now.getMonth();

7. a. LocalDate newDate = date.withDayOfMonth(12);

8. c. LocalDateTime newDateTime = now.plusMonths(1);

9. d. February 1998

Explanation: - plusMonths will add two months to 1st December 1997. So d) is the answer

10. c. Jan 1, 1976

11. c. 11:15 PM

LocalDateTime is the combination of LocalDate and LocalTime classes

The Month enumeration contains the months in the year (JANUARY through DECEMBER).

You can use the methods of the DateTimeFormatter class to get a formatted string for a LocalDate or LocalTime object.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote