Choose.. X Home Exams Certifications
 

Java 8 Interview Questions

1 . What Are The New Features Introduced In Java 8?

  • There are dozens of features added to Java 8, the most significant ones are mentioned below −

    • Lambda expression − Adds functional processing capability to Java.
    • Method references − Referencing functions by their names instead of invoking them directly. Using functions as parameter.
    • Default method − Interface to have default method implementation.
    • New tools − New compiler tools and utilities are added like 'jdeps' to figure out dependencies.
    • Stream API − New stream API to facilitate pipeline processing.
    • Date Time API − Improved date time API.
    • Optional − Emphasis on best practices to handle null values properly.
    • Nashorn, JavaScript Engine − A Java-based engine to execute JavaScript code.

    Along with these new featuers, lots of feature enhancements are done under-the-hood, at both compiler and JVM level.

2 . How Will You Sort A List Of String Using Java 8 Lambda Expression?

  • Following code sorts a list of string using Java 8 lambda expression:

    //sort using java 8private void sortUsingJava8(List<String> names){
      Collections.sort(names, (s1, s2) -> s1.compareTo(s2));
    }

3 . What Are The Characteristics Of A Java 8 Lambda Expression?

  • A lambda expression is characterized by the following syntax -  parameter −> expression body

    Following are the important characteristics of a lambda expression −

    • Optional type declaration − No need to declare the type of a parameter. The compiler can inference the same from the value of the parameter.
    • Optional parenthesis around parameter − No need to declare a single parameter in parenthesis. For multiple parameters, parentheses are required.
    • Optional curly braces − No need to use curly braces in expression body if the body contains a single statement.
    • Optional return keyword − The compiler automatically returns the value if the body has a single expression to return the value. Curly braces are required to indicate that expression returns a value.

4 . Why Lambda Expression Is To Be Used?

  • Lambda expressions are used primarily to define inline implementation of a functional interface, i.e., an interface with a single method only. In the above example, we've used various types of lambda expressions to define the operation method of MathOperation interface. Then we have defined the implementation of sayMessage of GreetingService.

    Lambda expression eliminates the need of anonymous class and gives a very simple yet powerful functional programming capability to Java.

5 . What Kind Of Variable You Can Access In An Lambda Expression?

  • Using lambda expression, you can refer to final variable or effectively final variable (which is assigned only once). Lambda expression throws a compilation error, if a variable is assigned a value the second time.

6 . What Are Method References?

  • Method references help to point to methods by their names. A method reference is described using :: (double colon) symbol. A method reference can be used to point the following types of methods −

    • Static methods
    • Instance methods
    • Constructors using new operator (TreeSet::new)

7 . Explain The System.out::println Expression?

  • System.out::println method is a static method reference to println method of out object of System class.

8 . What Are Functional Interfaces?

  • Functional interfaces have a single functionality to exhibit. For example, a Comparable interface with a single method 'compareTo' is used for comparison purpose. Java 8 has defined a lot of functional interfaces to be used extensively in lambda expressions.

9 . What Is The Purpose Of Biconsumer<t,u> Functional Interface?

  • It represents an operation that accepts two input arguments, and returns no result.

10 . What Is The Purpose Of Bifunction<t,u,r> Functional Interface?

  • It represents a function that accepts two arguments and produces a result.

11 . What Is The Purpose Of Binaryoperator<t> Functional Interface?

  • It represents an operation upon two operands of the same type, producing a result of the same type as the operands.

12 . What Is The Purpose Of Bipredicate<t,u> Functional Interface?

  • It represents a predicate (Boolean-valued function) of two arguments.

13 . What Is The Purpose Of Booleansupplier Functional Interface?

  • It represents a supplier of Boolean-valued results.

14 . What Is The Purpose Of Consumer<t> Functional Interface?

  • It represents an operation that accepts a single input argument and returns no result.

15 . What Is The Purpose Of Doublebinaryoperator Functional Interface?

  • It represents an operation upon two double-valued operands and producing a double-valued result.

16 . What Is The Purpose Of Doubleconsumer Functional Interface?

  • It represents an operation that accepts a single double-valued argument and returns no result.

17 . What Is The Purpose Of Doublefunction<r> Functional Interface?

  • It represents a function that accepts a double-valued argument and produces a result.

18 . What Is The Purpose Of Doublepredicate Functional Interface?

  • It represents a predicate (Boolean-valued function) of one double-valued argument.

