Java 8 - Interview Questions and Answers on Java 8

Q1.  What are new features introduced with Java 8 ?

Ans. Lambda Expressions , Interface Default and Static Methods , Method Reference , Parameters Name , Optional , Streams, Concurrency.

Q2.  What is a Lambda Expression ? What's its use ?

Ans. Its an anonymous method without any declaration. Lambda Expression are useful to write shorthand Code and hence saves the effort of writing lengthy Code.    It promotes Developer productivity, Better Readable and Reliable code.

Q3.  Difference between Predicate, Supplier and Consumer ? 

Ans. Predicate represents an anonymous function that accepts one argument and produces a result.

Supplier represents an anonymous function that accepts no argument and produces a result.

Consumer represents an anonymous function that accepts an argument and produces no result.

Q4.  What does the following lambda expression means ?

helloJava8 ( x-> x%2 )


Ans. helloJava8 receives an Integer as argument and then returns the modulus of that Integer.

Q5.  What are Default Methods ?

Ans. With Java 8, We can provide method definitions in the Interfaces that gets carried down the classes implementing that interface in case they are not overridden by the Class. Keyword "default" is used to mark the default method.

Q6.  Can we have a default method definition in the interface without specifying the keyword "default" ? 

Ans. No. Compiler complains that its an abstract method and hence shouldn't have the body.

Q7.  Can a class implement two Interfaces having default method with same name and signature ?

public interface DefaultMethodInterface {
    default public void defaultMethod(){
       System.out.println("DefaultMethodInterface");        
    }
}

public interface DefaultMethodInterface2 {          
        default public void defaultMethod(){
               System.out.println("DefaultMethodInterface2");        
        }
}

public class HelloJava8 implements DefaultMethodInterface,DefaultMethodInterface2 {
   public static void main(String[] args){   
           DefaultMethodInterface defMethIn = new HelloJava8();
           defMethIn.defaultMethod();
    }
}

Ans. No. Compiler gives error saying "Duplicate Default Methods"

Q8.  What If we make the method as abstract in another Interface ?

public interface DefaultMethodInterface {
    default public void defaultMethod(){
       System.out.println("DefaultMethodInterface");        
    }
}

public interface DefaultMethodInterface2 {          
        public void defaultMethod(){
               System.out.println("DefaultMethodInterface2");        
        }
}

public class HelloJava8 implements DefaultMethodInterface,DefaultMethodInterface2 {
   public static void main(String[] args){   
           DefaultMethodInterface defMethIn = new HelloJava8();
           defMethIn.defaultMethod();
    }
}

Ans. Even then the Compiler will give error saying that there is a conflict.

Q9.  What if we override the conflicting method in the Class ?

public interface DefaultMethodInterface {
    default public void defaultMethod(){
       System.out.println("DefaultMethodInterface");        
    }
}

public interface DefaultMethodInterface2 {          
        default public void defaultMethod(){
               System.out.println("DefaultMethodInterface2");        
        }
}

public class HelloJava8 implements DefaultMethodInterface,DefaultMethodInterface2 {
   public static void main(String[] args){   
           DefaultMethodInterface defMethIn = new HelloJava8();
           defMethIn.defaultMethod();
    }

   public void defaultMethod(){
       System.out.println("HelloJava8");
   }
}

Ans. There won't be any error and upon execution the overriding class method will be executed. 

Q10.  What will happen if there is a default method conflict as mentioned above and we have specified the same signature method in the base class instead of overriding in the existing class ?

Ans. There won't be any problem as the Base class method will have precedence over the Interface Default methods.

Q11.  If there is a conflict between Base Class Method definition and Interface Default method definition, Which definition is Picked ?

Ans. Base Class Definition.

Q12.  If a method definition has been specified in Class , its Base Class , and the interface which the class is implementing, Which definition will be picked if we try to access it using Interface Reference  and Class object ?   

Ans. Class method definition is overriding both the definitions and hence will be picked.

Q13.  If a method definition has been specified in the Base Class and the interface which the class is implementing, Which definition will be picked if we try to access it using Interface Reference and Class object ?   

Ans. Base Class Definition will have precedence over the Interface Default method definition.

