Core java interview Questions You'll Most Likely Be Asked (Second Edition)


Explain how you can find out if a year is a leap year using


Download 1.52 Mb.
Pdf ko'rish
bet92/171
Sana07.01.2023
Hajmi1.52 Mb.
#1081886
1   ...   88   89   90   91   92   93   94   95   ...   171
Bog'liq
Core JAVA Interview Questions Youll M...

Explain how you can find out if a year is a leap year using
the new DateTime API.
Answer:


There is a method called isLeapYear() on the LocalDate class.
This can be used to check if a year is a leap year as
demonstrated by the code below:
LocalDate date1 = LocalDate.of(2020, 7, 24);
boolean isLeapYear = date1.isLeapYear();
This code creates a LocalDate called date1 and then invokes
the isLeapYear() method on date1. The isLeapYear()
method returns a boolean value. In this case, since date1
corresponds to the year 2020 which is a leap year, the
isLeapYear() method will return true.



242.


243.
CHAPTER
06
Static and Default Interface
Methods
What are static and default methods? Why were they
introduced by Java 8?
Answer:
Before Java 8, interfaces could only have abstract methods. So,
they could not have methods with method bodies. Java 8 allows
interfaces to have methods with method bodies as static and
default methods. These were added for the following reasons:
Default interface methods help to keep code backward
compatible. Prior to Java 8, if a new method was added to an
interface, all the classes that implement the interface needed
to be modified. Default methods overcome this restriction. So,
they allow you to add a method with a method body in the
interface. So, all implementing classes do not need to be
modified.
Static interface methods help to group together utility
methods. Static interface methods cannot be overridden in the
classes that implement the method. So, this avoids unwanted
results due of incorrect implementation and provides an
additional level of security.

Download 1.52 Mb.

Do'stlaringiz bilan baham:
1   ...   88   89   90   91   92   93   94   95   ...   171




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling