What
are the key differences between HashMap and ConcurrentHashMap?
Answer:
HashMap is not synchronized and can be accessed by multiple threads
simultaneously, potentially leading to data inconsistency.
ConcurrentHashMap allows safe concurrent access by using segment-based
locking, ensuring thread safety without locking the entire map.
Explain
the difference between Comparable and Comparator in Java.
Answer:
Comparable is used for natural ordering by implementing the compareTo()
method in the class itself. Comparator is used to define custom ordering
outside the class by implementing the compare() method, allowing more
flexible sorting strategies.
What
is the Java Memory Model (JMM)?
Answer:
The Java Memory Model defines how threads interact through memory
and what behaviors are allowed in concurrent environments. It specifies
rules for visibility, ordering of reads/writes, and volatile, ensuring
consistent access to shared variables in a multithreaded environment.
What
are the differences between checked and unchecked exceptions in Java?
Answer:Checked exceptions are exceptions that must be handled or declared
in the method signature (e.g., IOException). Unchecked exceptions
(runtime exceptions like NullPointerException) do not need to be declared
or handled explicitly and can occur during execution.
Explain
the concept of volatile keyword in Java.
Answer:
The volatile keyword ensures visibility of changes to variables across
threads. When a variable is declared volatile, it guarantees that reads
and writes to that variable are directly done from and to the main
memory, ensuring the latest value is visible to all threads.
What
is the ForkJoinPool in Java, and how does it work?
Answer:
The ForkJoinPool is part of Java’s concurrency framework designed
to split tasks into smaller sub-tasks (fork) and then join their results
to improve performance in parallel processing. It is particularly useful
for divide-and-conquer algorithms like recursive tasks.
What
are the differences between String, StringBuilder, and StringBuffer?
Answer:
String is immutable, meaning its value cannot be changed after creation.
StringBuilder and StringBuffer are mutable. StringBuffer is thread-safe
(synchronized), while StringBuilder is not, making StringBuilder faster
in single-threaded environments.
Explain
the concept of garbage collection (GC) in Java.
Answer:Garbage collection is Java’s automatic memory management process,
which reclaims memory occupied by objects that are no longer referenced
by the application. The GC runs periodically, removing objects
from the heap that are no longer reachable to prevent memory leaks.
What
is the use of the transient keyword?
Answer:
The transient keyword is used to mark a variable as non-serializable.
When an object is serialized, the variables marked as transient are
excluded from the serialization process, meaning their values will not be
persisted.
What
is the difference between Executor and ExecutorService?
Answer:
Executor is a simple interface for executing tasks, while ExecutorService
extends Executor and provides more advanced features like thread pool
management, task lifecycle management (submit, shutdown), and future task
execution with results.
Explain
the difference between a soft reference and a weak reference in Java.
Answer:
A soft reference allows an object to be collected only when memory
is low, while a weak reference allows an object to be collected
more aggressively during garbage collection. Soft references are ideal
for caching, while weak references are useful for memory-sensitive
applications.
What
are the different types of class loaders in Java?
Answer:
The Bootstrap ClassLoader loads core Java classes (rt.jar). The Extension
ClassLoader loads classes from the ext directory. The Application
ClassLoader loads classes from the application’s classpath. Custom
class loaders can also be implemented for specific use cases.
How
does the try-with-resources statement work in Java?
Answer:
Introduced in Java 7, try-with-resources automatically closes
resources (like file streams) when the try block completes, reducing the
need for explicit finally blocks. Classes used in try-with-resources must
implement the AutoCloseable interface.
What
is the difference between Thread and Runnable?
Answer:
Thread is a class representing a thread of execution, and it can be
started directly by calling the start() method. Runnable is a functional
interface that defines a single run() method. Runnable can be passed to a
Thread constructor, allowing for more flexible and reusable thread
management.
What
are design patterns commonly used in Java?
Answer:
Common design patterns in Java include:
Singleton
(ensures only one instance of a class),
Factory
(provides a method to create objects),
Observer
(defines a subscription mechanism),
Builder
(helps construct complex objects step by step),
Decorator
(adds functionality dynamically to objects).
0 Comments