Java Programming
Java Programming
June 11, 2025 at 08:36 AM
🔥 *Scenario-Based Java Interview Questions with Sample Answers* 💼☕ *1. Memory Leak in Application* *Q:* How would you detect and fix a memory leak in your Java application? *A:* Use tools like VisualVM or Eclipse MAT. Analyze heap dumps for unreachable objects still referenced. Common causes include static references and large collections. Fix by removing unused references, using try-with-resources, and monitoring garbage collection. *2. Slow API Response* *Q:* What would you check if a REST API becomes slow suddenly? *A:* Check DB query performance, thread pool usage, memory usage, and GC logs. Use caching (e.g., Redis) if DB is slow. Load test using JMeter and profile the code. *3. Concurrent Data Update Issue* *Q:* Two users update the same record. How do you avoid data inconsistency? *A:* Use optimistic locking with a `@Version` field in JPA. If the version mismatches, throw an exception and notify the user to refresh. *4. Retry Mechanism on API Failure* *Q:* How would you design a system to retry a failed HTTP call? *A:* Use Spring Retry with exponential backoff. Limit retry attempts, log failures, and consider fallback logic. *5. Circular Dependency in Spring* *Q:* Spring app fails due to circular dependency. How do you fix it? *A:* Identify beans from the stack trace. Use `@Lazy`, refactor bean relationships, or use setter injection instead of constructor injection. *6. File Upload Fails for Large Files* *Q:* What will you check if large file uploads fail? *A:* Increase `max-file-size` and `max-request-size` in Spring config. Use streaming and handle exceptions like `MaxUploadSizeExceededException`. *7. High CPU Usage in Production* *Q:* Your Java app shows high CPU usage. What do you do? *A:* Use `top` and `jstack` to find problematic threads. Analyze thread dumps for loops or blocking code. Optimize or rewrite heavy logic. *8. Securing Sensitive Data* *Q:* How do you secure passwords in Java applications? *A:* Use hashed passwords with BCrypt or Argon2. Avoid plain-text storage. Secure credentials using environment variables or tools like HashiCorp Vault. *9. Multi-threaded Data Processing* *Q:* How would you handle processing of 1 million records efficiently? *A:* Use `ExecutorService`, parallel streams, or Spring Batch. Break records into chunks and process in parallel while ensuring thread safety. *React ❤️ for more*
❤️ 👍 🦾 ♥️ 😂 42

Comments