💻 Java Interview Questions and Answers (Part 3 – Real-World & Advanced Scenarios)
If you’ve mastered Java basics and collections, this final part will help you sound confident in real interviews.
Many interviewers love to test logical thinking, real-world use of OOP, and Java features like Streams, Lambda, and Design Patterns.
Let’s make these complex topics simple and beginner-friendly.
🔹 1. What is the difference between Java SE, Java EE, and Java ME?
| Type | Description |
|---|---|
| Java SE (Standard Edition) | Core Java for desktop and general-purpose applications |
| Java EE (Enterprise Edition) | Used for web and enterprise-level applications (Servlets, JSP, Spring, etc.) |
| Java ME (Micro Edition) | Used for mobile and embedded systems |
🔹 2. What is the Java Stream API?
Answer:
The Stream API (introduced in Java 8) is used for processing data in a functional way. It allows filtering, mapping, sorting, and collecting data easily.
Example:
✅ Output: [2, 4]
import java.util.*;
import java.util.stream.*;
class Example {
public static void main(String[] args) {
List<Integer> nums = Arrays.asList(1, 2, 3, 4, 5);
List<Integer> even = nums.stream()
.filter(n -> n % 2 == 0)
.collect(Collectors.toList());
System.out.println(even);
}
}
🔹 3. What are Lambda Expressions in Java?
Answer:
A Lambda Expression is a short way to write anonymous functions. It helps write cleaner code.
Example:
List<String> list = Arrays.asList("Java", "Python", "C++");
list.forEach(lang -> System.out.println(lang));
✅ Output:
Java
Python
C++
🔹 4. What is the difference between method overloading and overriding?
| Feature | Overloading | Overriding |
|---|---|---|
| Definition | Same method name, different parameters | Same method name & parameters, different class |
| Time | Compile-time | Runtime |
| Example | sum(int, int) and sum(double, double) | Parent and Child both define display() |
🔹 5. What is the use of the “final” keyword?
Answer:
- final variable: cannot be changed
- final method: cannot be overridden
- final class: cannot be inherited
Example:
final int x = 10;
🔹 6. What is the difference between shallow copy and deep copy?
| Copy Type | Description |
|---|---|
| Shallow Copy | Copies object reference, not full data |
| Deep Copy | Creates a new object with full data duplication |
🔹 7. What is garbage collection in Java?
Answer:
Garbage Collection (GC) automatically deletes unused objects to free memory.
You can suggest it using:
System.gc();
But it runs automatically in most cases.
🔹 8. What is the difference between equals() and == in Java?
| Operator | Compares | Example |
|---|---|---|
== | References (memory address) | str1 == str2 |
equals() | Actual content | str1.equals(str2) |
🔹 9. What is a Singleton Class?
Answer:
A Singleton class allows only one object to be created for the entire application.
Example:
class Singleton {
private static Singleton instance = new Singleton();
private Singleton() {}
public static Singleton getInstance() {
return instance;
}
}
🔹 10. What is the difference between String, StringBuilder, and StringBuffer?
| Type | Mutable | Thread-safe | Performance |
|---|---|---|---|
| String | No | Yes | Slow (creates new object) |
| StringBuilder | Yes | No | Fast |
| StringBuffer | Yes | Yes | Slightly slower than StringBuilder |
🔹 11. What are design patterns in Java?
Answer:
Design patterns are proven solutions to common software problems.
Common patterns:
- Singleton Pattern
- Factory Pattern
- Observer Pattern
- Builder Pattern
🔹 12. What is the Factory Design Pattern?
Answer:
It is used to create objects without exposing creation logic to the client.
Example:
interface Shape { void draw(); }
class Circle implements Shape { public void draw() { System.out.println("Circle"); } }
class ShapeFactory {
public Shape getShape(String type) {
if(type.equals("Circle")) return new Circle();
return null;
}
}
🔹 13. What are Java annotations?
Answer:
Annotations are metadata added to classes, methods, or variables to give instructions to the compiler or runtime.
Example:
@Override
void display() { ... }
🔹 14. What is the difference between interface and functional interface?
| Interface | Functional Interface |
|---|---|
| Can have many methods | Has only one abstract method |
| Introduced in Java 1.0 | Introduced in Java 8 |
| Example: List, Map | Example: Runnable, Callable |
🔹 15. What is an Optional class in Java?
Answer:
Optional is used to avoid NullPointerException.
It wraps a value that may or may not be present.
Example:
Optional<String> name = Optional.ofNullable(null);
System.out.println(name.orElse("No Name"));
🔹 16. What is the use of try-with-resources?
Answer:
Introduced in Java 7, it automatically closes resources like files or connections after use.
Example:
try (BufferedReader br = new BufferedReader(new FileReader("data.txt"))) {
System.out.println(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
🔹 17. What is the difference between Callable and Runnable?
| Interface | Return Value | Exception Handling |
|---|---|---|
| Runnable | No return value | Can’t throw checked exception |
| Callable | Returns a result | Can throw exceptions |
🔹 18. What is the Executor Framework?
Answer:
It is used to manage and control threads easily using a pool of threads.
Example:
ExecutorService service = Executors.newFixedThreadPool(3);
service.execute(new MyTask());
service.shutdown();
🔹 19. What are Streams vs Collections?
| Streams | Collections |
|---|---|
| Works with data in a functional way | Stores data in a structure |
| Lazy computation | Eager computation |
| Can’t modify elements | Can add/remove elements |
🔹 20. What is Java Reflection API?
Answer:
Reflection allows a program to inspect and modify classes, methods, and fields at runtime.
Example:
Class cls = MyClass.class;
Method[] methods = cls.getDeclaredMethods();
🔹 21. What is the difference between wait(), notify(), and notifyAll()?
| Method | Description |
|---|---|
| wait() | Makes thread wait until another thread calls notify() |
| notify() | Wakes up one waiting thread |
| notifyAll() | Wakes up all waiting threads |
🔹 22. What are generics in Java?
Answer:
Generics allow type safety in collections and methods.
Example:
ArrayList<Integer> list = new ArrayList<>();
list.add(10); // Only integers allowed
🔹 23. What is the difference between public static void main() and static block?
| Concept | main() | static block |
|---|---|---|
| Execution | Program entry point | Runs before main() |
| Purpose | Start program | Initialize resources |
Example:
static { System.out.println("Static block executed!"); }
🔹 24. What is immutability in Java?
Answer:
An immutable object cannot be changed once created.
Example: String is immutable.
🔹 25. How to make a class immutable?
- Declare class as
final - Make fields
privateandfinal - Don’t provide setters
Example:
final class Student {
private final String name;
Student(String name) { this.name = name; }
public String getName() { return name; }
}
🔹 26. What is Dependency Injection (DI)?
Answer:
It is a design principle where objects are provided their dependencies instead of creating them directly.
Common in Spring Framework.
🔹 27. What is difference between Stack and Heap memory?
| Memory Type | Description |
|---|---|
| Stack | Stores local variables and function calls |
| Heap | Stores objects and instance variables |
🔹 28. What is the difference between JDK, JRE, and JVM?
| Component | Description |
|---|---|
| JDK | Java Development Kit (includes JRE + tools) |
| JRE | Java Runtime Environment (for running code) |
| JVM | Java Virtual Machine (executes bytecode) |
🔹 29. What is a marker interface?
Answer:
An interface with no methods.
Used to indicate special behavior to JVM.
Example: Serializable, Cloneable.
🔹 30. What is functional programming in Java?
Answer:
Functional programming focuses on writing functions without changing state or data.
It became popular from Java 8 (with Lambda, Stream).
🔹 31. What is the difference between Parallel Stream and Sequential Stream?
| Type | Execution | Example |
|---|---|---|
| Sequential | Runs on a single thread | list.stream() |
| Parallel | Runs on multiple threads | list.parallelStream() |
🔹 32. What is the difference between String pool and heap memory?
| Concept | Description |
|---|---|
| String Pool | Stores unique String literals |
| Heap | Stores String objects created using new keyword |
🔹 33. What is ClassLoader in Java?
Answer:
It loads Java classes into memory dynamically at runtime.
Example: Bootstrap, Extension, and Application ClassLoaders.
🔹 34. What is the difference between fail-fast and fail-safe iterators?
| Type | Description |
|---|---|
| Fail-fast | Throws ConcurrentModificationException if modified |
| Fail-safe | Doesn’t throw exception (works on clone) |
Example: ArrayList → fail-fast, CopyOnWriteArrayList → fail-safe.
🔹 35. What are important Java 8 features?
- Lambda Expressions
- Stream API
- Optional Class
- Functional Interfaces
- Method References
- Default and Static methods in interfaces
🟢 Final Tips for Interview
✅ Focus on explaining concepts in simple words
✅ Always give small code examples
✅ Prepare real-world use cases (threads, collections, file handling)
✅ Keep revising OOP and Java 8+ features
✅ Stay confident even if you forget syntax — logic matters most