Skip to main content

Command Palette

Search for a command to run...

youtube java refference imp Q&A

Updated
5 min read
  1. Functional interface.

    → A functional interface in Java is an interface that contains exactly one abstract method, making it ideal for use with lambda expressions and method references.

    → A functional interface in Java cannot have two abstract methods.

    →You can still have:

    • Default methods

    • Static methods

    • But only one abstract method is allowed.

  1. Optional class:

    Optional is a container object introduced in Java 8 to represent nullable values.

    →It helps avoid NullPointerException by providing safe access to potentially missing data.

    → Common methods:

    • isPresent() – checks if a value is present.

    • get() – retrieves the value (unsafe if empty).

    • orElse(default) – returns value or default.

    • ifPresent(Consumer) – executes logic if a value exists.

  2. Map and flatmap:

    map() is for one-to-one transformations, while flatMap() is for one-to-many and flattening nested streams."

  3. How is Java Time api better than the old date calendar APIs:

    → It offers better parsing, formatting, and time zone handling with classes like LocalDate, ZonedDateTime, and DateTimeFormatter.

    →The Java Time API is immutable, thread-safe, and provides a cleaner, more intuitive design than the old Date and Calendar classes.

  4. What makes class immutable?

    →A class is considered immutable in Java when its state (i.e., its fields) cannot be changed after it's created. Here's what makes a class immutable:

    ✅ Key Rules for Immutability

    1. Make the class final – prevents subclassing, which could alter behavior.

    2. Make all fields private and final – ensures they’re set once and never changed.

    3. No setters – only getters to expose data.

    4. Initialize fields via the constructor only.

    5. Avoid exposing mutable objects directly – return copies or use defensive cloning.

  5. If a class contains date, how to ensure immutablity?

    →Since Date is mutable, we ensure immutability by making defensive copies in the constructor and getter. This prevents external code from modifying the internal state."

  6. What are intermediate operations and terminal operations?

    Intermediate operations in Java Streams are lazy and return another stream, like filter or map. While Terminal operations trigger execution and produce the final result, like collect, count, or forEach.

  7. What are design patterns

    → Design patterns are proven, reusable solutions to common software design problems. They help developers write cleaner, more maintainable, and scalable code by following best practices.

  8. What happens if we use request param and path param in the same method?

    → Use PathVariable for resource identification (e.g., user ID, product ID).

    → Use RequestParam for filters, sorting, and optional flags (e.g., active status, page number, search keyword).

  9. Can we have multiple controller advice?

    Allowed: Spring supports multiple @ControllerAdvice classes. Each one can handle exceptions, bind data, or apply advice to specific controllers.

  10. What are the different thread states?

    →In Java, a thread can exist in six different states: NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, and TERMINATED

    →A thread begins in NEW, then moves to RUNNABLE when started.

    →While running, it may pause in WAITING, TIMED_WAITING, or get BLOCKED by locks.

    →Once finished, it goes to TERMINATED and cannot be restarted.

  11. What are the significant in HashMap?

    →HashMap is significant because it provides fast lookups and updates using hashing, supports null keys/values, and is widely used for storing key-value pairs in applications like caching, configuration, and quick data retrieval

  12. When should I go for HashMap, and when should I go for ConcurrentHashMap?

    I use HashMap In single-threaded scenarios where performance is critical, and thread safety isn’t needed. For multi-threaded environments, I prefer ConcurrentHashMap because it provides built-in thread safety with better concurrency than synchronized maps, ensuring safe reads and writes without blocking the entire map.

  13. How does Microservice 1 talk to Microservice 2?

    → Microservices communicate either synchronously using REST/gRPC or asynchronously using message brokers like Kafka. For dynamic environments, we use service discovery and API gateways to manage communication.

  14. What is a circuit breaker pattern?

    → The Circuit Breaker Pattern is used in microservices to stop calls to a service that is failing repeatedly. It works in three states — closed, open, and half-open — to protect the system from cascading failures and allow recovery.

  15. Internal working of HashMap?

    A HashMap in Java stores data in key–value pairs using an internal array of buckets. Each bucket holds a linked list or tree of nodes. Keys are converted to a hash code, mapped to a bucket index, and collisions are resolved using chaining or tree structures


  1. What is a Unique Constraint in a database?

    → A unique constraint makes sure that the values in a column are not repeated. It allows only unique values. Unlike a primary key, you can have many unique constraints in a table, and they can allow NULL values.

  2. What is a primary key in a database?

    →A primary key uniquely identifies each record in a table, ensuring data integrity by enforcing uniqueness and non-null constraints.

  3. What is a foreign key in a database?

    →A foreign key is used to establish relationships between tables and maintain referential integrity, ensuring that child records always point to valid parent records.

  4. What is normalization?

    →Normalization is used to structure a database by removing redundancy and ensuring data integrity. It’s achieved through normal forms like 1NF, 2NF, and 3NF.

  5. Why do we use an index in a database?

    →We use indexes to improve query performance by allowing the database to quickly locate rows without scanning the entire table. They’re essential for large datasets, though they add overhead during write operations.