JAVA 8 Interview Question for freshers and experienced

Categories: Java 8(JDK1.8)

Question:- In which programming paradigm Java 8 falls?

Answer :- 

  • Object-oriented programming language.
  • Functional programming language.
  • Procedural programming language.
  • Logic programming language

 

Question:- What are the significant advantages of Java 8?

Answer :- 

  • Compact, readable, and reusable code.
  • Less boilerplate code.
  • Parallel operations and execution.
  • Can be ported across operating systems.
  • High stability.
  • Stable environment.
  • Adequate support

 

Question:- What is MetaSpace? How does it differ from PermGen?

Answer :- 

PremGen: MetaData information of classes was stored in PremGen (Permanent-Generation) memory type before Java 8. PremGen is fixed in size and cannot be dynamically resized. It was a contiguous Java Heap Memory.

 

MetaSpace: Java 8 stores the MetaData of classes in native memory called 'MetaSpace'. It is not a contiguous Heap Memory and hence can be grown dynamically which helps to overcome the size constraints. This improves the garbage collection, auto-tuning, and de-allocation of metadata.

 

Question:- What are functional or SAM interfaces?

 

Answer :- Functional Interfaces are an interface with only one abstract method. Due to which it is also known as the Single Abstract Method (SAM) interface. It is known as a functional interface because it wraps a function as an interface or in other words a function is represented by a single abstract method of the interface.

 

Question:- What is the default method, and why is it required?

Answer :- A method in the interface that has a predefined body is known as the default method. It uses the keyword default. default methods were introduced in Java 8 to have 'Backward Compatibility in case JDK modifies any interfaces. In case a new abstract method is added to the interface, all classes implementing the interface will break and will have to implement the new method. With default methods, there will not be any impact on the interface implementing classes. default methods can be overridden if needed in the implementation. Also, it does not qualify as synchronized or final.

 

@FunctionalInterface // Annotation is optional 

public interface Foo() { 

// Default Method - Optional can be 0 or more 

public default String HelloWorld() { 

return "Hello World"; 

// Single Abstract Method 

public void bar(); 

}

 

Question:-  What are static methods in Interfaces?

Answer :- Static methods, which contains method implementation is owned by the interface and is invoked using the name of the interface, it is suitable for defining the utility methods and cannot be overridden.

 

Question:- What are some standard Java pre-defined functional interfaces?

Answer :- Some of the famous pre-defined functional interfaces from previous Java versions are Runnable, Callable, Comparator, and Comparable. While Java 8 introduces functional interfaces like Supplier, Consumer, Predicate, etc. Please refer to the java.util.function doc for other predefined functional interfaces and its description introduced in Java 8.

 

Runnable: use to execute the instances of a class over another thread with no arguments and no return value. 

 

Callable: use to execute the instances of a class over another thread with no arguments and it either returns a value or throws an exception.

 

Comparator: use to sort different objects in a user-defined order

 

Comparable: use to sort objects in the natural sort order

 

Question:-What are the various categories of pre-defined function interfaces?

Answer :- 

  • Function: To transform arguments in returnable value.
  • Predicate: To perform a test and return a Boolean value.
  • Consumer: Accept arguments but do not return any values.
  • Supplier: Do not accept any arguments but return a value. 

 

  • Operator: Perform a reduction type operation that accepts the same input types.

 

Question:- What is the lambda expression in Java and How does a lambda expression relate to a functional interface?

Answer :- Lambda expression is a type of function without a name. It may or may not have results and parameters. It is known as an anonymous function as it does not have type information by itself. It is executed on-demand. It is beneficial in iterating, filtering, and extracting data from a collection.

 

As lambda expressions are similar to anonymous functions, they can only be applied to the single abstract method of Functional Interface. It will infer the return type, type, and several arguments from the signature of the abstract method of functional interface.

 

Question:- What are the features of a lambda expression?

Answer :- Below are the two significant features of the methods that are defined as the lambda expressions: 

  1. Lambda expressions can be passed as a parameter to another method. 
  2. Lambda expressions can be standalone without belonging to any class.

 

Question:- What is a type interface?

Answer :- Type interface is available even in earlier versions of Java. It is used to infer the type of argument by the compiler at the compile time by looking at method invocation and corresponding declaration.

 

 Question:- What does the String::ValueOf expression mean?

Answer :- It is a static method reference to method Valueof() of class String. It will return the string representation of the argument passed.

 

Question:- What are the advantages of using the Optional class?

Answer :- Below are the main advantage of using the Optional class: 

It encapsulates optional values, i.e., null or not-null values, which helps in avoiding null checks, which results in better, readable, and robust code It acts as a wrapper around the object and returns an object instead of a value, which can be used to avoid run-time NullPointerExceptions.

 

Question:- What are the sources of data objects a Stream can process?

Answer :- A Stream can process the following data:

  • A collection of an Array.
  • An I/O channel or an input device.
  • A reactive source (e.g., comments in social media or tweets/re-tweets) 
  • A stream generator function or a static factory.

 

R4Rin Team
The content on R4Rin.com website is created by expert teams.