SlideShare ist ein Scribd-Unternehmen logo
1 von 14
Downloaden Sie, um offline zu lesen
Java 8 Date and Time API - Quiz
GANESH & HARI
CODEOPS TECHNOLOGIES
ganesh@codeops.tech
hari@codeops.tech
Quiz Question #1
Choose the correct option based on this code segment:
LocalDate babyDOB = LocalDate.of(2015, Month.FEBRUARY, 20);
LocalDate now = LocalDate.of(2016, Month.APRIL, 10);
System.out.println(Period.between(now, babyDOB).getYears()); //
PERIOD_CALC
A. The code segment results in a compiler error in the line marked with the
comment PERIOD_CALC
B. The code segment throws a DateTimeException
C. The code segment prints: 1
D. The code segment prints: -1
Quiz Question #1 - Answer
The code segment prints: - 1
Here are the arguments to the between() method in the Period class:
Period between(LocalDate startDateInclusive, LocalDate
endDateExclusive)
The ïŹrst argument is the start and the second argument is the end, and
hence the call Period.between(now, babyDOB) results in -1 (not +1).
Quiz Question #2
Which one of the following classes is best suited for storing
timestamp values of application events in a ïŹle?
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
Quiz Question #2 - Answer
C . Instant class
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 ïŹle 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.
Quiz Question #3
Given this code segment:
ZoneId zoneId = ZoneId.of("Asia/Singapore");
ZonedDateTime zonedDateTime =
ZonedDateTime.of(LocalDateTime.now(), zoneId);
System.out.println(zonedDateTime.getOffset());
assume that the time-offset value for the Asia/Singapore time zone from UTC/Greenwich is
+08:00. Choose the correct option.
A. This code segment results in throwing DateTimeException
B. This code segment results in throwing UnsupportedTemporalTypeException
C. The code segment prints: Asia/Singapore
D. The code segment prints: +08:00
E. This code segment prints: +08:00 [Asia/Singapore]
Quiz Question #3 - Answer
D. The code segment prints: + 08:00
Given a ZonedDateTime object, the getOffset() method returns a
ZoneOffset object that corresponds to the offset of the time zone from
UTC/Greenwich. Given that the time-offset value for the Asia/
Singapore zone from UTC/Greenwich is +08:00, the toString() method of
ZoneOffset prints the string “+08:00” to the console.
Quiz Question #4
Choose the correct option based on this code segment:
DateTimeFormatter dateFormat = DateTimeFormatter.ISO_DATE; // DEF
LocalDate dateOfBirth = LocalDate.of(2015, Month.FEBRUARY, 31);
System.out.println(dateFormat.format(dateOfBirth)); // USE
A. The program gives a compiler error in the line marked with the comment DEF
B. The program gives a compiler error in the line marked with the comment USE
C. The code segment prints: 2015-02-31
D. The code segment prints: 2015-02-03
E. This code segment throws java.time.DateTimeException with the message "Invalid
date 'FEBRUARY 31'"
Quiz Question #4 - Answer
E. This code segment throws java.time.DateTimeException with the
message "Invalid date 'FEBRUARY 31'" .
The date value 31 passed in the call LocalDate.of(2015, 2, 3 1); is invalid
for the month February, and hence the of() method in the LocalDate class
throws DateTimeException.
One of the predeïŹned values in DateTimeFormatter is ISO_DATE.
Hence, it does not result in a compiler error for the statement marked
with the comment DEF. The statement marked with the comment USE
compiles without errors because it is the correct way to use the format()
method in the DateTimeFormatter class.
Quiz Question #5
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
Quiz Question #5 - Answer
B. Friday
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.
Meetups
h"p://www.meetup.com/JavaScript-Meetup-Bangalore/	
h"p://www.meetup.com/Container-Developers-Meetup-Bangalore/		
h"p://www.meetup.com/So>ware-Cra>smanship-Bangalore-Meetup/	
h"p://www.meetup.com/Core-Java-Meetup-Bangalore/	
h"p://www.meetup.com/Technical-Writers-Meetup-Bangalore/	
h"p://www.meetup.com/CloudOps-Meetup-Bangalore/	
h"p://www.meetup.com/Bangalore-SDN-IoT-NetworkVirtualizaHon-Enthusiasts/	
h"p://www.meetup.com/So>wareArchitectsBangalore/
Upcoming Bootcamps
Modern Software Architecture (10th Sept, BLR)
SOLID Principles and Design Patterns (27th August, BLR)
Azure Hands-on (27th August, BLR)
ganesh@codeops.tech @GSamarthyam
www.codeops.tech slideshare.net/sgganesh
+91 98801 64463 bit.ly/ganeshsg

