1.
Choose the correct output of the following program
interface Drawable
{
void draw();
}
public class FunctionalInterface {
public static void main(String[] args)
{
() -> System.out.println("Drawing...");
}
}
A. Compile time error
B. Run time error
C. Drawing
D. None of these
Answer: A
2.What is the output of the following snippet
public class FunctionalInterfaceExample
{
public static void main(String[] args) {
Runnable nit = () -> System.out.println("Thread Started");
nit.run();
}
}
Answer: Thread Started
3.What is the output of the following code
interface Course
{
String courseName();
String facultyName();
String duration();
}
@FunctionalInterface
interface NIT extends Course
{
String nit(String courseName);
}
public class FunctionalInterfaceExample
{
public static void main(String[] args) {
NIT nit = (String course) -> course;
System.out.println(nit.nit("Java"));
}
}
Answer: Compile time error
4.What is the output of the following code
@FunctionalInterface
interface NIT
{
String nit(String courseName);
}
interface Course extends NIT
{
String courseName();
String facultyName();
String duration();
}
public class FunctionalInterfaceExample
{
public static void main(String[] args) {
NIT nit = (String course) -> course;
System.out.println(nit.nit("Java"));
}
}
Answer: Java
5.What is the output of the following code
public class FunctionalInterfaceExample
{
public static void main(String[] args)
{
Function<String, Boolean> fun = str -> str.startsWith("A");
System.out.println(fun.apply("Ankit"));
}
}
Answer: true
---------------------------------------------------------------------
6.What is the output of the following code
@FunctionalInterface
interface NIT
{
String get(String nit);
}
public class FunctionalInterfaceExample
{
public static void main(String[] args) {
NIT nit = (str) -> str +"Welcome to"; // str concatenate to "Welcome
to"
NIT nit1 = (str) -> str + "NARESHIT"; // str1 concatenate to "NARESHIT"
information("Hai ", nit); // HAI + Welcome to
information("Hai ", nit1); // HAI + NARESHIT
}
public static void information(String str, NIT obj) {
String result = obj.get(str);
System.out.println(result);
}
}
Answer:
Hai Welcome to
Hai NARESHIT
7.By using FunctionalInterface annotation following code is correct or not?
@FunctionalInterface
interface NIT
{
String myInterface(String a);
String newInterface();
}
Answer:
Compile time error �Invalid '@FunctionalInterface' annotation; NIT is not a
functional interface�, because functional interface allows only one abstract method
8.What is the output of the following code
@FunctionalInterface
interface NIT{
int square(int x);
default int add(int a, int b){
return a+b;
}
default int sub(int a, int b){
return a-b;
}
static int multiply(int a, int b){
return a*b;
}
static int divide(int a, int b){
return a/b;
}
}
public class FunctionalInterfaceExample implements NIT{
public static void main(String[] args){
int a = 25;
int b = 5;
FunctionalInterfaceExample fun = new FunctionalInterfaceExample();
int add = fun.add(a,b);
int sub = fun.sub(a,b);
int mul = NIT.multiply(a,b);
int div = NIT.divide(a,b);
System.out.println(add+" "+sub+" "+mul+" "+div+" "+fun.square(6));
}
@Override
public int square(int x) {
return x*x;
}
}
Answer:
30 20 125 5 36
9.What is the output of the following code
@FunctionalInterface
interface NIT{
int square(int x);
static int add(int a, int b){
return a+b;
}
static int sub(int a, int b){
return a-b;
}
static int multiply(int a, int b){
return a*b;
}
static int divide(int a, int b){
return a/b;
}
}
public class FunctionalInterfaceExample{
public static void main(String[] args){
int a = 25;
int b = 5;
int add = NIT.add(a,b);
int sub = NIT.sub(a,b);
int mul = NIT.multiply(a,b);
int div = NIT.divide(a,b);
System.out.print(add+" "+sub+" "+mul+" "+div+" ");
NIT sq = (x) ->
{
return x*x;
};
System.out.print(sq.square(6));
}
}
Answer:
30 20 125 5 36
10. public class FunctionalInterfaceExample{
public static void main(String[] args) {
Function<String, String> fun = str -> str.substring(2,5);
System.out.println(fun.apply("Hyderabad"));
}
}
Answer: der
11.By using FunctionalInterface annotation following code is correct or not?
@FunctionalInterface
interface NIT
{
String myInterface(String a);
default void nit()
{
}
default int newInterface(int a)
{
return a;
}
}
Answer:
Successfully executed, because functional interface contains multiple default and
static methods
12.Is there any error in following code?
@FunctionalInterface
interface NIT
{
String myInterface(String a); //abstract method
int hashCode(); //Object class methods
String toString();
}
Answer:
Successfully Compiled, functional interface allows any Object class methods
13.Is there any error in following code?
@FunctionalInterface
interface NIT
{
String myInterface(String a); //abstract method
@Override
String toString(); //Object class method
void wait();
void notify();
}
Answer:
Compiler error, in functional interface cannot override the final method from
Object.
14.What is the output of the following code
public class FunctionalInterfaceExample{
public static void main(String[] args) {
Predicate<Integer> grater = a -> a >15;
Predicate<Integer> less = a -> a <10;
boolean bool = grater.test(20);
boolean bool1 = less.test(5);
System.out.println(bool);
System.out.println(bool1);
}
}
Answer:
true
true
15.What is the output of the following code
public class FunctionalInterfaceExample{
public static void main(String[] args)
{
String str[]= {"Java","Python","Naresh","IT"};
Predicate<String> prediacte = p -> p.endsWith("va");
for(String s: str)
{
if(prediacte.test(s))
System.out.println(s);
}
}
}
Answer: Java
16.What is the output of the following code
public class FunctionalInterfaceExample{
public static void main(String[] args) {
List<String> li = Arrays.asList("Java","Python","Naresh","IT");
Predicate<String> predicate = s -> s.contains("a");
for(String s: li)
{
if(predicate.test(s))
System.out.println(s);
}
}
}
Answer:
Java
Naresh
17.What is the output of the following code?
@FunctionalInterface
interface NIT
{
int myInterface(int a);
}
@FunctionalInterface
interface NewInterface
{
void alpha(String a);
}
public class Java8 {
public static void main(String[] args) {
NIT nit = (a) -> a*a*a;
System.out.println(nit.myInterface(5));
NewInterface result = (a) -> System.out.println("Welcome to NareshIT");
result.alpha("");
}
}
Answer:
125
Welcome to NareshIT
18.What is the output of the following code?
public class Java8 {
public static void main(String[] args) {
Consumer<Integer> value = (a) -> System.out.println(a*3);
value.accept(5);
}
}
Answer:
15
19 What is the output of the following code
class Student
{
private int id;
private String name;
private String gender;
private int age;
public Student(int id, String name, String gender, int age)
{
this.id = id;
this.name = name;
this.gender = gender;
this.age = age;
}
@Override
public String toString()
{
return "[id=" + id + ", name=" + name + ", gender=" + gender + ", age=" +
age + "]";
}
}
public class FunctionalInterface
{
public static void main(String[] args)
{
Supplier<Student> stdSupplier = () -> new Student(1, "Virat", "Male", 24);
Student std = stdSupplier.get();
System.out.println(std);
}
}
Answer : [id= 1 ,name= Virat, gender= Male, age=24]
20) Generate the output of the following code
public class Lambda11
{
public static void main(String args[])
{
Supplier<Double> randomValue = () -> Math.random();
System.out.println(randomValue.get());
}
}
21. What is the output of the following code?
public class Java8 {
public static void main(String[] args) {
Consumer<String> value = (a) -> System.out.println("Naresh I
Technologies");
value.accept("");
}
}
Answer:
Naresh I Technologies
22. What is the output of the following code?
public class Java8 {
public static void main(String[] args) {
Consumer<String> value = (a) -> System.out.println(a.toLowerCase());
value.accept("NareshIT");
}
}
Answer:
naresh