List of interview questions for different companies
Java Coding Q1) Get Avg salary of employees based on there department --- using java 8 class Employee { private int id; private String name; private String department; private double salary;
public Employee(int id, String name, String department, double salary) {
this.id = id;
this.name = name;
this.department = department;
this.salary = salary;
}
public String getDepartment() {
return department;
}
public double getSalary() {
return salary;
}
} import java.util.*; import java.util.stream.Collectors;
public class AvgSalaryByDept { public static void main(String[] args) {
List<Employee> list = Arrays.asList(
new Employee(1, "Abu", "IT", 50000),
new Employee(2, "Siddiq", "IT", 60000),
new Employee(3, "Rahul", "HR", 40000),
new Employee(4, "Ram", "HR", 45000),
new Employee(5, "Deepak", "Sales", 55000)
);
Map<String, Double> avgSalary =
list.stream()
.collect(Collectors.groupingBy(
Employee::getDepartment,
Collectors.averagingDouble(Employee::getSalary)
));
avgSalary.forEach((dept, avg) ->
System.out.println(dept + " -> " + avg)
);
}
} Q2) Anagram code --- Normal public class AnagramCharArray { public static void main(String[] args) {
String s1 = "listen";
String s2 = "silent";
boolean result = isAnagram(s1, s2);
if (result) {
System.out.println("Anagram");
} else {
System.out.println("Not Anagram");
}
}
public static boolean isAnagram(String s1, String s2) {
// Step 1: Length check
if (s1.length() != s2.length()) {
return false;
}
// Step 2: Convert to char arrays
char[] arr1 = s1.toCharArray();
char[] arr2 = s2.toCharArray();
// Step 3: Create frequency array (assuming lowercase a-z)
int[] freq = new int[26];
// Step 4: Increase count for first string
for (char c : arr1) {
freq[c - 'a']++;
}
// Step 5: Decrease count for second string
for (char c : arr2) {
freq[c - 'a']--;
}
// Step 6: Check all values are zero
for (int count : freq) {
if (count != 0) {
return false;
}
}
return true;
}
} Q3) Internal working of List Q4) Internal working of Set Q5) Primary and Qualifer Annotation Q6) what is abstraction Q7) Can we create concreate method in Interface Q8) When to use Abstract class and Interface Q9) Write a singleton design pattren with normal and with thread safe Q10) How did you handle exception in your project Q11) How to Create Global Exception Q12) What type of functional Interface have to used in your project Q13) How to create Custom Exception Q14) Java 8 features, which are the features you have used in your current project Q15) Optional classes, where you used this in you current project and what are the method present in the optional class Q16) What is stream API Q17) How to create custom functional interface
Q1) what is index Q2) How to create index on table
Java Coding Q1) using stream String = "My. name, is Abc" the output should be "yM. eman, si cbA";
import java.util.Arrays; import java.util.stream.Collectors;
public class ReverseEachWord { public static void main(String[] args) {
String s = "My, Name. Is Abc";
String output = Arrays.stream(s.split(" "))
.map(ReverseEachWord::reverseWordKeepPunctuation)
.collect(Collectors.joining(" "));
System.out.println(output);
}
private static String reverseWordKeepPunctuation(String word) {
char[] arr = word.toCharArray();
int left = 0;
int right = arr.length - 1;
while (left < right) {
if (!Character.isLetter(arr[left])) {
left++;
} else if (!Character.isLetter(arr[right])) {
right--;
} else {
char temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
left++;
right--;
}
}
return new String(arr);
}
}
Q2) Deadlock and RaceCondition Q3) Relationship between == and equals() Q4) How Auto configuration works in spring Q5) How to make a class imutable in Java Q6) Types of injections? difference between Constructor and field injection Q8) HashMap and LinkedHashMap differnce Q9) What is the role of consumer in kafka Q10) How to intialize actuator in springboot Q11) Logback in springboot Q12) How to specificy and update version in restapi's
πΉ Java Core & Collections
- Declare a 2D array and iterate over it using an enhanced for loop (for-each) in Java. Given: int[] arr = {1,2,2,3,3,3,4,4,4,4,5,5};
- Remove duplicates without using any additional array and fill remaining slots with null.
- Difference between throw and throws.
- Write a program to reverse an integer number.
- Explain internal working of HashMap.
- What is a lambda expression?
- What is a functional interface? Can it have static and default methods?
πΉ OOP Concepts
- Explain inheritance and polymorphism.
- Explain SOLID principles.
- What design patterns do you know in Java?
- Write a program to create a Singleton class.
- How can you optimize Singleton in a multithreaded environment? (using synchronized / double-checked locking)
- What is a synchronized block?
πΉ Java 8 / Streams / Collections
- Given a list: ["ram","sita","rahul", null, null, "deepak"] Convert it to output: [null, null, "ram","sita","rahul","deepak"] using Java 8.
- Print numbers from 1 to 100 using 3 threads (Executor Framework) and print thread names. πΉ Multithreading & Executor Framework
- What is the Executor Framework in Java?
- How do you create a fixed thread pool of size 3?
- How do you manage task execution using multiple threads?
πΉ Spring Boot / Microservices
- How can we configure timeout for a REST API?
- How to convert a monolithic application into microservices?
- What is dependency injection and what are its types?
- What will @Autowired do internally?
πΉ JPA / Hibernate
- What are criteria queries in JPA?
- Explain JPA Criteria API and its use cases.
πΉ Scenario Based / SQL / Design
- From a given employee table, find the manager having the maximum number of subordinates.
- Why do we store LDAP user personal data in DB tables after syncing? Is it really needed?
πΉ Additional Conceptual Questions
- Can we apply abstract to non-volatile variables?
- Explain Java 17 features you implemented in your project.
- Explain LRU Cache implementation using LinkedList (or other DS).
- java 17 features and what you implemented in your project.
- how to convert monolithic to microservices
- How can we implement LRU cache and what are all the steps and methods we have in it. i.e LinkedList
- can we perform abstract on non volatile instances
- what is executor framework in multithreading and what are all the methods present in it.
- new features indroduced in hashmap after java 8
- what is view
- can we create index on view
- types on index
- what is prop drilling, how can we handle it by using framework and tools
- redux and context api
- difference between fetch and axios
- print the dublicate elements using stream List names = Arrays.asList("Alice", "Bob", "Charlie", "Alice", "Bob", "David");
HashSet seen = new HashSet(); names.stream().filter(name-> !seen.add(name)).distinct().Collect(Collections.asList); 2) print only body data from this String str = "[ { "id": 1, "body": "First Item", "severity": 1, "status": 0 }, { "id": 2, "body": "Second Item", "severity": 2, "status": 1 } ]";
ObjectMapper mapper = new ObjectMapper(); JsonNode root = mapper.readTree(str);
for (JsonNode node : root) {
System.out.println(node.get("body").asText());
}
-
how can we use 2 dbs at one time @Configuration public class DataSourceConfig {
@Primary @Bean(name = "dataSource1") public DataSource dataSource1() { return DataSourceBuilder.create() .url("jdbc:mysql://localhost:3306/db1") .username("user1") .password("pass1") .build(); }
@Bean(name = "dataSource2") public DataSource dataSource2() { return DataSourceBuilder.create() .url("jdbc:mysql://localhost:3306/db2") .username("user2") .password("pass2") .build(); } }
-
write custom exception classes 5)functional interface methods 6)how we communicate between microservices
-
diff between resttemplate and webclient
-
ArrayList and LinkedList diff 9)which design pattren have you used in your project
-
create custom mutuable class
-
immutable classes in java
-
what are functional interface classes in java
-
what is concurrent modification exception
-
fail fast and fail safe, when to use this?
- Functional Interface
- Inheritance and it's type
- Polymorphism and it's types
- JDBC and Hibernate differnce
- can add or fetch queries in Hibernate, and it is
- monolithic and microservices differnce
- how microservices communicate with each other
- what is virtual dom in react
- what fregment
- what are props
- what is prop drilling, and how can we over come this
- What is state react, and how state management works
- what is redux
- create a table, where i has object with id, name and age and display this details in that table using react UI
-
What happens if we remove an element from a collection inside a for-each loop? Why does it cause ConcurrentModificationException?
-
Why do the methods clone(), notify(), and notifyAll() belong to the Object class and not to user-defined classes?
-
In a Singleton class, cloning can create multiple objects. How do you prevent cloning from breaking the Singleton pattern?
-
Can a Singleton class be serialized? If yes, how do you prevent multiple instances during deserialization?
-
In Spring Boot, can we replace @Controller and @Service annotations with @Component? If yes, why do we still prefer specialized annotations?
-
How can we configure timeout settings for a REST API in Spring Boot? Write the code.
-
Where do we use synchronous and asynchronous communication in microservices? Explain with a real-time example.
-
Which authentication mechanisms are you familiar with? Explain briefly.
- Filter age using Stream API
- list.stream().filter(s -> s.getPrice() > 100).collect(Collectors.toList());
- Write a program to find unique words from a sentence and sort them
- SOLID principles
- Explain MVC architecture in brief
- Java 8 features
- Java 17 features
- AOP (Aspect-Oriented Programming)
- Transient vs Volatile
- OAuth
- Performance tuning in Java
- Caching
- Spring Boot annotations
- Functional interface types
- Types of exceptions in Java
- How can we avoid finally block execution
- Deadlock condition
- Thread life cycle
- Spring Security architecture
- CompletableFuture
- Memory leaks in Java
- What do you mean by try-with-resources
- Abstract class vs Interface
- Dependency Injection
- What is design pattern you know or implemented
- How to implement Singleton pattern
- What if two separate threads try to create object of Singleton class
- HashMap vs Hashtable vs ConcurrentHashMap
- Fail-fast vs Fail-safe
- Types of operations in Stream API
- Best practices for performance tuning Spring Boot applications
β MySQL / Database ------------------------------------------------------------
- What is a View
- Triggers
- Sequence
- Joins
- What is Index
- Objects in MySQL
- Stored Procedure
- Indexing
- Types of Joins
- Query to find Second Highest Salary
- What is Index in Database
---------------------------------------------------------------
- SOLID principles
- Java 8 features
- Java 17 features
- Abstract class vs Interface
- Types of exceptions in Java
- Transient vs Volatile
- Try-with-resources
- Dependency Injection (concept)
- Circular dependency β what is it? how Spring handles it
- Thread life cycle
- Deadlock condition
- How can we avoid finally block execution
- Memory leaks in Java
- Collection framework
- HashMap vs Hashtable vs ConcurrentHashMap
- Fail-fast vs Fail-safe
- Functional interface types
- What design patterns do you know / implemented
- Singleton design pattern
- How to implement Singleton
- What if two threads try to create object of Singleton class
- Types of operations in Stream API
- Filter age using Stream API
-----------------------------------------------------------
- What is Index in database
- Indexing
- Views
- Triggers
- Sequence
- Objects
- Stored Procedure
- Joins
- Types of Joins
- Second highest salary (SQL query)
- Explain MVC architecture in brief
- AOP (Aspect Oriented Programming)
- OAuth
- Spring Boot annotations
- Spring Security architecture
- Caching (Spring caching / Redis)
- Performance tuning in Spring Boot
- Best practices to improve Spring Boot application performance
- Java program to get price greater than 100 using Stream
- Write a program to find unique words from a sentence and sort alphabetically
- Get first non-repeating character from a string (
"leetcode"βl)
- Find sum of all values in a list using Stream
- Get duplicate values from a list using Stream
Example list:
List<Integer> list = Arrays.asList(1,2,3,3,4,5,5);List list = Arrays.asList(1, 3, 5, 7, 9, 13, 23); int target = 5;
List list = new ArrayList<>(Arrays.asList(1,3,5,7,9,13,23)); int target = 5; int index = -1;
for (int i = 0; i < list.size(); i++) { if (list.get(i) == target) { index = i; break; } else if (list.get(i) > target) { list.add(i, target); index = i; break; } }
if (index == -1) { list.add(target); index = list.size() - 1; }
System.out.println("Index: " + index); System.out.println("List: " + list);
List list = new ArrayList<>(Arrays.asList(1,3,5,7,9,13,23)); int target = 5;
int low = 0, high = list.size() - 1; int index = -1;
while (low <= high) { int mid = (low + high) / 2;
if (list.get(mid) == target) {
index = mid;
break;
} else if (list.get(mid) < target) {
low = mid + 1;
} else {
high = mid - 1;
}
}
if (index == -1) { list.add(low, target); // correct sorted position index = low; }
System.out.println("Index: " + index); System.out.println("List: " + list);