Thursday, January 25, 2018

Java 8 Interview Questions

In this post you will learn some frequently asked java 8 interview questions and answers for freshers and experienced as well. 

1. What are the new features added in Java 8?

Ans: Lambda Expressions:

Lambdas are anonymous functions that capture the variables of its surrounding context. They can be used to treat code as data, or, as an example, to be referenced and used as an ordinary object and passed as argument an to methods and constructors.

Functional interfaces: Functional interfaces are plain old Java interfaces with only one abstract method.

Method References: Method references are also Lambda Expressions. When all a Lambda Expression does is to invoke one method or constructor, you can substitute for a much simpler syntax called Method Reference.

Stream API: a special iterator class that allows the processing of data in a declarative way. Streams can also leverage multi-core architectures without you having to write a single line of multi-threaded code. Writing parallel code is hard and error-prone, as we all know.

Default Methods: Interfaces can now contain implementation code, this is called default methods. It allows an Interface to have methods added to them without having to worry about classes who implement this interface.

2.What are main advantages of using Java 8?
Ans:
  • More compact code
  • Less boiler plate code
  • More readable and reusable code
  • More testable code
  • Parallel operations 
3. Can you create your own functional interface?
Yes, you can create your own functional interface. Java can implicitly identify functional interface but you can also annotate it with @FunctionalInterface.
Example:
Create interface named “Display” as below

public interface display {
void display();
default void displayColor()
{
System.out.println("display Color copy");
}
}
now Create main class named “FunctionalIntefaceMain”
class FunctionalIntefaceMain {

public static void main(String[] args)
{
FunctionalIntefaceMain pMain=new FunctionalIntefaceMain();
pMain.displayForm(() -> System.out.println("displayable form"));
}
public void printForm(display d)
{
d.display();
}
}
Output:
displayable form

5. What are method references?

Ans:
When all a lambda expression does is call one single method from an object or a class, or even a constructor, it can be expressed in a much less verbose way called Method Reference. So, method references are a syntax sugar to create lambda expressions that only invoke one method or constructor.

Syntax:
class::methodname

6.What is Optional?How can you use it?
Ans:

Java 8 has introduced new class Called Optional. This class is basically introduced to avoid NullPointerException in java.Optional class encapsulates optional value which is either present or not.It is a wrapper around object and can be use to avoid NullPointerExceptions.

7. What is the difference between Predicate and Function?
Ans:

Both are functional interfaces.
Predicate<T> is single argument function and either it returns true or false.This can be used as the assignment target for a lambda expression or method reference.
Function<T,R> is also single argument function but it returns an Object.Here T denotes type of input to the function and R denotes type of Result.

8.What is the difference between an interface with a default method and an abstract class?

Ans: Java 8 brought the concept of default methods or method implementations in an Interface. So one might ask what is the difference between an interface and an abstract class now.
Abstract classes can have many things that an interface cannot, like constructors and instance variables. Abstract classes, even if they have only one abstract method, can’t be used as functional interfaces, which seems pretty obvious but it is always better to make it clear.

9.What happens if you create an Interface that extends another interface which contains a Default Method?
Ans:
If an interface that contains a default method is extended, there are three options:

  1. The child interface does not mention the default method: The effect is that the default method is inherited;
  2. The child interface redeclares the method: The method then becomes abstract;
  3. The child interface redefines the method: The effect is that the new method overrides the one from the super interface
10.Do we have PermGen in Java 8? Are you aware of MetaSpace?
Ans:
Until Java 7, JVM used an area called PermGen to store classes. It got removed in Java 8 and replaced by MetaSpace.
Major advantage of MetaSpace over permgen:
PermGen was fixed in term of maximum size and can not grow dynamically but Metaspace can grow dynamically and do not have any size constraint.

11. What are Interface Static Methods in Java 8 ? How are they invoked?

Ans: Java 8 added the possibility to create static methods in interfaces. Neither the interfaces that extend an interface with a static method nor the classes that implement it, inherit this static method, it is necessary to invoke them explicitly using the interface name, just like any other static method: NameOfInteface.staticMethod();
A static method can only be invoked directly by the implementation methods (either static or default) from the defining interface.

12. Explain Java 8-Internal vs. External Iteration?
Ans:
External Iterators- This Iterator is also known as active iterator or explicit iterator. For this type of iterator the control over iteration of elements is with the programmer. Which means that the programmer define when and how the next element of iteration is called.
Internal Iterators- This Iterator is also known as passive iterator, implicit iterator or callback iterator. For this type of iterator the control over the iteration of elements lies with the iterator itself. The programmer only tells the iterator "What operation is to be performed on the elements of the collection". Thus the programmer only declares what is to be done and does not manage and control how the iteration of individual elements take place.

13.What is :: (double colon) operator in Java 8?
Ans:
Usually we use lambda expressions to create anonymous methods which return us the desired output. But sometimes lambda expressions do nothing but call an existing method. Because this lambda expression calls an existing method, method reference can be used here instead of Lambda function. Method reference is described using :: (double colon) symbol.




No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...