Interview Question
Question 1: Simulating Concurrent Modification via Stream API
Concurrent modification can be simulated by modifying a collection while it is being processed by a Stream. For example:
java
List
This will throw a ConcurrentModificationException
because the list is being modified during the Stream operation.
Question 2: Threads for Parallel Streams and Internal Working
-
Threads Count:
Parallel streams use the ForkJoinPool.commonPool, which typically uses the number of threads equal to the number of available processors (Runtime.getRuntime().availableProcessors() - 1
). -
Internal Working:
- A stream splits the data into smaller chunks.
- Each chunk is processed by separate threads in the ForkJoinPool.
- Results from threads are combined in a merge phase.
To customize the pool:
java
System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism", "4");
Question 3: Internal Working of ThreadPoolExecutor
-
Core Concepts:
- The thread pool maintains a queue for tasks and a set of worker threads.
- Threads can be active, idle, or terminated.
-
How it Works:
- A task is submitted.
- If there are fewer active threads than the
corePoolSize
, a new thread is created. - If all threads are busy, the task is added to a queue.
- If the queue is full, a new thread may be created up to
maximumPoolSize
. - Threads idle beyond the
keepAliveTime
are terminated.
-
Checking Threads: Use methods like
getActiveCount()
andgetPoolSize()
to monitor the state.
Question 4: Java Memory Model (JMM)
The Java Memory Model defines how threads interact with memory:
-
Main Components:
- Heap: Shared memory for all threads (stores objects, class metadata).
- Stack: Thread-specific (stores method call info and local variables).
-
Key Concepts:
- Volatile ensures visibility of changes to variables across threads.
- Happens-before relationships dictate the order of operations.
- Synchronization (e.g., locks) is used to avoid race conditions.
Question 5: Changes in PermGen (JDK 8)
-
PermGen Removal:
- Replaced by Metaspace.
- PermGen was fixed-size; Metaspace grows dynamically.
-
Key Differences:
- PermGen stored class metadata, string pools, and method information.
- Metaspace allocates memory in native memory, avoiding
OutOfMemoryError: PermGen space
.
Question 6: Difference Between REST and RESTful Web Service
- REST: Architectural style for building APIs.
- RESTful Web Service: An implementation of REST principles in a service.
Feature | REST | RESTful WS |
---|---|---|
Protocol | Conceptual | Implementation via HTTP |
Statelessness | Assumed | Enforced |
Standards | Guidelines | W3C-compliant Web Services |
Question 7: HTTP Methods and Differences
- GET: Retrieve data (idempotent).
- POST: Create a resource (non-idempotent).
- PUT: Update or replace a resource (idempotent).
- DELETE: Delete a resource (idempotent).
- PATCH: Partial update.
- HEAD: Retrieve headers only.
- OPTIONS: Check supported methods.
Question 8: DDoS Attack and Prevention
-
Definition: A Distributed Denial of Service (DDoS) attack overwhelms an application with requests to disrupt normal operations.
-
Prevention:
- Rate Limiting: Restrict number of requests.
- CDN: Use services like Cloudflare.
- CAPTCHA: Prevent automated abuse.
- Firewall: Block malicious IPs.
Question 9: Method References and Functional Interfaces
-
Method Reference: A shorthand for a lambda expression:
list.forEach(System.out::println); // Equivalent to (x -> System.out.println(x))
-
Functional Interface: An interface with a single abstract method:
@FunctionalInterface interface Calculator { int calculate(int x, int y); }
Question 10: Overriding Rules for Default/Static Methods (Java 8)
-
Default Methods:
- Can be overridden in implementing classes.
- If multiple interfaces provide conflicting default methods, the implementing class must resolve the conflict.
-
Static Methods:
- Cannot be overridden.
- They belong to the interface, not the implementing class.
Design Patterns
Creational
Singleton | Only one instance of the object in the JVM |
Factory | JDBC - > You are injecting the factory in the client |
Abstract Factory | You are returning the factory to the client |
Builder | Like Lombok -> You are injecting the Builder object to object that you are creating |
Prototype | Creating the object by cloning the object |
Structural
Adapter | |
Bridge | |
Composit | |
Decorator | |
Facade | |
Flyweight | |
Proxy |
List - ordered
Set - unordered
- Array Vs ArrayList
- HashMap vs HashTable
- How HashMap works
- What is SOLID
- Different types of Design Patterns
- Diamond Problem
- Generic Erasure
- Load Balancer and Reverse Proxy
- PubSub pattern
- Stream API
- Functional Programming
- Completable Futures