Java 8 Interview Questions

JAVA 8 INTERVIEW QUESTIONS

What is lambda expression ?

Its a block of code that we can pass around so that it can be used later many times.It has following syntax.


(argument-list) -> {body}

Give one example of lambda expression ?


package com.example.preparationforinterview;

interface MyInterface {
	String sayHello(String name);
}

public class LambdaExpressionExample {

	public static void main(String args[]) {

		MyInterface myInterface = (name) -> {
			return "Hello " + name;
		};
		System.out.println(myInterface.sayHello("preparationforinterview"));
	}
}

Output


Hello preparationforinterview

What is functional Interface ?

An interface which has only single abstarct method is called functional Interface.

What is the use of @FunctionalInterface ?

To annotate a interface with @FunctionalInterface will force that interface is to be a functional interface.

What is method reference in Java 8 ?

It is used to refer method in functional interface using lambda expression.They have three form

  • object::instanceMethod
  • Class::staticMethod
  • Class::instanceMethod

Example

Consumer<String> c = s -> System.out.println(s);

can be converted to 

Consumer<String> c = System.out::println;

What is constructor references ?

It is similar to method references but as a name we use new


Button::new is a reference to a Button constructor

Why following code will not compile ?


package com.example.preparationforinterview;

interface MyInterface {
	int increaseNumber();
}

public class LambdaExpressionExample {
	
	public static int variableScope(int count){
		MyInterface myInterface = () -> {
			return count++;
		};
		return myInterface.increaseNumber();
		
	}

	public static void main(String args[]) {
		variableScope(1);
	}
}

Lambda expression can capture the value of a variable in the enclosing scope but it can not modify it but if count is static or instance variable then its OK.

What is default method ?

In java 8 we can define method with concrete implementation with default key.



package com.example.preparationforinterview;

interface MyInterface {
	int increaseNumber(){
	System.out.println("Default Method");
}

If a class implements two interface with same default method then concrete class is forced to implement that method otherwise error will be thrown.

If one class extends other class and implements one interface and from both if we get same methods then superclass method will be considered.

Can we write static method in interface ?

yes in java 8 its allowed.For example Comparator has a static comparing method. To compare Person objects by name, use Comparator.comparing(Person::name).

What is stream API ?

stream API is used to process the values in collection.They don't store elements and change source collection.For example

package com.example.preparationforinterview;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class StreamAPIExample {

	public static void main(String args[]) {
		List<String> list = Arrays.asList("a", "b", "c", "d");

		List upperCase = list.stream().map(s -> s.toUpperCase())
				.collect(Collectors.toList());
		System.out.println(upperCase);
	}

}

Output

[A, B, C, D]

How can we create a stream from number of arguments ?

Stream<String> stream =  Stream.of("a", "b", "c", "d");

Give example of flatMap ?

flatMap is used to flatten the stream of stream.It can be understood by an example

package com.example.preparationforinterview;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class FlatMapExample {

	public static Stream<Character> getCharacterStream(String word) {
		List list = new ArrayList();
		char[] charArray = word.toCharArray();
		for (char c : charArray) {
			list.add(c);
		}
		return list.stream();
	}

	public static void main(String args[]) {
		String line = "My Line";
		Stream<Stream<Character>> streaOfStreamOfCharacter = Arrays
				.asList(line.split(" ")).stream()
				.map(FlatMapExample::getCharacterStream);
		
		System.out.println(streaOfStreamOfCharacter.collect(Collectors.toList()));
		
		Stream<Character> streaOfStreamOfCharacter1 = Arrays
				.asList(line.split(" ")).stream()
				.flatMap(FlatMapExample::getCharacterStream);
		
		System.out.println(streaOfStreamOfCharacter1.collect(Collectors.toList()));
	}

}

Output

[java.util.stream.ReferencePipeline$Head@119d7047, java.util.stream.ReferencePipeline$Head@776ec8df]
[M, y, L, i, n, e]

If we use only map then it return stream of stream of character while flatMap flatten the stream of stream.

What is the use of limit method ?

It limits the number of element that can be extracted into other stream.

Give example of stateful stream transformations ?

distinct , sorted because they require to look complete stream for comparing.

How can we convert primitive stream to Object stream ?

Using boxed method.

Which framework does ParrelStream uses under the hood ?

fork/join framework

In java 8 why don't we get java.lang.OutOfMemoryError: PermGen space ?

In java 8 PermGen has been removed and instead of PermGen they have introduced Metaspace.Difference between PermGen and Metaspace is PermGen is part of Heap while Metaspace is part of Native Memory (process memory) which is only limited by the Host Operating System.If application loads lots of classes then entire server can go down because native memory is only limited by the Operating System and we take all memory on server.




Visit Others Links

Interview Questions Top Elasticsearch Interview Question
Top Kubernetes Interview Question
Top HTTP Interview Question
Top PouchDB Interview Question
Top Pivotal Cloud Foundry (pcf) Interview Question
Top Apache Camel Interview Question
Top Docker Interview Question
Top Gradle Interview Question
Top Generics Interview Questions In Java
Java 9 Interview Questions
Top Maven Interview Question
Top RabbitMQ Interview Question
Top Spring Batch Interview Question
Top Spring Boot Interview Question
Top Tika Interview Question
Top Kotlin Interview Question
Top OpenShift Interview Question
Top Polymer Interview Question
Top WebGL Interview Question
Top Continuous Integration Interview Question
Top Spring Cloud Interview Question
Top JOGL Interview Question
Top JFreeChart Interview Question
Java MultiThreading Interview Question
Top Spring ROO Interview Question
Top Seaborn Interview Question
Top TestRail Interview Question
Top LolCode Interview Question
Top Spring Transaction Management Interview Question
Top DynamoDB Interview Question
Top PySpark Interview Question
Top SaltStack Interview Question
Top Spring AOP Interview Question
© 2019 PreparationForInterview.com