Q14.  Can we use static method definitions in Interfaces ?

Ans. Yes, Effective Java 8.

Q15.  Can we access Interface static method using Interface references ?

Ans. No, only using Interface Name.

Q16.  Can we have default method with same name and signature in the derived Interface as the static method in base Interface and vice versa ?

Ans. Yes , we can do that as static methods are not accessible using references and hence cannot lead to conflict. We cannot do inverse as Default methods cannot be overridden with the static methods in derived interface.

Q17.  Name few Java 8 annotations ?

Ans. 

@FunctionalInterface annotation, introduced in Java SE 8, indicates that the type declaration is intended to be a functional interface, as defined by the Java Language Specification.

@Repeatable annotation, introduced in Java SE 8, indicates that the marked annotation can be applied more than once to the same declaration or type use. For more information, see Repeating Annotations.

Q18.  What is the @FunctionalInterface annotation ?

Ans. This is an informative annotation that specify that the interface is a functional interface. A Function Interface has only one abstract method and many default methods. Compiler generates an error if the interface specified with the annotation doesn't abide by the specifications for functional interface.

Q19.  Difference between final and effectively final ? Why is effectively final even required ?

Ans. Final variable means a variable that has been declared final and hence cannot be changed after initialization.

Effective final means a variable that has not been declared final but haven't been reassigned the value after initialization.

First is the regulation that restricts the reassignment and will raise a compilation error if we try to do so. Second is the outcome without the restriction.

Effective Final is the eventual treatment of the variable that is required for many features. For eq - Java 8 requires that

local variables referenced from a lambda expression must be final or effectively final

It means all local referenced from lambda expressions must be such that their value shouldn't be changed after initialization whether declared final or not.

Q20.  Difference between DoubleSummaryStatistics , IntSummaryStatistics and LongSummaryStatistics ?

Ans. They all does the same task i.e to compute statistical information on the stream of data. They differ by the way they store the statistical information as they expect a different data type of the values being used. 

IntSummaryStatistics and LongSummaryStatistics expect non floating point values and hence stores the statistical information like min,max and sum as non floating values ( int or long ) whereas DoubleSummaryStatistics stores these information as floating value.

Q21.  What is StringJoiner ?

Ans. StringJoiner is a util method to construct a string with desired delimiter. This has been introduced with wef from Java 8.

Sample Code

StringJoiner strJoiner = new StringJoiner(".");
strJoiner.add("Buggy").add("Bread");
System.out.println(strJoiner); // prints Buggy.Bread

Q22.  What is the use of Optional ?

Ans. Optional is a good way to protect application from runtime nullPointerException in case the the absent value has been represented as null. So basically Optional class provides the type checking during compile time and hence will never result in NPE.

For ex -

List<Optional<Employee>> intList = new ArrayList<Optional<Employee>>();
intList.add(Optional.empty());
intList.add(Optional.of(new Employee("abc")));
intList.add(Optional.of(new Employee("xyz")));
intList.add(Optional.of(new Employee("123")));
System.out.println(intList.get(0).getName());

So Now , even when the first list element is empty, this code will never throw an NullPointerException.

Q23.  Name few "Optional" classes introduced with Java 8 ?

Ans. http://www.buggybread.com/2015/01/java-optional-classes-and-interfaces.html

Q24.  What are the changes in Java.io in Java 8 ?

Ans. http://www.buggybread.com/2015/01/migrating-to-java-8-new-classes_62.html

Q25.  Name few Java Util classes introduced with Java 8 ?

Ans. http://www.buggybread.com/2015/01/migrating-to-java-8-new-classes_25.html

Q26.  Name few java.lang classes introduced with Java 8 ?

Ans. http://www.buggybread.com/2015/01/migrating-to-java-8-new-classes.html

Q27.  Which keyword specify that a variable is effectively final ?

 a. final
 b. No Keyword
 c. Both of the above
 d. None of the above

Ans. No Keyword

Q28.  Which of the following has been introduced with Java 8 ?

 a. StringBuffer
 b. StringBuilder
 c. StringFilter
 d. StringJoiner

Ans. StringJoiner