Java Programming Questions

The Java language and platform: type system, collections framework, generics, exceptions, the memory and JVM model, and idiomatic object-oriented Java. Covers writing correct Java and reasoning about its runtime and language semantics. A staple server-side and test-automation language surface.

EasyTechnical
40 practiced

In Java 8+, describe and (optionally) write a short JDBC code example that executes a simple SELECT and demonstrates correct resource management with try-with-resources. Show creating the Connection, PreparedStatement, executing the query, iterating the ResultSet, and handling SQLExceptions. Explain why try-with-resources prevents resource leaks compared to manual finally blocks.

MediumTechnical
38 practiced

Compare OFFSET/LIMIT pagination with keyset (cursor) pagination. For a posts table with columns id (PK) and created_at, write an example SQL query using keyset pagination to fetch the next page given the last seen cursor values. Describe how to implement keyset pagination in Java (Spring Data or manual SQL) and trade-offs when sorting by multiple columns.

MediumTechnical
56 practiced

Describe how you would instrument a Java Spring Boot REST endpoint to capture latency percentiles using Micrometer and export to Prometheus. Provide sample metric naming conventions and tag (label) choices, the Prometheus query to compute the 95th percentile over the last 5 minutes, and discuss pitfalls such as label cardinality and choosing histogram vs summary.

MediumTechnical
43 practiced

Design a caching strategy to reduce database load for user profile reads where updates must be visible immediately to subsequent reads (strong consistency). The service is Java/Spring Boot and uses a relational DB. Propose an architecture including cache type (distributed vs local), invalidation/eviction strategy, and how to implement atomic invalidation with database updates, considering multi-instance coherence.

MediumTechnical
45 practiced

Given the following simplified schema:

users(id BIGINT PRIMARY KEY, name TEXT, email TEXT)
orders(id BIGINT PRIMARY KEY, user_id BIGINT REFERENCES users(id), status VARCHAR(20), created_at TIMESTAMP, total_amount NUMERIC)
order_items(order_id BIGINT REFERENCES orders(id), product_id BIGINT, quantity INT)

Consider this query used by an API to fetch recent orders with total_amount > 100 for a given user, including item count:

SELECT o.id, o.created_at, o.total_amount, COUNT(oi.*) AS item_count
FROM orders o
JOIN order_items oi ON oi.order_id = o.id
WHERE o.user_id = ? AND o.total_amount > 100
GROUP BY o.id, o.created_at, o.total_amount
ORDER BY o.created_at DESC
LIMIT 50;

Explain what indexes you would add to improve performance for this query, why those indexes help, and any trade-offs (write amplification, storage). Also discuss possible query rewrites or schema changes (for example denormalizing item_count) to further optimize.

Unlock Full Question Bank

Get access to all 33 Java Programming interview questions and detailed answers.

Sign in to Continue

Join thousands of developers preparing for their dream job.