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.

MediumTechnical
47 practiced

Implement a thread-safe token-bucket rate limiter in Java that supports per-user limits. Provide a class TokenBucketRateLimiter with a method boolean allowRequest(String userId). Requirements: thread-safe for concurrent requests, tokens refill at a rate tokensPerSecond with a max burst capacity, memory-bounded (evict state for inactive users), and reasonable performance for many users. You may provide code or pseudo-code (Java 8+).

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.

HardTechnical
39 practiced

A heap dump shows a large retained set attributed to org.example.cache.Cache$Node objects. Describe a methodical approach to identify the root cause, reproduce the leak locally, and remediate. Include tools to use (jmap, jcmd, Eclipse MAT), what to inspect in heap analyzers (GC roots, reference chains), common leak patterns (static collections, ThreadLocal, listener registrations), and tests or CI checks to prevent regressions.

EasyTechnical
49 practiced

Explain the difference between a primary key, a unique constraint/index, and a foreign key in a relational database. For each, give an example use case and discuss performance implications such as index usage for lookups and insert/update overhead. Also mention how each concept maps to JPA/Hibernate annotations.

EasyTechnical
41 practiced

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 Continue

Join thousands of developers preparing for their dream job.