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.
Implement or outline a thread-safe LRU cache in Java that supports O(1) get and put operations, an optional TTL per entry, and an eviction callback. Provide the public API and describe an implementation using ConcurrentHashMap and a concurrency-friendly eviction structure, or explain how to use existing libraries (e.g., Caffeine) and why they are preferable. Discuss correctness under concurrency and tests you'd write to validate behavior.
Design an idempotent POST endpoint in Java that creates orders and must safely support client retries. Propose an idempotency key scheme, the database schema to record processed idempotency keys and their responses, transaction boundaries to ensure correct behavior under concurrency, and a strategy to garbage collect old idempotency records.
Given this query and a simplified EXPLAIN ANALYZE output from Postgres, interpret the plan and explain why the query is slow. Suggest concrete fixes.
Query:
SELECT * FROM orders WHERE user_id = ? AND created_at > now() - interval '30 days';
EXPLAIN ANALYZE (simplified):
Seq Scan on orders (cost=0.00..50000.00 rows=100000 width=128) (actual time=0.01..1500.00 rows=2000 loops=1)
Filter: ((user_id = 123) AND (created_at > ...))
Rows Removed by Filter: 100000
Explain what this plan indicates, why Postgres is doing a sequential scan, and what specific indexes or schema/query changes you would apply to make the query efficient.
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.
Explain dependency injection in Java backend applications and compare constructor injection, setter injection, and field injection in Spring. Describe advantages and disadvantages of each approach, testability implications, and common pitfalls (for example circular dependencies). Provide a short Java (Spring Boot) example showing constructor injection for a Service that depends on a UserRepository.
Unlock Full Question Bank
Get access to all 33 Java Programming interview questions and detailed answers.
Sign in to ContinueJoin thousands of developers preparing for their dream job.