Weitere Àhnliche Inhalte

Mehr von Ganesh Samarthyam

Mehr von Ganesh Samarthyam (20)

Wonders of the Sea
Wonders of the SeaWonders of the Sea
Wonders of the Sea
 
Animals - for kids
Animals - for kids Animals - for kids
Animals - for kids
 
Applying Refactoring Tools in Practice
Applying Refactoring Tools in PracticeApplying Refactoring Tools in Practice
Applying Refactoring Tools in Practice
 
CFP - 1st Workshop on “AI Meets Blockchain”
CFP - 1st Workshop on “AI Meets Blockchain”CFP - 1st Workshop on “AI Meets Blockchain”
CFP - 1st Workshop on “AI Meets Blockchain”
 
Great Coding Skills Aren't Enough
Great Coding Skills Aren't EnoughGreat Coding Skills Aren't Enough
Great Coding Skills Aren't Enough
 
College Project - Java Disassembler - Description
College Project - Java Disassembler - DescriptionCollege Project - Java Disassembler - Description
College Project - Java Disassembler - Description
 
Coding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean CodeCoding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean Code
 
Design Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on ExamplesDesign Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on Examples
 
Bangalore Container Conference 2017 - Brief Presentation
Bangalore Container Conference 2017 - Brief PresentationBangalore Container Conference 2017 - Brief Presentation
Bangalore Container Conference 2017 - Brief Presentation
 
Bangalore Container Conference 2017 - Poster
Bangalore Container Conference 2017 - PosterBangalore Container Conference 2017 - Poster
Bangalore Container Conference 2017 - Poster
 
Software Design in Practice (with Java examples)
Software Design in Practice (with Java examples)Software Design in Practice (with Java examples)
Software Design in Practice (with Java examples)
 
OO Design and Design Patterns in C++
OO Design and Design Patterns in C++ OO Design and Design Patterns in C++
OO Design and Design Patterns in C++
 
Bangalore Container Conference 2017 - Sponsorship Deck
Bangalore Container Conference 2017 - Sponsorship DeckBangalore Container Conference 2017 - Sponsorship Deck
Bangalore Container Conference 2017 - Sponsorship Deck
 
Let's Go: Introduction to Google's Go Programming Language
Let's Go: Introduction to Google's Go Programming LanguageLet's Go: Introduction to Google's Go Programming Language
Let's Go: Introduction to Google's Go Programming Language
 
Google's Go Programming Language - Introduction
Google's Go Programming Language - Introduction Google's Go Programming Language - Introduction
Google's Go Programming Language - Introduction
 
Java Generics - Quiz Questions
Java Generics - Quiz QuestionsJava Generics - Quiz Questions
Java Generics - Quiz Questions
 
Java Generics - by Example
Java Generics - by ExampleJava Generics - by Example
Java Generics - by Example
 
Software Architecture - Quiz Questions
Software Architecture - Quiz QuestionsSoftware Architecture - Quiz Questions
Software Architecture - Quiz Questions
 
Docker by Example - Quiz
Docker by Example - QuizDocker by Example - Quiz
Docker by Example - Quiz
 
Core Java: Best practices and bytecodes quiz
Core Java: Best practices and bytecodes quizCore Java: Best practices and bytecodes quiz
Core Java: Best practices and bytecodes quiz
 

