1.What is the difference between == and .equals() in Java?
---- Answer:
==
compares object references (memory location)
.equals()
compares object content (defined in the class)
- What is the difference between final, finally, and finalize() in Java?
Answer:
• final: Used to declare constants, prevent method overriding, or inheritance of classes.
• finally: A block that always executes after try and catch, used for cleanup operations.
• finalize(): A method called by the Garbage Collector before destroying an object (deprecated since Java 9).
- What is the difference between ArrayList and LinkedList in Java?
Answer:
• ArrayList uses a dynamic array to store elements.
• LinkedList uses a doubly linked list structure.
• ArrayList provides faster random access (O(1)) but slower insert/delete in the middle (O(n)).
• LinkedList has faster insert/delete (O(1) if node is known), but slower access (O(n)).
4.What is the difference between HashMap and Hashtable in Java?
💡 Answer:
• HashMap is non-synchronized — not thread-safe, but faster.
• Hashtable is synchronized — thread-safe, but slower.
• HashMap allows one null key and multiple null values.
• Hashtable does not allow null keys or null values.
• Both implement the Map interface and store data as key-value pairs.
5.What is the difference between String, StringBuilder, and StringBuffer in Java?
Answer:
• String is immutable – any change creates a new object.
• StringBuilder is mutable and not thread-safe, faster for single-threaded use.
• StringBuffer is mutable and thread-safe, slower due to synchronization.
6.What is the difference between Abstract Class and Interface in Java?
Answer:
• Abstract Class can have both abstract and concrete methods.
• Interface (since Java 8) can have abstract, default, and static methods.
• A class can extend only one abstract class, but can implement multiple interfaces.
• Interfaces are ideal for contract-based design, while abstract classes are used for shared base behavior.
- What is method overloading and method overriding in Java?
Answer:
• Method Overloading: Same method name with different parameters in the same class.
• Method Overriding: Same method name and parameters in child class, modifying behavior of the parent class method.
• Overloading is compile-time polymorphism, overriding is runtime polymorphism.
8.What is the difference between Comparator and Comparable in Java?
Answer:
Comparable is used to define the natural ordering of objects.
Comparator is used to define custom ordering outside the class.
Comparable is implemented by the class itself, while Comparator is a separate object.
- What is the difference between HashMap, LinkedHashMap, and TreeMap in Java? Answer: HashMap: No ordering of keys; allows one null key; fastest for most operations. LinkedHashMap: Maintains insertion order of keys. TreeMap: Maintains sorted order (natural or custom) using a Red-Black tree.
10.What is the difference between wait(), sleep(), and join() in Java?
Answer:
sleep() – Pauses the current thread for a specified time (doesn’t release lock).
wait() – Causes a thread to wait and releases the lock, to be resumed with notify() or notifyAll().
join() – Waits for another thread to finish before continuing.
11.What is a functional interface in Java? How is it related to Lambda Expressions?
Answer:
A functional interface has exactly one abstract method.
It can have default or static methods, but only one abstract method.
Lambda expressions are used to provide implementation for functional interfaces.
- What is the difference between throw and throws in Java?
Answer:
throw is used to explicitly throw an exception from a method or block.
throws is used to declare exceptions that a method might throw.
throw is followed by a single exception object.
throws is used in method signatures and can list multiple exceptions.
- What is the difference between static and instance variables/methods in Java?
Answer:
static belongs to the class, shared across all objects.
instance belongs to each object, unique for every object created.
static can be accessed without creating an object, instance requires an object.
- What is the difference between try-catch-finally and try-with-resources in Java?
Answer:
try-catch-finally is used to handle exceptions and ensure cleanup (e.g., closing resources).
try-with-resources (Java 7+) automatically closes resources that implement AutoCloseable.
It reduces boilerplate code and ensures safer resource handling.
- What are volatile and synchronized keywords in Java?
Answer:
volatile: A keyword that ensures changes to a variable are always visible to all threads.
Used when multiple threads read/write a variable.
Doesn’t provide atomicity — just visibility.
synchronized: A keyword used to lock a method or block so that only one thread can execute it at a time.
Ensures both visibility and atomicity.
Prevents race conditions in multithreaded environments.
- What are Vector and Stack classes in Java, and how are they different? Answer: Both Vector and Stack are part of the legacy collection classes (introduced before Java Collections Framework). Vector is a synchronized, growable array. Stack is a subclass of Vector and follows LIFO (Last-In-First-Out) order.
17.What is the transient keyword in Java?
Answer:
transient is a keyword used to exclude a field from serialization.
When an object is serialized, transient fields are not saved in the byte stream.
Used to protect sensitive data (like passwords) or skip non-critical info during serialization.
- What is the difference between Stack and ArrayDeque in Java? Which one should you use?
Answer:
Stack is a legacy class that extends Vector and is synchronized.
ArrayDeque is part of the modern Java Collections Framework and is faster, unsynchronized, and more efficient.
19.What is Optional in Java and why should you use it?
Answer:
Optional is a container object introduced in Java 8 to avoid NullPointerException.
It represents a value that may or may not be present.
Promotes cleaner, more null-safe code.
20.What are Lambda Expressions in Java and where are they used?
Answer:
Lambda expressions are a Java 8 feature that lets you write anonymous functions.
They are mainly used to implement functional interfaces in a concise and readable way.
Enable functional programming with less boilerplate code.
- What is the Stream API in Java and how is it useful?
Answer:
The Stream API, introduced in Java 8, allows processing collections in a declarative, functional style.
It supports operations like filter, map, reduce, collect, etc.
Streams do not store data, they operate on data.
22.What is Collectors in Java and how is it used with Stream API?
Answer:
Collectors is a utility class in Java 8 used to collect results from a stream into collections or summary statistics.
It works with the Stream’s collect() terminal operation.
- What is flatMap in Java Streams and how is it different from map?
Answer:
map() transforms each element in the stream into another object (1-to-1).
flatMap() is used when each element needs to be transformed into a stream, then all the resulting streams are flattened into a single stream (1-to-many).
24.What is groupingBy in Java Streams and how is it used?
Answer:
Collectors.groupingBy() is a collector used to group stream elements based on a classifier function (like a field or condition).
Returns a Map>, where K is the grouping key.
- What are Method References in Java and how are they different from Lambda expressions?
Answer:
Method References are shorthand notation of lambda expressions to call a method directly.
Syntax: ClassName::methodName or object::instanceMethod
Improves readability when the lambda is just calling a method.
here i just copied the questions from my old pdf also some ai
Top comments (0)