Java Interview Preparation – Core Java, Spring Boot, SQL, and More
Prepare for your Java interview with a comprehensive set of interview questions covering Core Java, OOP Concepts, Collections and Generics, Exception Handling, Multithreading, JVM, Spring Boot, SQL, and Coding Challenges.
To succeed in a Java interview, it's essential to have a solid grasp of the core Java concepts, libraries, frameworks, and tools. This guide includes the top Java interview questions across various categories, helping you to prepare for your upcoming interviews.
1. Core Java Interview Questions
1.1 What are the key features of Java?
Answer:
- Object-Oriented: Everything in Java is an object.
- Platform Independent: Java uses JVM to make programs independent of the underlying platform.
- Multithreading: Java supports multi-threading for concurrent execution of tasks.
- Robust: Strong memory management, automatic garbage collection, and exception handling.
- Secure: Provides secure execution via JVM security mechanisms and bytecode verification.
1.2 Explain the difference between == and equals() in Java.
Answer:
==: Compares memory references for objects (checks if both references point to the same object).equals(): Compares the content of two objects (e.g., strings, collections).
1.3 What is the role of JVM in Java?
Answer:
The Java Virtual Machine (JVM) is responsible for:
- Loading class files.
- Verifying bytecode to ensure it is safe to execute.
- Interpreting or compiling bytecode into native machine code (Just-In-Time compilation).
- Garbage collection and memory management.
2. OOP Concept Questions
2.1 What are the four pillars of Object-Oriented Programming (OOP)?
Answer:
- Encapsulation: Bundling data and methods that operate on the data within a single unit (class). Access is controlled via getter and setter methods.
- Inheritance: A mechanism where one class inherits the properties and behaviors of another class.
- Polymorphism: The ability of a single method to perform different tasks based on the object that calls it.
- Abstraction: Hiding the complexity of the system and exposing only essential features to the user.
2.2 What is the difference between method overloading and method overriding?
Answer:
- Method Overloading: Defining multiple methods with the same name but different parameter lists (e.g., different number or types of arguments).
- Method Overriding: Redefining a method in a subclass that was already defined in the parent class, with the same signature and return type.
2.3 What is this keyword in Java?
Answer:
thisrefers to the current object instance within a class. It is used to access the current object’s fields, methods, or constructors.- It is also used to differentiate instance variables from local variables when they have the same name.
3. Collections and Generics Questions
3.1 What is the difference between ArrayList and LinkedList?
Answer:
- ArrayList: Uses dynamic arrays to store elements, provides fast access but slow insertion/removal (in the middle of the list).
- LinkedList: Uses doubly-linked nodes for storing elements, provides fast insertion/removal but slower access.
3.2 What is the difference between HashMap and TreeMap?
Answer:
- HashMap: Stores key-value pairs with no specific order (unordered).
- TreeMap: Stores key-value pairs in a sorted order (sorted by keys).
3.3 What is a Generic in Java?
Answer:
- Generics allow you to create classes, interfaces, and methods with type parameters, so you can write code that works for any object type while maintaining type safety.
- Example:
List<T>allows creating lists of different types likeList<String>,List<Integer>, etc.
4. Exception Handling Questions
4.1 What are the types of exceptions in Java?
Answer:
- Checked Exceptions: Exceptions that must be declared in the method signature or handled with a try-catch block (e.g.,
IOException). - Unchecked Exceptions: Runtime exceptions that are not checked at compile time (e.g.,
NullPointerException,ArrayIndexOutOfBoundsException).
4.2 How does try-catch-finally work in Java?
Answer:
try: Contains the code that might throw an exception.catch: Catches the exception if it occurs and handles it.finally: Always executes, regardless of whether an exception was thrown, often used for cleanup operations.
4.3 What is the difference between throw and throws in Java?
Answer:
throw: Used to explicitly throw an exception in the method body.throws: Used in method signatures to declare that a method might throw an exception.
5. Multithreading Questions
5.1 What is the difference between Thread and Runnable in Java?
Answer:
- Thread: A class in Java that represents a single thread of execution.
- Runnable: An interface designed to define the task (logic) to be executed by a thread. It is often used when you want to separate the task from the thread management.
5.2 What is a deadlock in Java?
Answer:
A deadlock occurs when two or more threads are blocked forever, each waiting for the other to release a resource they need to continue executing.
5.3 What is the purpose of synchronized keyword in Java?
Answer:
The synchronized keyword is used to control access to a method or block of code by multiple threads. It ensures that only one thread can execute the synchronized code at a time, thus avoiding data corruption.
6. JVM and GC Questions
6.1 What is JVM?
Answer:
Java Virtual Machine (JVM) is a runtime environment that allows Java bytecode to be executed on any platform. It performs several important tasks, such as loading class files, memory management, and garbage collection.
6.2 What is Garbage Collection (GC) in Java?
Answer:
Garbage Collection (GC) is the process of automatically reclaiming memory by identifying and removing objects that are no longer reachable or in use, preventing memory leaks.
6.3 What are the different types of memory areas in JVM?
Answer:
- Heap: Stores objects created by the program.
- Stack: Stores local variables and method calls.
- Method Area: Stores class-level data.
- Metaspace: Stores class metadata.
7. Spring Boot Interview Questions
7.1 What is Spring Boot?
Answer:
Spring Boot is a framework that simplifies the process of setting up and developing Java-based applications by providing auto-configuration, embedded servers, and production-ready defaults.
7.2 What is @SpringBootApplication annotation?
Answer:
The @SpringBootApplication annotation is a convenience annotation that combines:
@Configuration@EnableAutoConfiguration@ComponentScan
8. SQL and Database Questions
8.1 What is the difference between INNER JOIN and OUTER JOIN?
Answer:
INNER JOIN: Returns rows with matching values in both tables.OUTER JOIN: Returns rows with matching values in one table andNULLvalues in the other table if there is no match.
8.2 What is a foreign key in SQL?
Answer:
A foreign key is a column or set of columns in one table that uniquely identifies a row in another table. It establishes a relationship between two tables.
9. Coding Questions
9.1 Write a program to reverse a string in Java.
Answer:
9.2 Write a program to check if a number is prime.
Answer: