# Java Interview question-4

**Question 31: Which Microservice pattern will you use for read-heavy and write-heavy applications?**

CQRS which is a command query pattern, CQRS stands for Command and Query Responsibility Segregation, a pattern that separates read and update operations for a data store. Implementing CQRS in your application can maximize its performance, scalability, and security. The flexibility created by migrating to CQRS allows a system to better evolve over time and prevents update commands from causing merge conflicts at the domain level.

![](https://miro.medium.com/v2/resize:fit:771/0*O0eKaa6sP7cK8dUn.png align="left")

**Question 32: What is the SAGA pattern is used?**

The Saga design pattern is a way to manage data consistency across microservices in distributed transaction scenarios. A saga is a sequence of transactions that updates each service and publishes a message or event to trigger the next transaction step. If a step fails, the saga executes compensating transactions that counteract the preceding transactions.

**Question 33: What circuit breaker pattern have you used it?What are the examples of it?**

If we want to stop any error cascading to another component/service in the microservice, to stop that we usually use circuit breakers. **Hystrix library** from Netflix and **Resiliency4j** are examples of it.

**Question 34: How to call methods Asynchronously, in the spring framework how can we do that?**

Using @**Async** annotation with executors to achieve that.

**Question 35: How to handle exceptions in the spring framework?**

@ControlAdvice annotation- This is a global exception handler in the spring boot application.

**Question 36: Can we override the static method? Why Can’t we do that?**

This is one of the most popular Java interview questions. The answer to this question is **No**, you cannot override the static method in Java because the method overriding is based upon **dynamic binding** at runtime and static methods are bonded using static binding at compile time. This means static methods are resolved even before objects are created, that’s why it’s not possible to override static methods in Java. Though you can declare a method with the same name and method signature in the subclass which does look like you can override static methods in Java but in reality that is **method hiding.**

Method overriding is an object-oriented concept that is based upon method resolution at runtime depending upon which object is calling the method rather than which class variable is holding the reference.

Java won’t resolve the static method call at runtime and depending upon the type of object which is used to call static methods, the corresponding method will be called. It means if you use Parent class’s type to call a static method, original static will be called from a parent class, on the other hand, if you use Child class’s type to call static methods, the method from child class will be called.

**Question 37: What is the use of static keywords in java?**

It means we’ll create only one instance of that static member that is shared across all instances of the class.

**Question 38: What is the Covariant type?**

Covariant return type refers to the **return type of an overriding method**. It allows the narrowing down of the return type of an overridden method without any need to cast the type or check the return type. The covariant return type works only for non-primitive return types

**Question 39: Write a Program to find the duplicates in an array using stream API.**

```java
package learning.functionalprogramming;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
public class FindDuplicateUsingStream {
    public static void main(String[] args) {
        List<Integer> list = List.of(1, 2, 3, 5, 5, 6, 6, 5);
        System.out.println(findDuplicateUsingGroupingBy(list));
        System.out.println(findDuplicateUsingFrequency(list));
    }
    private static List<?> findDuplicateUsingGroupingBy(List<?> list) {
        return list
                .stream()
                .collect(Collectors.groupingBy(
                        Function.identity(),
                        Collectors.counting()
                ))
                .entrySet()
                .stream()
                .filter(e -> e.getValue() > 1)
                .map(Map.Entry::getKey)
                .collect(Collectors.toList());
    }
    private static <T> List<T> findDuplicateUsingFrequency(List<T> list) {
        return list
                .stream()
                .filter(x -> Collections.frequency(list, x) > 1)
                .distinct()
                .toList();
    }
}
/**
 * Output:
 * [5, 6]
 * [5, 6]
 */
```

**Question 40: Write a program to find the missing number in an Array.**

```java
import java.util.Arrays;
public class FindMissingNumberFromSeries {
  public static void main(String[] args) {
    int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12};
    int N = numbers[numbers.length-1];  //The last element in the array
    int expectedSum = (N * (N + 1)) / 2;
    int actualSum = Arrays.stream(numbers).sum();
    int missingNumber = expectedSum - actualSum;
    System.out.println(missingNumber);
  }
}
```

[  
](https://medium.com/tag/java-interview-questions?source=post_page-----bc7a1531278c---------------java_interview_questions-----------------)
