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


particular data type while the Consumer interface operates on


Download 1.52 Mb.
Pdf ko'rish
bet75/171
Sana07.01.2023
Hajmi1.52 Mb.
#1081886
1   ...   71   72   73   74   75   76   77   78   ...   171
Bog'liq
Core JAVA Interview Questions Youll M...


particular data type while the Consumer interface operates on
an input value
What is the output of the following code snippet?.
Predicate numberChecker = (num)–> num > 20;
int input = 10;
System.out.println(input+” greater than
20–”+numberChecker.test(input)); //Line 1
input = 40;
System.out.println(input+” greater than
20–”+numberChecker.test(input)); //Line 2
Answer:
The above code snippet uses the built–in functional interface
called Predicate. This interface accepts an argument of any
data type and returns a boolean value. So, it is basically used to
test a condition. Here, the code creates a Predicate called


202.
203.
numberChecker that accepts an integer value. It is implemented
via a lambda expression that checks whether the input number is
greater than 20. Line 1 applies the Predicate on the value 10
while Line 2 applies the Predicate on the value 40. So, the
code prints the following output:
10 greater than 20:false
40 greater than 20:true
Which in–built functional interface would you use to
convert a String to uppercase?
Answer:
There is a built–in functional interface called UnaryOperator. It
accepts an argument of any data type and returns a result of the
same data type. It has a method called apply that operates on
the input value. So, you can use this to convert a String to
uppercase. The following code demonstrates this:
UnaryOperator converter = str–>
str.toUpperCase();
System.out.println(converter.apply(“hello world”));
Here, the code creates a UnaryOperator implementation called
converter that operates on a String value. A lambda
expression is used that converts the String to uppercase. So, this
code prints the following output:
HELLO WORLD

Download 1.52 Mb.

Do'stlaringiz bilan baham:
1   ...   71   72   73   74   75   76   77   78   ...   171




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