Java Interview question-8
Java || SpringBoot ||AWS
Question 71: How to resolve the circular dependency problem in spring?
A simple way to break the cycle is by telling Spring to initialize one of the beans lazily. So, instead of fully initializing the bean, it will create a proxy to inject it into the other bean. The injected bean will only be fully created when it’s first needed
Using the lookup method and @Lazy annotation we can resolve that
Question 72: Which exception can be thrown from the threads run method?
The Exception must thrown back to the caller. The caller of the run() method is not your code. It is the Thread itself. So even if run() throws an exception the program cannot catch it.
You should put the thread execution result to some class-level variable and then read it from there. Or alternatively use a new API: executors and interface Callable that declares method call() that returns the future result of the thread execution.
Question 73: Abstract vs interface and use of static vs default methods?

Question 74: Internal implementation of Concurrent HashMap?

Question 75: You have been given an employee list with EMP<Id, Name, Salary, Deptt>, find the highest salary of an employee from the HR department.
Optional<Employee> maxSalaryEmployee =
employeeList.stream().filter(e -> "HR".equals(e.getDepartment()))
.collect(Collectors.maxBy(Comparator.comparing(Employee::getSalary)));
Question 76: Polymorphism coding puzzle. Guess the Output?
package com.vivek;
public class InheritanceTest {
public static void main(String[] args) {
method(null);
}
public static void method(Object o) {
System.out.println("Object method");
}
public static void method(String s) {
System.out.println("String method");
}
}
/**
* Output: String method
*/
Question 77: Inheritance Coding problem, guess the output?
package com.vivek;
class A {
public void m1() throws ArrayIndexOutOfBoundsException {
System.out.println("In m1 A");
}
}
class B extends A {
void m1() throws IndexOutOfBoundsException {
System.out.println("In m1 B");
}
void m2() throws IndexOutOfBoundsException {
System.out.println("In m2 B");
}
}
public class Test {
public static void main(String[] args) {
A a = new B();
a.m2();
}
}
/**
* Compile time error at a.m2()
*/
Question 78: Write a Program to find a common element from three integer ArrayList. eg. arr1, arr2, and arr3.
package com.vivek;
public class FindCommonElementInThree {
public static void commonElementBruteForce(int[] arr1, int[] arr2,
int[] arr3) {
for (int i = 0; i < arr1.length; i++) {
for (int j = 0; j < arr2.length; j++) {
for (int k = 0; k < arr3.length; k++) {
if (arr1[i] == arr2[j] && arr2[j] == arr3[k]) {
System.out.println(arr1[i]);
}
}
}
}
}
public static void commonElementsPerformant(int[] arr1, int[] arr2,
int[] arr3) {
//Declare three variables and intialized to zero
int x = 0, y = 0, z = 0;
while (x < arr1.length && y < arr2.length && z < arr3.length) {
if (arr1[x] == arr2[y] && arr2[y] == arr3[z]) {
System.out.println(arr1[x]);
x++;
y++;
z++;
} else if (arr1[x] > arr2[y]) {
y++;
} else if (arr2[y] > arr3[z]) {
z++;
} else {
x++;
}
}
}
public static void main(String[] args) {
int[] arr1 = {1, 5, 10, 20, 40, 80};
int[] arr2 = {6, 7, 20, 80, 100};
int[] arr3 = {3, 4, 15, 20, 30, 70, 80, 120};
commonElementBruteForce(arr1, arr2, arr3);
commonElementsPerformant(arr1, arr2, arr3);
}
}
Question 79: What is Method Reference in Java 8?
Method reference is used to refer method of the functional interface. It is a compact and easy form of lambda expression. Each time when you are using a lambda expression to just referring a method, you can replace your lambda expression with a method reference. You can use those methods with the “::” double colon operator.
example:
ContainingClass::staticMethodName
Question 80: Tell me About SpringBoot’s Entry point. What it does? What component scan does? how to exclude any configuration?
Many Spring Boot developers like their apps to use auto-configuration, component scan, and be able to define extra configuration on their “application class”. A single @SpringBootApplication annotation can be used to enable those three features, that is:
@EnableAutoConfiguration: enable Spring Boot’s auto-configuration mechanism@ComponentScan: enable@Componentscan on the package where the application is located@Configuration: allow to register extra beans in the context or import additional configuration classes
The @SpringBootApplication annotation is equivalent to using @Configuration, @EnableAutoConfiguration, and @ComponentScan with their default attributes.
