Java Interview Question

 First Round

1.Why did you choose Java?
2.What are the features of Java?
3.What does “platform independent” mean?
4.What is the the difference between the == operator and the equals() method?
5.What is compile-time polymorphism?
6.What is OOP / what are the principles of OOP?
7.Are Collection, Set, List, Queue classes or interfaces?
8.What is the difference between List, Set, and Queue?
9.What is a Map in the collections framework?
10.What is the difference between HashMap and Hashtable?
11.What algorithms (or data structures) underlie the Java Collection framework?
12.If you have a large amount of data (e.g. millions of entries) and you need to perform update operations, which collection is most suitable?
13.What is the difference between arrays and collections?
14.What are varargs (variable arguments)?
15.What is the difference between String and StringBuffer?
16.If we create an object of a class, where is it stored (in memory)?
17.What is the DROP command in SQL?
18.What is the TRUNCATE command in SQL?
19.What is the DELETE command in SQL?
20What is the difference between DROP and TRUNCATE?

Second Round
Theory / Conceptual
Explain your project.
1.What is OOP (Object-Oriented Programming)?
2.What is multiple inheritance?
3.Can we achieve multiple inheritance in Java? If yes, how? If no, why not?
4.What are the problems or drawbacks of multiple inheritance?
5.What is multilevel inheritance?
6.What is the String constant pool (or interned string pool)?
7.How many ways/types can a String be created?
8.What does it mean for an object or data to be immutable?
9.Difference between String and StringBuffer?
10.What are final, finally, and finalize in Java?
11.What is a collection (in Java)?
12.Difference between HashMap and Hashtable?
13.What are the sublanguages of SQL (DDL, DML, etc.)?
14.What are the main categories of SQL commands?
15.Where do you use TCL (Transaction Control Language) commands like COMMIT, ROLLBACK, SAVEPOINT?

Coding / Programming
Q1.
Input: 3
Expected Output:
1 2 3 4 5
10 9 8 7 6
11 12 13 14 15

Q2.
Input:
char ch[] = {'a','b','a','c','c','s','a','b'};
Expected Output:
a-3
b-2
c-2
s-1


πŸ’‘ Java Spring Boot + Microservices Engineer 3+ Years Exp :: Interview Questions Asked @CGI πŸš€

1️⃣ Self Introduction
2️⃣ Explain about your current project ?
3️⃣ What are your roles & responsibilities as Java Developer
4️⃣ Difference between Abstract Class and Interface
5️⃣ Can we achieve multiple inheritance in Java ? How ?
6️⃣ Explain OOPs concepts with real-time examples
7️⃣ Difference between HashMap and ConcurrentHashMap
8️⃣ What is Java Stream API ? Give examples
9️⃣ How does Garbage Collection work in Java ?
πŸ”Ÿ Difference between Checked Exception & Unchecked Exception

1️⃣1️⃣ Explain Spring Boot Auto Configuration
1️⃣2️⃣ What are Spring Boot Starters ?
1️⃣3️⃣ Difference between @Component, @Service, @Repository
1️⃣4️⃣ How do you secure REST API in Spring Boot ?
1️⃣5️⃣ What is Spring Boot Actuator ? Where have you used it ?
1️⃣6️⃣ Difference between Monolithic vs Microservices Architecture
1️⃣7️⃣ How do microservices communicate with each other ?
1️⃣8️⃣ What is API Gateway ? Which one you used ?
1️⃣9️⃣ Explain Circuit Breaker Pattern (Resilience4j/Hystrix)
2️⃣0️⃣ How do you handle configuration management in Microservices ?

2️⃣1️⃣ How to implement service discovery ?
2️⃣2️⃣ What is difference between REST and gRPC ?
2️⃣3️⃣ Explain Saga Pattern vs 2 Phase Commit
2️⃣4️⃣ How to deploy Spring Boot microservices on AWS ?
2️⃣5️⃣ What are different ways of externalizing configuration in Spring Boot ?
2️⃣6️⃣ How do you monitor microservices in production ?
2️⃣7️⃣ What is difference between SQL & NoSQL DB ? Which one used in your project ?
2️⃣8️⃣ Explain CI/CD pipeline you worked on
2️⃣9️⃣ How do you handle logging & tracing in distributed systems ?
3️⃣0️⃣ What are the challenges you have faced in your project ?


Coding ---



 How do you reverse a string in Java using streams?

