Java Interview Questions and Answers (Part 3 – Real-World & Advanced Scenarios)

Mou

October 29, 2025

💻 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?

TypeDescription
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?

FeatureOverloadingOverriding
DefinitionSame method name, different parametersSame method name & parameters, different class
TimeCompile-timeRuntime
Examplesum(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 TypeDescription
Shallow CopyCopies object reference, not full data
Deep CopyCreates 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?

OperatorComparesExample
==References (memory address)str1 == str2
equals()Actual contentstr1.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?

TypeMutableThread-safePerformance
StringNoYesSlow (creates new object)
StringBuilderYesNoFast
StringBufferYesYesSlightly 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?

InterfaceFunctional Interface
Can have many methodsHas only one abstract method
Introduced in Java 1.0Introduced in Java 8
Example: List, MapExample: 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?

InterfaceReturn ValueException Handling
RunnableNo return valueCan’t throw checked exception
CallableReturns a resultCan 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?

StreamsCollections
Works with data in a functional wayStores data in a structure
Lazy computationEager computation
Can’t modify elementsCan 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()?

MethodDescription
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?

Conceptmain()static block
ExecutionProgram entry pointRuns before main()
PurposeStart programInitialize 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?

  1. Declare class as final
  2. Make fields private and final
  3. 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 TypeDescription
StackStores local variables and function calls
HeapStores objects and instance variables

🔹 28. What is the difference between JDK, JRE, and JVM?

ComponentDescription
JDKJava Development Kit (includes JRE + tools)
JREJava Runtime Environment (for running code)
JVMJava 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?

TypeExecutionExample
SequentialRuns on a single threadlist.stream()
ParallelRuns on multiple threadslist.parallelStream()

🔹 32. What is the difference between String pool and heap memory?

ConceptDescription
String PoolStores unique String literals
HeapStores 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?

TypeDescription
Fail-fastThrows ConcurrentModificationException if modified
Fail-safeDoesn’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



Leave a Comment