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


Explain what type of method reference is


Download 1.52 Mb.
Pdf ko'rish
bet88/171
Sana07.01.2023
Hajmi1.52 Mb.
#1081886
1   ...   84   85   86   87   88   89   90   91   ...   171
Bog'liq
Core JAVA Interview Questions Youll M...

Explain what type of method reference is
String::toUpperCase.
Answer:
String::toUpperCase is an example of arbitrary method
reference. It refers to the toUpperCase method of the String
class but not on any specific object. This is generally used while
iterating through a Collection or a Stream. For example, suppose
you want to convert all the elements of a List to uppercase. You
can use this String::toUppercase as follows:
List colours =
Arrays.asList(“Red”,”Blue”,”Green”);
List upperCaseColours =
colours.stream().map(String::toUpperCase).collect(Collec
tors.toList());



232.
233.
CHAPTER
05
DateTime API
Explain what is wrong in the following code snippet and
how you can fix it.
LocalDate date1 = LocalDate.of(2018, 7, 24);
LocalTime time1 = date1.atTime(5, 30);
Answer:
The code snippet above first creates a LocalDate called
date1. It then uses the atTime() method on date1 and
assigns the result to a LocalTime object called time1. This
causes a compilation error. The atTime() method combines the
date in the current time object with the specified time and returns
a LocalDateTime object. So, you can fix the code above by
making the following change:
LocalDateTime time1 = date1.atTime(5, 30);
This combines the date specified in date1 with the time 5:30 and
returns a LocalDateTime object.
Explain with appropriate code samples the different ways
in which you can create a LocalDate.
Answer:
The LocalDate class has some static methods using for
LocalDate creation. These are as follows:


a.
b.
c.
234.
Creating a LocalDate corresponding to the current Date– The
LocalDate.now() method can be used for this as shown
below:
LocalDate date1 = LocalDate.now();
Creating a LocalDate corresponding to the specified year,
month and day– The LocalDate.of method can be used
for this as shown below:
LocalDate date2 = LocalDate.of(2018, 7, 24);
Creating a LocalDate corresponding to a String Date– The
LocalDate.parse can be used for this as shown below:
LocalDate date3 = LocalDate.parse(“2014–08–11”);

Download 1.52 Mb.

Do'stlaringiz bilan baham:
1   ...   84   85   86   87   88   89   90   91   ...   171




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