How does MVC work?
What is a REST API?
 Expalin the flow of a Rest API call from frontend to backend and response from backend to frontend.
Explain JWT token.
What is a Functional Interface?
Give an example of a java interface which uses a functional interface.
Write a code to implement Runnable using lambda.
Which is springboot default server and How to use another server in springboot?
What are profiles and how to use them?
How is bean Injection working?
How to inject a specific bean of the same object?
How to set up a discovery server for microservices?
What is the use of @Primary annotation?
Which method can be used to check if a service is up or not?
What is an optional class and its use?
Write SQL query to find highest salary.
What is a Rest template?
Tell me about your project and what is your day-to-day work?
How do microservices communicate internally?
What is SDLC?
What is a daemon thread?
What is volatile?
  • What is Functional Interface? What is Flat Map? What is Method Reference? What is stream API
  • What is Flat Map
  • What is Stream API

  • Soap and Rest
  • circuit breaker
  • system ressilece
  • spring data JPA vs hibernate

  • why some financial services prefer soap over rest.


  • How you handle logs in previous company
  • Ans. 

    In my previous company, I implemented structured logging and centralized log management for better monitoring and debugging.

    • Utilized SLF4J with Logback for logging, allowing for flexible configuration and log formatting.

    • Implemented log levels (INFO, DEBUG, ERROR) to control the verbosity of logs based on the environment.

    • Set up centralized logging using ELK stack (Elasticsearch, Logstash, Kibana) for real-time log analysis.

    • Created custom log formats to include contextual information like user ID and transaction ID for easier tracing.

    • Regularly monitored logs for anomalies and set up alerts for critical errors using tools like Prometheus and Grafana.


  •  How can you write custom exception in Java?
  • Ans. 

    To write a custom exception in Java, create a new class that extends Exception or a subclass of Exception.

    • Create a new class that extends Exception or a subclass of Exception.

    • Add a constructor to the custom exception class to pass a message to the superclass constructor.

    • Throw the custom exception using the 'throw' keyword in your code.


  • Q2. Write code to demonstrate the flow of Spring MVC
  • Ans. 

    Demonstrate the flow of Spring MVC through code

    • Create a controller class with request mapping annotations

    • Define methods in the controller to handle different requests

    • Configure the dispatcher servlet in web.xml

    • Create a view resolver to map logical view names to actual views




    ⚡ Core Java (OOP, Collections, Concurrency)
    1️⃣ Difference between HashMap and ConcurrentHashMap — how thread safety is achieved.
    2️⃣ How Java ensures immutability and why it matters in multi-threaded programming.
    3️⃣ How the Java Memory Model provides visibility & ordering guarantees.
    4️⃣ ThreadLocal vs synchronized blocks — use cases & differences.
    5️⃣ Designing a high-performance producer-consumer system using Java concurrency.

    🌱 Spring & Spring Boot
    6️⃣ Internal working of Spring Security (filters, authentication manager, context).
    7️⃣ Difference between @RequestParam, @PathVariable, and @RequestBody.
    8️⃣ Implementing global exception handling in Spring Boot REST APIs.
    9️⃣ How to build idempotent APIs in Spring Boot (key for payments).
    πŸ”Ÿ Effective use of @Transactional for data integrity.

    πŸ—‚️ JPA / Hibernate
    1️⃣1️⃣ Handling bulk inserts/updates efficiently in JPA.
    1️⃣2️⃣ Difference between persist(), merge(), and save().
    1️⃣3️⃣ Solving the N+1 select problem in Hibernate.
    1️⃣4️⃣ Optimistic vs Pessimistic locking in transactional systems.
    1️⃣5️⃣ Entity change auditing with Envers or custom auditing.

    πŸ—️ Microservices & Architecture
    1️⃣6️⃣ Handling distributed transactions (Saga, Outbox, 2PC).
    1️⃣7️⃣ Designing a secure API Gateway for microservices.
    1️⃣8️⃣ Ensuring retries & idempotency with downstream services.
    1️⃣9️⃣ Implementing rate limiting & throttling for high-traffic APIs.
    2️⃣0️⃣ Designing a scalable notification service for millions of messages.

    πŸ” Security & Compliance
    2️⃣1️⃣ Securely storing sensitive data (PCI DSS compliance).
    2️⃣2️⃣ Difference between OAuth2, OpenID Connect, and SAML.
    2️⃣3️⃣ Handling token expiration & refresh securely.
    2️⃣4️⃣ Data encryption at rest & in transit.
    2️⃣5️⃣ Adding fraud/anomaly detection hooks in services.

    🧠 DSA & System Design
    3️⃣1️⃣ Design a system to handle 100K TPS with idempotency guarantees.
    3️⃣2️⃣ Implement a thread-safe rate limiter.
    3️⃣3️⃣ Design a multi-factor authentication system.
    3️⃣4️⃣ Real-time detection of fraud/duplicate transactions in a payment stream.
    3️⃣5️⃣ Implement Luhn’s algorithm to validate credit card numbers.



    πŸš€ Postman SDE-1 Backend Interview Experience πŸš€
    πŸ’» Mode: Virtual Interview
    🎯 Experience Level: 1–3 Years

    The process had 3 rounds (all elimination) πŸ‘‡

    πŸ”Ή Round 1 – Core Java + DSA
    1️⃣ How would you handle a race condition in a multithreaded program?
    2️⃣ You have a list of 1M integers → find the first non-repeating number efficiently.
    3️⃣ Implement an LRU Cache with O(1) operations.
    4️⃣ Merge two sorted arrays in O(m+n) without extra space.
    5️⃣ Given a string, find the longest substring without repeating characters.
    6️⃣ How would you detect and resolve a deadlock in Java?
    7️⃣ Debugging: A Java app is showing OutOfMemoryError – how do you fix it?
    8️⃣ Write code to perform level-order traversal of a binary tree.

    πŸ”Ή Round 2 – Spring Boot + Microservices
    1️⃣ Walk me through what happens when you hit a Spring Boot API (Controller → Service → Repository).
    2️⃣ You deployed a service, but it’s taking too long to start → how do you optimize?
    3️⃣ How would you implement rate limiting in Spring Boot (per user / per API)?
    4️⃣ Your API depends on another service that fails intermittently → how do you add resilience & retries?
    5️⃣ Two microservices need to update data in different DBs → how do you ensure transaction consistency?
    6️⃣ How do you handle circular dependencies in Spring?
    7️⃣ You need to log every API request/response → what’s the best approach?
    8️⃣ If multiple services need a common config, how do you implement a centralized config system?

    πŸ”Ή Round 3 – Database + System Design
    1️⃣ A SQL query is running very slow in prod → how do you debug & fix it?
    2️⃣ Design a transactional money transfer API ensuring idempotency.
    3️⃣ In a high-traffic system, how do you ensure consistent reads & writes across DB replicas?
    4️⃣ How would you design a real-time notification service (email + push + retries)?
    5️⃣ How do you handle schema changes in production without downtime?
    6️⃣ Design a collaborative Postman collection sync system (like Google Docs).
    7️⃣ You need to store billions of API request logs – how would you design the DB + storage?



    🏒 Java Backend Developer Interview Questions
    πŸ’» Mode: Virtual – Technical Round
    πŸ‘€ Role: Senior Java Backend Developer – Spring Boot & Microservices

    🟦 Java
    1. What are the benefits of sealed classes introduced in Java 17?
    2. How do virtual threads in Java 21 improve request handling in high-load systems?
    3. What is the difference between Optional.map() and Optional.flatMap()?
    4. How does garbage collection differ in Serial, Parallel, and G1 collectors?
    5. How does var help with type inference in Java?
    6. Explain the difference between CopyOnWriteArrayList and ArrayList.
    7. What is the purpose of CompletableFuture in asynchronous programming?
    8. How does pattern matching for switch improve readability in Java 17+?
    9. What are text blocks, and how do they simplify working with JSON/XML?
    10. Explain immutability and how to enforce it in Java.

    🟦 Spring + Spring Boot
    11. How does dependency injection work in Spring Framework?
    12. What is the role of @Configuration and @Bean annotations?
    13. How do you configure application properties for multiple environments in Spring Boot?
    14. How do you secure Spring Boot REST APIs with JWT authentication?
    15. Explain how Spring Boot manages embedded servers like Tomcat/Jetty.
    16. How does Spring Boot Actuator help with monitoring and metrics?
    17. How do you manage transaction propagation in Spring?
    18. What is the difference between @ControllerAdvice and @ExceptionHandler?
    19. How do you enable asynchronous method execution in Spring Boot?
    20. How does Spring Boot integrate with cloud services (e.g., AWS S3, RDS)?

    🟦 Microservices
    21. How do you implement inter-service communication in microservices?
    22. What is the difference between API Gateway and Service Mesh?
    23. How do you handle eventual consistency in distributed systems?
    24. How do you implement rate limiting in a microservices ecosystem?

    🟦 Coding Questions
    25. Write a program to check if a number is a palindrome without converting it to a string.
    26. Implement a function to sort a stack using another stack.
    27. Write a program to find the maximum sum subarray (Kadane’s algorithm).

    🟨 Others
    28. What is the difference between Kafka topics and partitions?
    29. How do StatefulSets in Kubernetes differ from Deployments?
    30. Write a SQL query to get the 2nd highest salary in a department.
    31. What are readiness probes in Kubernetes, and how do they differ from liveness probes?
    32. How do you implement request tracing across multiple microservices?
    33. What is the difference between Docker volumes and bind mounts?
    34. How do you implement rolling updates in Kubernetes?



     Core Java (OOPs, Collections, Concurrency Basics)

    1️⃣ Explain difference between abstract class and interface (Java 8 default/static methods).
    2️⃣ How does HashMap work internally? (hashing, buckets, collisions).
    3️⃣ Difference between Comparable and Comparator with code example.
    4️⃣ How do you create an immutable class in Java?
    5️⃣ What is the difference between synchronized method, synchronized block, and Lock API?

    ---

    ♦️ Spring & Spring Boot

    6️⃣ Explain Spring Boot Starter dependencies and how they simplify configuration.
    7️⃣ Difference between @Autowired and @Qualifier.
    8️⃣ How do you handle global exception handling in Spring Boot REST APIs?
    9️⃣ How do you implement Spring Boot profiles for different environments?
    πŸ”Ÿ How do you secure Spring Boot REST APIs using JWT/OAuth2?
    ---

    ♦️ JPA / Hibernate

    1️⃣1️⃣ Difference between save(), saveOrUpdate(), and persist() in Hibernate.
    1️⃣2️⃣ Explain lazy loading and how to avoid LazyInitializationException.
    1️⃣3️⃣ How does @OneToMany and @ManyToOne mapping work?
    1️⃣4️⃣ How do you use pagination and sorting in Spring Data JPA?
    1️⃣5️⃣ Explain optimistic vs pessimistic locking in JPA.
    ---

    ♦️ Microservices & Architecture

    1️⃣6️⃣ Difference between monolithic and microservices architecture.
    1️⃣7️⃣ How do you implement service registry and discovery (Eureka/Consul)?
    1️⃣8️⃣ What is the role of API Gateway in microservices?
    1️⃣9️⃣ How do you handle configuration management across multiple services?
    2️⃣0️⃣ What is the circuit breaker pattern and where do you use it?


    ♦️ Cloud & DevOps Basics

    2️⃣1️⃣ What is the difference between Docker image and Docker container?
    2️⃣2️⃣ How does a CI/CD pipeline work in real-time projects? (Jenkins/GitLab CI)
    2️⃣3️⃣ What is load balancing and how is it achieved in cloud platforms?
    2️⃣4️⃣ How do you monitor a Spring Boot app (Actuator, Prometheus, ELK)?
    2️⃣5️⃣ Difference between vertical scaling and horizontal scaling with examples.


    ♦️ SQL / Database Knowledge

    2️⃣6️⃣ Write a SQL query to find the second highest salary from an Employee table.
    2️⃣7️⃣ Explain difference between INNER JOIN, LEFT JOIN and RIGHT JOIN.
    2️⃣8️⃣ How would you optimize a slow-running SQL query?
    2️⃣9️⃣ How do transactions and isolation levels work in RDBMS?
    3️⃣0️⃣ How do you handle database connection pooling in Spring Boot?

    ♦️ DSA & Coding (Accenture Special Focus)

    3️⃣1️⃣ Implement a program to remove duplicates from a list without using Set.
    3️⃣2️⃣ Write a program to reverse a linked list iteratively and recursively.
    3️⃣3️⃣ Design a simple REST API to fetch last 10 transactions of a user.
    3️⃣4️⃣ Implement a thread-safe LRU cache in Java.
    3️⃣5️⃣ Given a list of employee records, group by department and find average salary using Java Streams.



Comments

Popular posts from this blog

Two Sum II - Input Array Is Sorted

Comparable Vs. Comparator in Java

Increasing Triplet Subsequence