These 64 Java Full Stack questions answer common beginner questions about Java, Spring Boot, Databases and Testing. Use the topic links to find a short explanation, then try the practice exercise to apply it.
Explore the ScholarLogic Java Full Stack course in Bengaluru, based in BTM Layout, or try the beginner practice exercise.
Core Java
What is the difference between an abstract class and an interface?
An abstract class can share instance state and constructors. An interface defines a contract that multiple unrelated classes can implement.
What does encapsulation mean in Java?
Expose intentional operations and keep internal state behind them so callers cannot break invariants directly.
Why should equals and hashCode agree?
Objects that are equal must have the same hash code so hash-based collections can find them correctly.
When should I use a HashSet in Java?
Use it for uniqueness and membership, provided elements have consistent equality and hashing. It does not guarantee iteration order.
How does LinkedHashMap differ from HashMap?
LinkedHashMap offers a defined encounter order, normally insertion order. HashMap provides no order guarantee.
What is a Java generic type useful for?
It expresses type constraints such as a list of strings and catches incompatible uses at compile time.
What is an overloaded Java method?
It has the same name as another method but a different parameter list. Return type alone cannot distinguish overloads.
What is method overriding in Java?
A subclass provides behavior for an inherited instance method with a compatible signature. Use Override to catch mistakes.
What does try-with-resources do?
It closes declared AutoCloseable resources when the block exits, including when an exception occurs.
When should Java use BigDecimal?
Use it for decimal calculations requiring explicit precision, such as money. Choose rounding rules and avoid constructing from an imprecise double.
How should a Java API represent a timestamp?
Use an instant or offset-aware type for moments and a date type for dates without a time. Document the wire format.
Why prefer immutable values for API data?
They reduce surprising changes after validation and can simplify reasoning about shared values.
What is a Java lambda expression?
It supplies behavior for a functional interface, such as a predicate used to filter a list.
What is a Java comparator used for?
It defines an ordering, for example sorting tasks by due date and then by ID.
What is the difference between map and flatMap in a stream?
Map produces one value per item. FlatMap can produce a stream per item and flatten those streams together.
When is a Java stream consumed?
A terminal operation such as collect consumes it. Create a new stream if you need another traversal.
Spring Boot APIs
How does Spring map a request to a handler?
Controller annotations specify a path and HTTP method. Spring chooses a matching method and binds request data.
What does RequestBody do in Spring?
It asks Spring to deserialize the request body into a method argument using a configured converter.
How does RequestParam differ from PathVariable?
RequestParam typically reads a query parameter. PathVariable reads a value embedded in the route path.
When should a Java API use HTTP 204?
Use it for a successful operation with no response body, such as a deletion with nothing further to return.
When should a Java API return HTTP 401 versus 403?
Use 401 when authentication is missing or invalid. Use 403 when the request is understood but access is forbidden.
What does HTTP PUT mean in a REST API?
It requests creation or replacement of the resource at the target URI. Repeating an identical PUT should have the same intended effect.
When should I use PATCH in a Spring API?
Use it for a documented partial update. Define how absent fields and explicit null values differ.
Why return a Location header after creating a record?
It lets the client identify the newly created resource directly, commonly alongside a 201 response.
How can Spring centralize API error responses?
Use an exception handler or controller advice to map known errors to consistent status codes and safe response bodies.
How do I prevent repeated POST requests from creating duplicates?
Accept an idempotency key or enforce a suitable business uniqueness rule, with atomic handling of concurrent requests.
What is Spring Boot auto-configuration?
It configures common components according to dependencies, properties and existing beans. Override defaults when your app needs explicit choices.
What does a Spring component scan do?
It finds eligible annotated classes within configured packages and registers them as beans.
When is constructor injection useful in Spring?
It makes dependencies explicit and allows required fields to be initialized when the object is created.
What is externalized configuration in Spring Boot?
It lets one application use different configuration across environments through properties, environment variables or other supported sources.
How do I keep secrets out of a Spring Boot project?
Read them from controlled environment settings or a secret store. Avoid committing or logging their values.
What does a useful Java API contract describe?
It gives routes, fields, statuses and examples, including validation and authorization failures.
Persistence and concurrency
What is a JPA entity identifier?
It identifies a persistent entity. Map it to a stable key and choose a generation strategy that fits the database.
What is the JPA persistence context?
It tracks managed entities within its scope and coordinates changes with database writes.
What does lazy loading mean in JPA?
Related data can be loaded only when accessed. Plan access within a valid persistence scope and watch query counts.
What is the N+1 query problem in a Java API?
A list query is followed by a query for each item. Fetch only the required relationships with an appropriate query.
What is a projection in Spring Data?
It returns the fields needed for a particular view or operation, reducing unnecessary data transfer.
Why use a foreign key in the database?
It prevents references to missing parent records according to the configured rules.
How do I safely add a unique rule to a populated database?
Identify and resolve existing duplicates before adding the constraint. Keep application changes compatible during rollout.
How can a database migration be versioned in Java projects?
Use a migration tool such as Flyway or Liquibase with reviewed scripts stored alongside the application.
What is a pessimistic lock useful for?
It can serialize conflicting access to a row when the database lock is held. Keep transactions short and handle waits.
What is a lost update?
It happens when a stale write overwrites another change. Use version checks or a suitable atomic update.
Why should a transaction avoid slow network calls?
The database may hold locks while it waits. Separate the external call or use a design that records work for later delivery.
What is a database deadlock?
Transactions wait on each other in a cycle. The database usually aborts one, which may be retried when safe.
When should a Java API return a paged result instead of all records?
Use a page for potentially large collections. Cap page size and make ordering deterministic.
What is a soft delete?
It marks a record as deleted while retaining it. Queries and unique rules must handle that state intentionally.
How do I avoid SQL injection in Java?
Bind values as parameters instead of concatenating input into SQL. Validate any requested sort field against a fixed list.
Why test Java persistence against the intended database?
Dialects, constraints and locking can behave differently from an in-memory substitute.
Testing and delivery
What is a JUnit assertion?
It compares an observed result with the expected behavior and fails the test when the condition does not hold.
When is a parameterized JUnit test useful?
Use it when the same behavior needs checking against several meaningful input and expected-output pairs.
What does MockMvc help test?
It exercises Spring MVC request handling without starting a real HTTP server. Check status, headers and response fields.
How do I test authorization in a Java API?
Exercise the same route with anonymous, allowed and disallowed users, then verify no forbidden write happened.
What does a contract test check for an API?
It checks that a provider and its consumers agree on request and response behavior.
Why use a migration smoke test?
It verifies that the target schema supports common reads and writes after deployment.
What is Maven’s test phase for?
It runs configured unit tests during the build lifecycle. Integration test behavior depends on plugin configuration.
Why commit a Maven or Gradle wrapper?
It makes the build tool version consistent across developers and CI.
How do I investigate a Spring Boot startup failure?
Read the first relevant cause and check missing configuration, port conflicts and database connectivity.
What does a Spring Boot Actuator health endpoint report?
It reports application health and can include configured checks. Restrict access to details as appropriate.
Why use a correlation ID in a Java service?
It helps connect logs for the same request across components. Generate or validate it at a trusted boundary.
How do I test a browser frontend connected to Spring?
Exercise a real request flow, including a validation error and an empty result, using a safe test environment.
What should happen when a client loses its connection during a write?
The client should treat the result as uncertain. Use a request ID or idempotency mechanism to check or retry safely.
What should a Java portfolio API demonstrate?
Show a reproducible request flow, validation, data persistence and at least one error case.
How do I document Java API setup for a reviewer?
State the required JDK, build command, database setup, sample configuration and a request the reviewer can run.
How can I make a Java release easier to roll back?
Version the artifact and configuration, and plan schema changes so the previous app version can still run during rollback.
For further reading, see the official reference and compare ScholarLogic’s six courses.