Christopher Anabo
Christopher Anabo
Senior Tech Lead
Christopher Anabo

Notes

Interview Question

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 list = new ArrayList<>(Arrays.asList(1, 2, 3, 4)); list.stream().forEach(num -> { if (num == 2) list.add(5); // ConcurrentModificationException });

This will throw a ConcurrentModificationException because the list is being modified during the Stream operation.


Question 2: Threads for Parallel Streams and Internal Working

  1. 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).

  2. 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

  1. Core Concepts:

    • The thread pool maintains a queue for tasks and a set of worker threads.
    • Threads can be active, idle, or terminated.
  2. 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.
  3. Checking Threads: Use methods like getActiveCount() and getPoolSize() to monitor the state.


Question 4: Java Memory Model (JMM)

The Java Memory Model defines how threads interact with memory:

  1. Main Components:

    • Heap: Shared memory for all threads (stores objects, class metadata).
    • Stack: Thread-specific (stores method call info and local variables).
  2. 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)

  1. PermGen Removal:

    • Replaced by Metaspace.
    • PermGen was fixed-size; Metaspace grows dynamically.
  2. 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

  1. GET: Retrieve data (idempotent).
  2. POST: Create a resource (non-idempotent).
  3. PUT: Update or replace a resource (idempotent).
  4. DELETE: Delete a resource (idempotent).
  5. PATCH: Partial update.
  6. HEAD: Retrieve headers only.
  7. OPTIONS: Check supported methods.

Question 8: DDoS Attack and Prevention

  1. Definition: A Distributed Denial of Service (DDoS) attack overwhelms an application with requests to disrupt normal operations.

  2. 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

  1. Method Reference: A shorthand for a lambda expression:

    list.forEach(System.out::println); // Equivalent to (x -> System.out.println(x))

  2. 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)

  1. Default Methods:

    • Can be overridden in implementing classes.
    • If multiple interfaces provide conflicting default methods, the implementing class must resolve the conflict.
  2. 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

  1. Array Vs ArrayList
    •  
  2. HashMap vs HashTable
  3. How HashMap works
  4. What is SOLID
  5. Different types of Design Patterns
  6. Diamond Problem
  7. Generic Erasure
  8. Load Balancer and Reverse Proxy
  9. PubSub pattern
  10. Stream API
  11. Functional Programming
  12. Completable Futures