1. OOPs Concepts & Real-World Examples
Q1: What is Encapsulation and how is it implemented?
Answer: Encapsulation is the mechanism of wrapping data (variables) and code acting on the data (methods) together as a single unit. It protects an object's internal state from unintended modification.
- Implementation: Declare class variables as
privateand providepublicgetter and setter methods with validation logic.
Q2: What is the difference between Method Overloading and Method Overriding?
2. Java Memory Model (Heap vs Stack)
Q3: How does JVM allocate memory between Stack and Heap?
- Stack Memory: Used for static memory allocation and the execution of threads. It contains primitive values specific to a method and references to objects in the Heap. Memory allocation follows LIFO (Last-In-First-Out) order and is very fast.
- Heap Memory: Used for dynamic memory allocation for Java Objects and JRE classes at runtime. Heap is shared across all running threads and is cleaned up by the Garbage Collector.
3. Strings & Immutability
Q4: Why is `String` immutable in Java?
- String Constant Pool (SCP): Multiple references can point to the exact same String literal in the pool, saving immense RAM.
- Security: Strings are widely used to store sensitive information (Database URLs, Passwords, Network sockets). If mutable, an unauthorized thread could alter the connection string.
- Thread Safety: Because the state cannot change, Strings are automatically thread-safe without explicit synchronization.
- HashCode Caching: The hashcode is computed once and cached, making Strings ideal keys for
HashMap.
4. Collections Framework & HashMap Internals
Q5: How does `HashMap` work internally in Java?
HashMap works on the principle of Hashing. It stores key-value pairs in an array of Node<K,V> buckets.
- When you call
map.put(key, value), JVM computeskey.hashCode(). - The index is calculated as
index = hash & (n - 1)wherenis the bucket array length (default 16). - If two different keys map to the same bucket index (Hash Collision):
- In Java 7: Stored as a singly Linked List ($O(N)$ lookup).
- In Java 8+: If the bucket size exceeds 8 elements, the LinkedList transforms into a Balanced Red-Black Tree ($O(\log N)$ lookup).
5. Exception Handling & Best Practices
Q6: What is the difference between Checked and Unchecked Exceptions?
- Checked Exceptions (Compile-Time): Exceptions that extend
java.lang.Exception(excludingRuntimeException). The compiler forces you to handle them usingtry-catchor declare them withthrows(IOException,SQLException,ClassNotFoundException). - Unchecked Exceptions (Runtime): Exceptions that extend
java.lang.RuntimeException. They occur due to logical bugs or improper API usage and do not require mandatory compile-time handling (NullPointerException,ArrayIndexOutOfBoundsException,ArithmeticException).
Q7: What is Try-With-Resources and why is it preferred?
Introduced in Java 7, Try-With-Resources automatically closes any resource that implements java.lang.AutoCloseable (such as Database connections, File streams, and Sockets) once the try block completes, preventing resource leaks.
6. Java 8 Streams & Lambda Expressions
Q8: What are Lambda Expressions and Functional Interfaces?
A Functional Interface is an interface with exactly one abstract method (e.g. Runnable, Callable, Predicate<T>, Function<T,R>, Consumer<T>). It is annotated with @FunctionalInterface.
Lambda expressions provide a clear and concise syntax to implement functional interfaces without writing anonymous inner classes.