Java 8 Interview Questions


Question 1:

What is the reason "synchronized" is not allowed in Java 8 interface methods?

Question 2:

What are the differences between abstract classes and interfaces in Java 8?

Question 3:

What are the new Java 8 annotations?

Question 4:

How to convert a Java 8 Stream to an Array?

You can convert stream to an array as following - 

Stream streamString = Stream.of("a", "b", "c");
String[] stringArray = streamString.toArray(size -> new String[size]);

OR,

Stream streamString = Stream.of("a", "b", "c");
String[] stringArray = streamString.toArray(String[]::new);
Question 5:

What is the difference between Functional Programming and Object-Oriented Programming?


Question 6:

What is Nashorn in Java 8?

Prior to Java 8, Rhino was used as default JavaScript engine by JVM. Nashorn replaces Rhino as the default JavaScript engine for the Oracle JVM in Java 8.

Nashorn is much faster as compared to Rhino.

Question 7:

What are the newly added features in Java 8?

Java 8 includes the following features:

  • Lambda expressions
  • Method references
  • Default Methods
  • A new Stream API.
  • Optional
  • A new Date/Time API.
  • Nashorn, the new JavaScript engine
  • Removal of the Permanent Generation
Question 8:

What is the difference between final and effectively final?

A variable if declared as final, its value cannot be changed in case of primitive type and reference cannot be changed in case of object.

Similarly, a variable or parameter whose value is never changed after it is initialized is effectively final.

For example -  

public void myMethod(int variable){
      // do something
      //if the value of variable is not changed it will be treated by java compiler as effectively final
}

If the value of int variable is not changed it is treated by the compiler as a final which is saying to be as effectively final. But the difference between effective final and final is that for a final variable if you want to assign a new value then compiler gives an error that it's a final. Where as if you assign a new value to a normal variable no errors or warnings will come, but now it is no more effectively final.

Hence, effectively final variable is a variable which behave like a final variable without declaring it as final.

Question 9:

Why is "final" not allowed in Java 8 interface methods?

Question 10:

How to convert an iterator to a stream in Java 8?

Share interview questions

Comments

Comments
comments powered by Disqus

Navigation

Social Media