Java 8 Coding Questions | Java 8 Progamming Questions 2024

This Session I have written, Those who are given the interview and were not able to write the Java 8 program can read this article and practice the program multiple times, which will clear your Java 8 programming interview, Like as: java 8 programming questions or  java 8 coding examples or java 8 sample coding questions or Coding Based Java 8 Coding Interview Question or java 8 streams programs for interview or java 8 coding practice problems or java 8 coding interview questions for exrienced or java 8 coding interview questions for freshers.

Table of Contents

1). Write a program of functional Interface in java 8.  or  Example of functional interface in java 8  or Java 8 Functional Interface Example .
    @FunctionalInterface

interface Addition {

public int divide(int a, int b);

}

public class FunctionalInterface_Example {

public static void main(String[] args) {

Addition add = (a, b) -> (a / b);

System.out.println(add.divide(20, 10));

}}



2). Whrite a program of lambda expression in Java 8 ?  or  What is lambda expression in Java 8 program Example ? or  Write a Lambda Function example in Java 8 or Write a Lambda Function example

     @FunctionalInterface

interface Addition {

public int divide(int a, int b);

}


public class Lambda_Example {

public static void main(String[] args) {

Addition add = (a, b) -> (a / b);

System.out.println(add.divide(20, 10));

}}


3). Write a program of Default method in Java 8 ? or Java Default Method Example or  What is Default method in Java 8 ?

     @FunctionalInterface

interface xyz {

public void show();

public default void show1() {

System.out.println(“It is default method and body of thid mehtod already defined”);

}

}


public class Default implements xyz {

@Override

public void show() {

System.out.println(“this is abstract mehtod of XYZ interface”);

}

public static void main(String[] args) {

Default d1 = new Default();

d1.show();

d1.show1();

}}


4). How to handle Mutliple Inheritance by Default method ? or Mutlipe Inheritance in Java 8 Default method.

    interface Poet{

default void write() {

System.out.println(“Poet writing a Poem”);

}

}


interface Writer{

default void write() {

System.out.println(“Writer writing a Poem”);

}

}


public class MultipleIneritance_DefaultMethod implements Poet,Writer {

@Override

public void write() {

// TODO Auto-generated method stub

Poet.super.write(); // it will print Poet method

}

public static void main(String[] args) {

MultipleIneritance_DefaultMethod md= new MultipleIneritance_DefaultMethod();

md.write();

}

}

5). Write a Java 8 Static method Program ? or Write a Static method program in Java 8 ? or Java 8 Static method example ?

    @FunctionalInterface

interface X {

public void x();

public static void showX() {

System.out.println(“Hi this is static method”);

}

}


public class StaticMethod_Example implements X {

@Override

public void x() {

System.out.println(“Abstract method of X interface”);

}

public static void main(String[] args) {

X.showX();// call by X interface name directly but can’t override

}

}


6). Write a Predicate interface program in java 8 ? or  Write a Java Predicate Interface Example ?

     import java.util.function.Predicate;

public class Predicate_exam {


public static void main(String[] args) {

Predicate<Integer> pr = a -> (a > 18);

System.out.println(pr.test(25));

}

}

7). Write a Consumer Functional Interface program in java 8 ? or Java 8 Consumer Interface program ? or Write a Consumer Interface Example ?

     import java.util.function.Consumer;

public class Cosumer_Exam {

public static void main(String[] args) {

Consumer<String> print = x -> System.out.println(x);

print.accept(“java”);

}

}


8). Write a Supplier Functional Interface program in java 8 ? or Java 8 Supplier Interface program ? or Write a Supplier Interface Example ?

    public class Supplier_Exam {

public static void main(String[] args) {

Supplier<Double> supplier = () -> Math.random(); // to print randon number every time

System.out.println(supplier.get());

}

}

9). Write a Function Functional Interface program in java 8 ?  or Java 8 Function Interface program ? or Write a Function Interface Example ?

       import java.util.function.Function;

public class Function_Exam {

public static void main(String[] args) {

Function<String, Integer> func = x -> x.length();

Integer apply = func.apply(“Aryan”);

System.out.println(apply);

}

}


10). Write a UnaryOperator Functional Interface program in java 8 ? or Java 8 UnaryOperator Interface program ? or Write a UnaryOperator Interface Example ?

     public class UnaryOperator_Exam {

public static void main(String[] args) {

UnaryOperator<Integer> unaryOperation = x -> x + x;

System.out.println(unaryOperation.apply(5));

}

}   


11). Write a BinaryOperator Functional Interface program in java 8 ? or Java 8 BinaryOperator Interface program ? or Write a BinaryOperator Interface Example ?

       public class BinaryOperator_Exam {

public static void main(String[] args) {

BinaryOperator<String> binaryOperation = (str1, str2) -> str1 + str2;

System.out.println(binaryOperation.apply(“Aryan”, “Singh”));

}

}

12). Whrite a program of Optional Class in Java 8 ?  or  What is Optional Class in Java 8 program Example ? or Write a Optional Class example in Java 8 or Write a Optional Class example

       import java.util.Optional;

public class Optional_Exam {

public static Optional<String> getName(int id) {

String name =null; // value is coming null from DB

return Optional.ofNullable(name);

}


public static void main(String[] args) {

Optional<String> name = getName(1);

if (name.isPresent()) {

System.out.println(name.get()); // here we are getting name from database & may be name will be null value

}

name.ifPresent(System.out::println);

}

}