Java Interview Questions and Answers
1. What is an abstract class?
An abstract class can have both concrete and abstract methods. It cannot be instantiated directly
and must be extended by another class, which must provide implementations for any abstract
methods.
2. What are generics in Java?
Generics allow us to use classes and methods with a specific type, providing type safety.
3. Explain the heap and stack memory in Java.
Stack stores method execution and local variables, with each thread having its own stack. Heap
stores objects and is divided into younger and older generations for garbage collection.
4. What is synchronization?
Synchronization is used to restrict access to a variable when multiple threads are involved, ensuring
that only one thread can modify it at a time.
5. What are annotations in Java?
Annotations define metadata in Java. They help reduce compile-time errors. We can create our own
annotations using the @interface.
6. What are design patterns?
Design patterns are solutions to common software design problems. They are classified into three
main types: creational, structural, and behavioral.
7. What is multithreading?
Multithreading allows running multiple threads simultaneously in a program. It can be achieved by
extending the Thread class or implementing the Runnable interface.
8. What is dependency injection?
Dependency injection allows a class to receive its dependencies from an external source, promoting
loose coupling.
9. What is a functional interface?
A functional interface is an interface with a single abstract method. It can be implemented using
lambdas.
10. What are Stream APIs?
Stream APIs provide a way to process sequences of elements in a functional style, allowing for
operations like map, filter, and reduce.
11. Explain the singleton pattern.
The singleton pattern ensures that a class has only one instance and provides a global point of
access to it.
12. What is garbage collection?
Garbage collection automatically reclaims memory by removing unreferenced objects from memory.
13. What is the transient keyword?
The transient keyword prevents serialization of certain variables, meaning their state will not be
saved.
14. What is the volatile keyword?
The volatile keyword ensures visibility of changes to variables across threads, making them visible
to all threads.
15. Explain shallow copy and deep copy.
A shallow copy copies an object's references, while a deep copy creates a new object and copies all
values.