Java Interview – Part 1: Model Answers (English)
Core Java – Basics
• String is immutable, while StringBuilder and StringBuffer are mutable. StringBuilder is faster but not
thread-safe, and StringBuffer is thread-safe but slower.
• Immutability means the value cannot be changed after creation. Any modification creates a new
object.
• The final keyword prevents inheritance, method overriding, or variable reassignment.
OOP Concepts
• The four principles are encapsulation, inheritance, polymorphism, and abstraction.
• Encapsulation hides internal details and exposes only what is necessary.
• Polymorphism allows the same interface to have different implementations.
Interface vs Abstract Class
• Interfaces define contracts, abstract classes provide shared behavior.
• Use interfaces for loose coupling and multiple implementations.
• Since Java 8, interfaces can have default method implementations.
Collections
• List allows duplicates, Set does not, and Map stores key-value pairs.
• HashMap uses hashCode and equals for fast data access.
• equals and hashCode ensure correct behavior in hash-based collections.
Exceptions
• Checked exceptions must be handled; unchecked exceptions occur at runtime.
• Custom exceptions represent business rules.
• Catching generic Exception hides real issues.
Java 8 – Streams & Optional
• Streams process collections in a functional and readable way.
• map transforms values, flatMap flattens nested structures.
• Optional represents the presence or absence of a value.
JVM
• Heap stores objects; Stack stores method calls and local variables.
• Garbage Collection automatically frees unused memory.
• OutOfMemoryError occurs when the JVM cannot allocate memory.
Concurrency
• A process contains threads; threads share memory.
• synchronized prevents concurrent access to shared resources.
• ExecutorService manages thread pools.
Spring Boot
• Dependency Injection allows Spring to manage dependencies.
• Component, Service, and Repository have semantic differences.
• Beans follow a lifecycle managed by Spring.
REST APIs
• Common HTTP methods are GET, POST, PUT, PATCH, and DELETE.
• PUT replaces a resource; PATCH updates part of it.
• Errors are handled using proper status codes and exception handlers.
JPA / Hibernate
• Entities map database tables; DTOs transfer data.
• Lazy loading loads data only when needed.
• The N+1 problem causes multiple unnecessary queries.
Transactions
• @Transactional ensures atomic execution.
• On failure, the transaction is rolled back.
• Commit saves changes; rollback discards them.
Security
• Authentication verifies identity; authorization verifies permissions.
• JWT is used for stateless authentication.
• Spring Security uses filters to intercept requests.
Testing
• Unit tests validate isolated code behavior.
• Mockito is used to mock dependencies.
• Focus tests on business logic.
CI/CD
• CI/CD automates integration and deployment.
• Code reviews improve quality and collaboration.
• Common tools include Git, GitLab CI, and Sonar.