17 Java Developer Interview Question Series-9(Experienced candidates 0–6 years)
This article will cover Java interview questions on, Core Java, Java-8, Functional programming, Spring Framework, Coding interview questions, and SQL database questions. Let's Dive into it.
Coding
Java 8 Stream API:
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()
.collect(Collectors.maxBy(Comparator.comparing(Employee::getSalary)));
Polymorphism coding puzzle? Guess the Output?
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");
}
}
Inheritance Coding problem, guess the output?
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();
}
}
Write a Program to find a common element from three integer ArrayList. eg. arr1, arr2, and arr3.
findCommon(int a[], int b[], int c[], int n1, int n2,int n3)
{
// three sets to maintain frequency of elements
HashSet<Integer> uset = new HashSet<>();
HashSet<Integer> uset2 = new HashSet<>();
HashSet<Integer> uset3 = new HashSet<>();
for (int i = 0; i < n1; i++) {
uset.add(a[i]);
}
for (int i = 0; i < n2; i++) {
uset2.add(b[i]);
}
// checking if elements of 3rd array are present in
// first 2 sets
for (int i = 0; i < n3; i++) {
if (uset.contains(c[i]) && uset2.contains(c[i])) {
// using a 3rd set to prevent duplicates
if (uset3.contains(c[i]) == false)
System.out.print(c[i]+" ");
uset3.add(c[i]);
}
}
}
Employee list, find employees based on location or city and sort in alphabet manners like a-z and each city employee’s salary should be sort max to min salary.
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
HashSet Internal Working?
Spring-Boot
Tell me About SpringBoot’s Entry point. What it does do? 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@Component
scan on the package where the application is located (see the best practices)@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,
Spring Bean Lifecycle?
How bean is injected in spring? which type of dependency injection is you will prefer?
Dependency injection is a pattern we can use to implement IoC, where the control is inverted in setting an object’s dependencies.
Connecting objects with other objects, or “injecting” objects into other objects, is done by an assembler rather than by the objects themselves.
In the case of constructor-based dependency injection, the container will invoke a constructor with arguments each representing a dependency we want to set. Spring resolves each argument primarily by type, followed by name of the attribute, and index for disambiguation. Let’s see the configuration of a bean and its dependencies using annotations: For setter-based DI, the container will call setter methods of our class after invoking a no-argument constructor or no-argument static factory method to instantiate the bean. Let’s create this configuration using annotations:
Tell me about the Java memory model. Memory leak in java? how to rectify that in java? how to use a profiler for that? out of memory error?
Memory Leak is a situation where there are objects present in the heap that is no longer used, but the garbage collector is unable to remove them from memory, and therefore, they’re unnecessarily maintained. A memory leak is bad because it blocks memory resources and degrades system performance over time.
Using Java profiling you can monitor different JVM parameters, including object creation, thread execution, method execution, and yes, garbage collection. also, Using tools that can detect memory leaks. Using heap dumps can also help.
Optional in java?
Optional is a new type introduced in Java 8. It is used to represent a value that may or may not be present. In other words, an Optional object can either contain a non-null value (in which case it is considered present) or it can contain no value at all (in which case it is considered empty).
An Optional object can have one of the following possible states:
- Present: The Optional object does not represent absence. A value is in the Optional object and it can be accessed by invoking get().
- Absent: The Optional object does represent the absence of a value; you cannot access its content with get().
Usage of the index in java? Advantages and disadvantages of it in Database?
index means the position in the ArrayList, with the first position being 0 (zero). This is the case for most programming languages
Advantage — Searching is easy and hassle-free.
Disadvantage- Insertion, and deletion are not easy.
Explain database joins.
How to write wrapper class in java?
What is JIT in java?
The Just-In-Time (JIT) compiler is a component of the JRE (Java Runtime Environment) that improves the performance of Java applications at run time. It helps improve the performance of Java programs by compiling bytecodes into native machine code at runtime.
Have you written the main method without static?
You can write the main method in your program without the static modifier, the program gets compiled without compilation errors. But, at the time of execution, JVM does not consider this new method (without static) as the entry point of the program.
Looking for more interview questions and answers?
Here are some recommended books i have used for my preparation.
You can get your copy here — Grokking the Spring Boot Interview:
Grokking the Spring Boot Interview [Free Sample Copy]: click here
You can get your copy here: Grokking the Java Interview
Grokking the Java Interview [Free Sample Copy]: click here