Interview Questions and Answers
Ques 1. What are main advantages of using Java 8?
- More compact code
- Less boiler plate code
- More readable and reusable code
- More testable code
- Parallel operations
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 2. What is the difference between Predicate and Function?
- 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. This can also be used as the assignment target for a lambda expression or method reference.
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Freshers / Beginner level questions & answers
Ques 3. What are new features which got introduced in Java 8?
- Lambda Expression
- Stream API
- Default methods in the interface
- Functional Interface
- Optional
- Method references
- Date API
- Nashorn, JavaScript Engine
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 4. Can you explain the syntax of Lambda expression?
- Arguments
- Array Token
- Statements
- Body can have expression or statements.
- If there is only one statement in body, curly brace is not needed and return type of the anonymous function is same as of body expression.
- If there are more than one statements, then it should be in curly braces and return type of anonymous function is same as value return from code block, void if nothing is returned.
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 5. What are functional interfaces?
- Functional interfaces are those interfaces which can have only one abstract method. It can have static method, default methods or can override Object’s class methods.
- There are many functional interfaces already present in java such as Comparable, Runnable.
- As we have only one method in Runnable, hence it is considered as functional interface.
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 6. What Is a Default Method and When Do We Use It?
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 7. What is Optional and how can we use it?
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 8. Provide some APIs of Java 8 Date and Time.
LocalDate, LocalTime, and LocalDateTime are the Core API classes for Java 8. As the name suggests, these classes are local to context of observer. It denotes current date and time in context of Observer.
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 9. How will you get current date and time using Java 8 Date and TIme API?
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Intermediate / 1 to 5 years experienced level questions & answers
Ques 10. What is lambda expression?
Thread thread = new Thread(new Runnable() { public void run() { System.out.println("Hello World!"); }});Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 11. Given a list of employees, sort all the employee on the basis of age by using java 8 APIs only.
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 12. Given the list of employees, find the employee with name 'John' using Java 8 API.
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 13. Given a list of employee, find maximum age of employee using Java 8 API.
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 14. Provide some examples of Intermediate operations.
- filter(Predicate)
- map(Function)
- flatmap(Function)
- sorted(Comparator)
- distinct()
- limit(long n)
- skip(long n)
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 15. Provide some examples of Terminal operations.
- forEach
- toArray
- reduce
- collect
- min
- max
- count
- anyMatch
- allMatch
- noneMatch
- findFirst
- findAny
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Experienced / Expert level questions & answers
Ques 16. How lambda expression and functional interfaces are related?
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 17. Can you create your own functional interface?
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 18. What is method reference in java 8?
Method reference is used refer method of functional interface. It is nothing but compact way of lambda expression.You can simply replace lambda expression with method reference.
Syntax:
class::methodname
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 19. What the issues with Old Date and time API before Java 8? Date and Time API differences between Java 8 and earlier Java version.
- Thread Safety: You might be already aware that java.util.Date is mutable and not thread safe. Even java.text.SimpleDateFormat is also not Thread-Safe. New Java 8 date and time APIs are thread safe.
- Performance: Java 8 new APIs are better in performance than old Java APIs.
- More Readable: Old APIs such Calendar and Date are poorly designed and hard to understand. Java 8 Date and Time APIs are easy to understand and comply with ISO standards.
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 20. Do we have PermGen in Java 8? What is MetaSpace in Java 8?
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 21. Difference between Intermediate and terminal operations in Stream.
- Intermediate operations are lazy in nature and do not get executed immediately. Terminal operations are not lazy, they are executed as soon as encountered.
- Intermediate operation is memorized and is called when terminal operation is called.
- All Intermediate operations return stream as it just transforms stream into another and terminal operation don't.
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 22. Given the list of numbers, remove the duplicate elements from the list.
See the following code:
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 23. Difference between Stream findFirst() and findAny().
- findFirst will always return the first element from the stream whereas findAny is allowed to choose any element from the stream.
- findFirst has deterministic behavior whereas findAny is nondeterministic behavior.
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 24. What is consumer function interface?
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 25. What is predicate function interface?
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 26. What Is a Stream? How Does It Differ from a Collection?
- In simple terms, a stream is an iterator whose role is to accept a set of actions to apply on each of the elements it contains.
- The stream represents a sequence of objects from a source such as a collection, which supports aggregate operations. They were designed to make collection processing simple and concise. Contrary to the collections, the logic of iteration is implemented inside the stream, so we can use methods like map and flatMap for performing a declarative processing.
- And yet another important distinction from collections is that streams are inherently lazily loaded and processed.
- Another difference is that the Stream API is fluent and allows pipelining:
- int sum = Arrays.stream(new int[]{1, 2, 3}).filter(i -> i >= 2).map(i -> i * 3).sum();
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 27. What Is the Difference Between Map and flatMap Stream Operation?
- There is a difference in signature between map and flatMap. Generally speaking, a map operation wraps its return value inside its ordinal type while flatMap does not.
- For example, in Optional, a map operation would return Optional<String> type while flatMap would return String type.
- So after mapping, one needs to unwrap (read “flatten”) the object to retrieve the value whereas, after flat mapping, there is no such need as the object is already flattened. The same concept is applied to mapping and flat mapping in Stream.
- Both map and flatMap are intermediate stream operations that receive a function and apply this function to all elements of a stream.
- The difference is that for the map, this function returns a value, but for flatMap, this function returns a stream. The flatMap operation “flattens” the streams into one.
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 28. What is Stream Pipelining in Java 8?
- Stream pipelining is the concept of chaining operations together. This is done by splitting the operations that can happen on a stream into two categories: intermediate operations and terminal operations.
- Each intermediate operation returns an instance of Stream itself when it runs, an arbitrary number of intermediate operations can, therefore, be set up to process data forming a processing pipeline.
- There must then be a terminal operation which returns a final value and terminates the pipeline.
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 29. What does the flatmap() function do? why you need it?
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Ques 30. What is the parallel Stream? How can you get a parallel stream from a List?
Save For Revision
Save For Revision
Bookmark this item, mark it difficult, or place it in a revision set.
Log in to save bookmarks, difficult questions, and revision sets.
Most helpful rated by users:
- What are new features which got introduced in Java 8?
- What are main advantages of using Java 8?
- Can you explain the syntax of Lambda expression?
- What are functional interfaces?
- What Is a Default Method and When Do We Use It?