KĂŒrzlich hochgeladen

Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
chiefasafspells
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Bert Jan Schrijver
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
Abortion Pills In Pretoria ](+27832195400*)[ đŸ„ Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ đŸ„ Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ đŸ„ Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ đŸ„ Women's Abortion Clinic In Pre...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
shinachiaurasa2
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
masabamasaba
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
masabamasaba
 

KĂŒrzlich hochgeladen (20)

Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Abortion Pills In Pretoria ](+27832195400*)[ đŸ„ Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ đŸ„ Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ đŸ„ Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ đŸ„ Women's Abortion Clinic In Pre...
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
 

Java 8 Date and Time API - Quiz

  • 1. Java 8 Date and Time API - Quiz GANESH & HARI CODEOPS TECHNOLOGIES ganesh@codeops.tech hari@codeops.tech
  • 2. Quiz Question #1 Choose the correct option based on this code segment: LocalDate babyDOB = LocalDate.of(2015, Month.FEBRUARY, 20); LocalDate now = LocalDate.of(2016, Month.APRIL, 10); System.out.println(Period.between(now, babyDOB).getYears()); // PERIOD_CALC A. The code segment results in a compiler error in the line marked with the comment PERIOD_CALC B. The code segment throws a DateTimeException C. The code segment prints: 1 D. The code segment prints: -1
  • 3. Quiz Question #1 - Answer The code segment prints: - 1 Here are the arguments to the between() method in the Period class: Period between(LocalDate startDateInclusive, LocalDate endDateExclusive) The ïŹrst argument is the start and the second argument is the end, and hence the call Period.between(now, babyDOB) results in -1 (not +1).
  • 4. Quiz Question #2 Which one of the following classes is best suited for storing timestamp values of application events in a ïŹle? 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
  • 5. Quiz Question #2 - Answer C . Instant class 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 ïŹle 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.
  • 6. Quiz Question #3 Given this code segment: ZoneId zoneId = ZoneId.of("Asia/Singapore"); ZonedDateTime zonedDateTime = ZonedDateTime.of(LocalDateTime.now(), zoneId); System.out.println(zonedDateTime.getOffset()); assume that the time-offset value for the Asia/Singapore time zone from UTC/Greenwich is +08:00. Choose the correct option. A. This code segment results in throwing DateTimeException B. This code segment results in throwing UnsupportedTemporalTypeException C. The code segment prints: Asia/Singapore D. The code segment prints: +08:00 E. This code segment prints: +08:00 [Asia/Singapore]
  • 7. Quiz Question #3 - Answer D. The code segment prints: + 08:00 Given a ZonedDateTime object, the getOffset() method returns a ZoneOffset object that corresponds to the offset of the time zone from UTC/Greenwich. Given that the time-offset value for the Asia/ Singapore zone from UTC/Greenwich is +08:00, the toString() method of ZoneOffset prints the string “+08:00” to the console.
  • 8. Quiz Question #4 Choose the correct option based on this code segment: DateTimeFormatter dateFormat = DateTimeFormatter.ISO_DATE; // DEF LocalDate dateOfBirth = LocalDate.of(2015, Month.FEBRUARY, 31); System.out.println(dateFormat.format(dateOfBirth)); // USE A. The program gives a compiler error in the line marked with the comment DEF B. The program gives a compiler error in the line marked with the comment USE C. The code segment prints: 2015-02-31 D. The code segment prints: 2015-02-03 E. This code segment throws java.time.DateTimeException with the message "Invalid date 'FEBRUARY 31'"
  • 9. Quiz Question #4 - Answer E. This code segment throws java.time.DateTimeException with the message "Invalid date 'FEBRUARY 31'" . The date value 31 passed in the call LocalDate.of(2015, 2, 3 1); is invalid for the month February, and hence the of() method in the LocalDate class throws DateTimeException. One of the predeïŹned values in DateTimeFormatter is ISO_DATE. Hence, it does not result in a compiler error for the statement marked with the comment DEF. The statement marked with the comment USE compiles without errors because it is the correct way to use the format() method in the DateTimeFormatter class.
  • 10. Quiz Question #5 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
  • 11. Quiz Question #5 - Answer B. Friday 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.
  • 13. Upcoming Bootcamps Modern Software Architecture (10th Sept, BLR) SOLID Principles and Design Patterns (27th August, BLR) Azure Hands-on (27th August, BLR)