This post is based on a Java backend developer interview simulation and provides direct transcript-based answers to help you prepare for similar interviews.
📺 This interview scenario was inspired by a video from the YouTube channel: CloudTech.
🔗 Watch the full video here: https://www.youtube.com/watch?v=yVj2EgwZxk4
1. Can you introduce yourself and talk about your tech stack and domains?
I have around 4.5 years of experience working as a Java developer. My core expertise is in Java and developing REST APIs using Spring Boot. I also have basic knowledge of microservices architecture. On the database side, I work with SQL and Oracle. Additionally, I have exposure to UI technologies like Angular, HTML, and CSS, but they are secondary skills. My primary focus is backend Java development.
2. Which version of Java are you currently working on?
We are currently using Java 8 but are in the process of upgrading to Java 17.
3. How would you rate yourself in Core Java out of 5?
I would rate myself a 4 out of 5 in Core Java.
4. What are the different ways to create threads in Java?
Threads can be created in three ways:
-
By extending the
Thread
class and overriding therun()
method. -
By implementing the
Runnable
interface and providing the implementation forrun()
. -
By using the Executor framework which manages a pool of threads.
5. What is the recommended way to create threads?
-
For multiple threads: use the Executor framework.
-
For a single thread and if the class is not extending any other class: extend
Thread
. -
In general, it's best to implement
Runnable
.
6. What is a deadlock and how can we avoid it?
Deadlock occurs when two or more threads are waiting on each other’s resources. For example, Thread 1 holds Resource A and waits for Resource B, while Thread 2 holds Resource B and waits for Resource A.
To avoid deadlock:
-
Use locks in a consistent order.
-
Use
tryLock()
fromjava.util.concurrent.locks
. -
Minimize synchronized blocks.
7. What are the thread states in Java?
-
New: Thread is created but not started.
-
Runnable: Thread is ready to run.
-
Blocked/Waiting/Timed Waiting: Thread is waiting to acquire a lock or a specific time.
-
Terminated: Thread has finished execution.
8. Can a terminated thread be restarted?
No, once a thread is terminated, it cannot be restarted. You can only create a new thread.
9. Difference between throw
and throws
?
-
throw
is used to explicitly throw an exception in the code. -
throws
is used in the method signature to indicate it may throw exceptions.
10. Types of exceptions in Java?
-
Checked exceptions: Checked at compile-time (e.g.,
IOException
). -
Unchecked exceptions: Runtime exceptions (e.g.,
NullPointerException
,ArrayIndexOutOfBoundsException
).
11. What is try-with-resources?
A try-with-resources block allows us to declare resources like BufferedReader
with the try
block. The resource is automatically closed after execution. The resource must implement AutoCloseable
.
12. Difference between ==
and .equals()
?
-
==
compares object references. -
.equals()
compares the content of objects.
Example:
String s1 = "abc";
String s2 = new String("abc");
s1 == s2 // false
s1.equals(s2) // true
13. Difference between abstract class and interface?
-
Abstract class can have both abstract and concrete methods.
-
Interface can have abstract methods, and from Java 8 onwards, also static and default methods.
-
A class can extend only one abstract class but can implement multiple interfaces.
14. What is Dependency Injection and how does Spring Boot support it?
Dependency Injection is a design pattern where dependencies are injected rather than created. Spring Boot supports it using annotations like @Autowired
or @Bean
.
15. What is circular dependency and how does Spring handle it?
Spring handles circular dependencies using proxy objects or by using @Lazy
annotation to lazily load dependencies. Best practice is to refactor code to avoid circular dependencies.
16. Difference between CrudRepository and JpaRepository?
-
CrudRepository
: Basic CRUD operations. -
JpaRepository
: ExtendsCrudRepository
and provides extra features like pagination and sorting.
17. What kind of repository do you use in your project?
We use JpaRepository
because it supports pagination and other features.
18. Best practices to improve Spring Boot performance?
-
Use caching to avoid frequent DB calls.
-
Apply proper indexing in DB.
-
Use Spring Boot Actuator for monitoring.
-
Use
@Async
for asynchronous processing.
19. How to convert a monolithic application to microservices?
-
Analyze and identify domain boundaries.
-
Break the application into independent services.
-
Use REST for synchronous communication, and Kafka/RabbitMQ for asynchronous.
-
Add API Gateway.
-
Use Service Discovery.
-
Apply CI/CD with Docker and Kubernetes.
-
Implement unit and regression testing.
20. Difference between synchronous and asynchronous communication in microservices?
-
Synchronous: Client sends request and waits (e.g., REST APIs).
-
Asynchronous: Client sends message and continues (e.g., Kafka, RabbitMQ).
21. Java 8 Program: Unique Words from Sentence
public class Client {
public static void main(String[] args) {
String str = "Java is fun and Java is powerful";
List<String> uniqueWords = Arrays.stream(str.split(" "))
.map(String::toLowerCase)
.distinct()
.sorted()
.collect(Collectors.toList());
System.out.println(uniqueWords);
}
}
Output: [and, fun, is, java, powerful]
📌 Conclusion
👉 If you found this useful, explore our other posts on:
-
Java Interview Questions
-
Spring Boot Real-World Scenarios
-
Microservices Implementation Guide
💬 Got any questions or tips to share? Drop them in the comments!
0 Comments