1/34
Comprehensive practice flashcards covering Core Java, Collections, Concurrency, SQL, Spring Boot, REST APIs, JPA/Hibernate, Microservices, and Testing based on the 0-2 Years Java Backend Interview Guide.
Why is Java considered platform-independent?
Java source code is compiled into bytecode, which a compatible JVM executes on different operating systems. Java is platform-independent, while JVM implementations are platform-dependent.
What is the difference between JDK, JRE, and JVM?
JVM executes bytecode. JRE traditionally means the JVM plus runtime libraries. JDK includes development tools such as javac and runtime components. Modern distributions do not always offer a separate JRE installation.
What are the four pillars of OOP and their roles?
Encapsulation protects and organizes state; inheritance extends existing behavior; polymorphism allows different implementations behind a common type; abstraction exposes essential behavior while hiding details.
What is the difference between method overloading and method overriding?
Overloading uses the same method name with different parameter lists and is resolved at compile time. Overriding provides a compatible implementation of an inherited instance method and uses runtime dispatch.
What is the difference between == and equals() in Java?
For primitives, == compares values; for references, it checks object identity. equals() checks logical equality when implemented that way. Overriding equals() requires implementing a consistent hashCode().
Why is String immutable in Java?
A String value cannot change after creation. Immutability supports safe sharing, reliable map keys, security, and string pooling. Operations like concat() return a new String.
How do String, StringBuilder, and StringBuffer differ?
String is immutable. StringBuilder is mutable and preferred for local string building. StringBuffer synchronizes its methods and is useful when synchronized mutable-string operations are needed.
What is the difference between an abstract class and an interface in Java?
An abstract class can have constructors, instance state, abstract methods, and concrete methods, but a class can extend only one class. A class can implement multiple interfaces, which define contracts and can include default and static methods.
What is the distinction between final, finally, and finalize()?
final restricts reassignment, overriding, or inheritance. finally is a cleanup block. finalize() is deprecated for removal and should not be used for resource cleanup; try-with-resources is preferred.
What is the difference between checked and unchecked exceptions?
Checked exceptions must be caught or declared. Unchecked exceptions extend RuntimeException and do not require catching or declaration.
How do ArrayList and LinkedList compare in performance?
ArrayList uses a resizable array and offers O(1) indexed access on average. LinkedList has O(n) indexed access but supports O(1) insertion and removal once a node is reached. ArrayList is usually the default choice.
How does HashMap work internally in Java?
HashMap uses a key's hash code to select a bucket, then uses hash and equality checks to locate entries. Collisions are handled with linked structures and, under certain conditions, tree-based structures, providing O(1) average operations.
What causes a ConcurrentModificationException?
It occurs when a collection detects structural modification during iteration outside the iterator's supported rules. It can be avoided by using Iterator.remove() or an appropriate concurrent collection.
What is a functional interface in Java 8?
An interface with exactly one abstract method. Examples of built-in functional interfaces include Predicate
What is the difference between map() and filter() in the Stream API?
filter() retains elements satisfying a condition. map() transforms each element into another value.
What is the difference between orElse() and orElseGet() in Optional?
orElse(value) evaluates its fallback expression eagerly, while orElseGet(supplier) invokes the supplier only if the Optional is empty, which is optimal for expensive fallback logic.
What is the difference between synchronized and volatile in Java concurrency?
synchronized provides monitor-based mutual exclusion and visibility guarantees. volatile provides visibility and ordering for reads/writes but does not make compound operations such as count++ atomic.
What is deadlock and how can its risk be reduced?
Deadlock occurs when threads wait indefinitely for locks held by one another. Risk can be reduced by acquiring locks in a consistent order, minimizing nested locks, keeping critical sections short, and using timed locks.
What is the difference between an INNER JOIN and a LEFT JOIN in SQL?
INNER JOIN returns matching rows from both sides. LEFT JOIN returns all left-side rows plus matching right-side data, filling unmatched right-side columns with NULL.
What SQL query finds the second-highest distinct salary from an employees table?
SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM employees); This returns NULL if no lower distinct salary exists.
What does the ACID acronym stand for in database transactions?
Atomicity, Consistency, Isolation, and Durability.
Why is constructor injection preferred for Dependency Injection in Spring?
Constructor injection reduces coupling, makes dependencies explicit, supports final fields for immutability, and simplifies unit testing without requiring the Spring container.
What is the difference between @Component, @Service, and @Repository in Spring?
All identify Spring-managed components. @Component is general-purpose, @Service denotes business logic, and @Repository denotes data access while supporting persistence exception translation where applicable.
What is the difference between @Controller and @RestController?
@Controller is used for MVC controllers returning views. @RestController combines @Controller and @ResponseBody so handler return values are written directly to the response body, often as JSON.
What is the difference between @Primary and @Qualifier in Spring?
@Primary marks a preferred default bean when several candidate beans exist, while @Qualifier selects a specific bean by name at the injection point.
What is Spring Boot Actuator?
A feature that provides operational endpoints for health, metrics, and application monitoring. Access to Actuator endpoints should be restricted to protect sensitive operational details.
What is the idempotency distinction between POST, PUT, and PATCH in REST APIs?
POST creates or initiates an operation and is non-idempotent. PUT completely replaces a representation and is idempotent. PATCH applies partial modifications to a resource.
What is the difference between authentication and authorization?
Authentication verifies who a user is, whereas authorization determines what permissions or resources that user is allowed to access.
What is the difference between findById() and getReferenceById() in Spring Data JPA?
findById() returns an Optional indicating if the entity exists. getReferenceById() returns a proxy reference without immediately querying the database; accessing its state triggers a query, throwing an exception if missing.
What is the N+1 query problem and how can it be resolved?
The N+1 problem occurs when an initial query loads records, followed by N separate queries to load related data for each record. It is resolved using fetch joins, entity graphs, DTO projections, or batch fetching.
What triggers a rollback by default in a Spring @Transactional method?
By default, unchecked exceptions (subclasses of RuntimeException) and Error trigger rollback, while checked exceptions do not.
What is the difference between JUnit and Mockito?
JUnit defines and runs test cases and assertions. Mockito creates mocks or test doubles for dependencies to isolate the class under test.
What are the three typical states of a circuit breaker in microservices?
Closed, open, and half-open.
What is the difference between Git merge and Git rebase?
Merge integrates branches and can create a merge commit, preserving historical branch context. Rebase replays commits on a new base, creating new commit IDs and a linear history.
How should backend duplicate submissions be prevented when a user submits a form twice?
Prevent duplicates on the backend using database unique constraints, idempotency keys, and transaction management, rather than relying solely on disabling client-side submit buttons.