Java Interview Questions

 Java 8 introduced several new features and improvements, including lambda expressions, functional interfaces, streams, and default methods in interfaces. Here are some scenario-based interview questions with real-time examples that demonstrate the usage of these Java 8 features:

1. Lambda Expressions

Question: How would you use lambda expressions to implement a functional interface?

Example:


// Functional interface @FunctionalInterface interface Calculator { int calculate(int a, int b); } public class LambdaExample { public static void main(String[] args) { // Lambda expression to implement the Calculator interface Calculator addition = (a, b) -> a + b; // Using the lambda expression int result = addition.calculate(5, 3); // Result: 8 System.out.println("Addition result: " + result); } }

2. Streams API

Question: How can you use Streams API to filter and collect elements from a list?

Example:


import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class StreamExample { public static void main(String[] args) { List<String> fruits = Arrays.asList("Apple", "Orange", "Banana", "Grape", "Pineapple"); // Using Stream API to filter and collect elements List<String> filteredFruits = fruits.stream() .filter(fruit -> fruit.length() > 5) .collect(Collectors.toList()); System.out.println("Filtered fruits: " + filteredFruits); // Output: [Orange, Pineapple] } }

3. Functional Interfaces and Method References

Question: Explain how method references can be used with functional interfaces.

Example:


import java.util.function.Consumer; public class MethodReferenceExample { public static void main(String[] args) { // Using method reference to implement a Consumer functional interface Consumer<String> printer = System.out::println; // Using the Consumer interface printer.accept("Hello, World!"); // Output: Hello, World! } }

4. Default Methods in Interfaces

Question: How do default methods in interfaces improve API design and backward compatibility?

Example:


interface Greeting { default void greet() { System.out.println("Hello, Guest!"); } } class Guest implements Greeting { // No need to override the default method unless necessary } public class DefaultMethodExample { public static void main(String[] args) { Guest guest = new Guest(); guest.greet(); // Output: Hello, Guest! } }

5. Optional Class

Question: When would you use the Optional class and how does it prevent null pointer exceptions?

Example:


import java.util.Optional; public class OptionalExample { public static void main(String[] args) { String value = "Java 8 Optional Example"; // Creating an Optional Optional<String> optionalValue = Optional.ofNullable(value); // Using Optional to prevent null pointer exception if (optionalValue.isPresent()) { System.out.println(optionalValue.get()); // Output: Java 8 Optional Example } else { System.out.println("Value is null"); } } }

6. Parallel Streams

Question: How can you leverage parallel streams to improve performance in Java applications?

Example:


import java.util.Arrays; public class ParallelStreamExample { public static void main(String[] args) { int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; // Using parallel stream to perform operations concurrently int sum = Arrays.stream(numbers) .parallel() .sum(); System.out.println("Sum of numbers: " + sum); // Output: Sum of numbers: 55 } }

7. Date and Time API (java.time)

Question: How does the java.time API improve upon the legacy Date and Calendar classes?

Example:


import java.time.LocalDate; import java.time.format.DateTimeFormatter; public class DateTimeExample { public static void main(String[] args) { // Formatting date using DateTimeFormatter LocalDate today = LocalDate.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy"); String formattedDate = today.format(formatter); System.out.println("Formatted Date: " + formattedDate); // Output: Formatted Date: 30-06-2024 } }

Conclusion

These Java 8 scenario-based interview questions with real-time examples cover fundamental concepts such as lambda expressions, streams, functional interfaces, default methods in interfaces, Optional class, parallel streams, and the java.time API. Understanding these features and being able to demonstrate their usage in practical scenarios will showcase your proficiency in modern Java development and prepare you effectively for Java 8-focused interviews.

Daily Knowledge Journey: A Quest for Learning

Object Class

 The Object class in Java is the root of the class hierarchy and serves as the superclass for all other classes. It provides fundamental me...