19 . What Is The Purpose Of Doublesupplier Functional Interface?

  • It represents a supplier of double-valued results.

20 . What Is The Purpose Of Doubletointfunction Functional Interface?

  • It represents a function that accepts a double-valued argument and produces an int-valued result.

21 . What Is The Purpose Of Doubletolongfunction Functional Interface?

  • It represents a function that accepts a double-valued argument and produces a long-valued result.

22 . What Is The Purpose Of Doubleunaryoperator Functional Interface?

  • It represents an operation on a single double-valued operand that produces a double-valued result.

23 . What Is The Purpose Of Function<t,r> Functional Interface?

  • It represents a function that accepts one argument and produces a result.

24 . What Is The Purpose Of Intbinaryoperator Functional Interface?

  • It represents an operation upon two int-valued operands and produces an int-valued result.

25 . What Is The Purpose Of Intconsumer Functional Interface?

  • It represents an operation that accepts a single int-valued argument and returns no result.

26 . What Is The Purpose Of Intfunction<r> Functional Interface?

  • It represents a function that accepts an int-valued argument and produces a result.

27 . What Is The Purpose Of Intpredicate Functional Interface?

  • It represents a predicate (Boolean-valued function) of one int-valued argument.

28 . What Is The Purpose Of Intsupplier Functional Interface?

  • It represents a supplier of int-valued results.

29 . What Is The Purpose Of Inttodoublefunction Functional Interface?

  • It represents a function that accepts an int-valued argument and produces a double-valued result.

30 . What Is The Purpose Of Inttolongfunction Functional Interface?

  • It represents a function that accepts an int-valued argument and produces a long-valued result.

31 . What Is The Purpose Of Intunaryoperator Functional Interface?

  • It represents an operation on a single int-valued operand that produces an int-valued result.

32 . What Is The Purpose Of Longbinaryoperator Functional Interface?

  • It represents an operation upon two long-valued operands and produces a long-valued result.

33 . What Is The Purpose Of Longconsumer Functional Interface?

  • It represents an operation that accepts a single long-valued argument and returns no result.

34 . What Is The Purpose Of Longfunction<r> Functional Interface?

  • It represents a function that accepts a long-valued argument and produces a result.

35 . What Is The Purpose Of Longpredicate Functional Interface?

  • It represents a predicate (Boolean-valued function) of one long-valued argument.

36 . What Is The Purpose Of Longsupplier Functional Interface?

  • It represents a supplier of long-valued results.

37 . What Is The Purpose Of Longtodoublefunction Functional Interface?

  • It represents a function that accepts a long-valued argument and produces a double-valued result.

38 . What Is The Purpose Of Longtointfunction Functional Interface?

  • It represents a function that accepts a long-valued argument and produces an int-valued result.

39 . What Is The Purpose Of Longunaryoperator Functional Interface?

  • It represents an operation on a single long-valued operand that produces a long-valued result.

40 . What Is The Purpose Of Objdoubleconsumer<t> Functional Interface?

  • It represents an operation that accepts an object-valued and a double-valued argument, and returns no result.

41 . What Is The Purpose Of Objintconsumer<t> Functional Interface?

  • It represents an operation that accepts an object-valued and an int-valued argument, and returns no result.

42 . What Is The Purpose Of Objlongconsumer<t> Functional Interface?

  • It represents an operation that accepts an object-valued and a long-valued argument, and returns no result.

43 . What Is The Purpose Of Predicate<t> Functional Interface?

  • It represents a predicate (Boolean-valued function) of one argument.

44 . What Is The Purpose Of Supplier<t> Functional Interface?

  • It represents a supplier of results.

45 . What Is The Purpose Of Todoublebifunction<t,u> Functional Interface?

  • It represents a function that accepts two arguments and produces a double-valued result.

46 . What Is The Purpose Of Todoublefunction<t> Functional Interface?

  • It represents a function that produces a double-valued result.

47 . What Is The Purpose Of Tointbifunction<t,u> Functional Interface?

  • It represents a function that accepts two arguments and produces an int-valued result.

48 . What Is The Purpose Of Tointfunction<t> Functional Interface?

  • It represents a function that produces an int-valued result.

49 . What Is The Purpose Of Tolongbifunction<t,u> Functional Interface?

  • It represents a function that accepts two arguments and produces a long-valued result.

50 . What Is The Purpose Of Tolongfunction<t> Functional Interface?

  • It represents a function that produces a long-valued result.