Start Learning
Javaneer

Interview Prep

Common Java interview questions with clear, model answers - grouped by topic.

🧩Core Java

What's the difference between == and .equals()?+

== compares object identity (are they the same object in memory); .equals() compares value/equality. Use .equals() to compare object contents, especially Strings.

Why are Strings immutable in Java?+

Immutability makes Strings safe to share and cache (the String pool), thread-safe, and safe as HashMap keys, and prevents a class of bugs. Methods like toUpperCase() return new Strings.

What is the difference between an interface and an abstract class?+

An interface is a pure contract; a class can implement many. An abstract class can hold state, constructors, and shared code but a class extends only one. Prefer interfaces for capabilities, abstract classes to share code among related types.

🏗️OOP

What are the four pillars of OOP?+

Encapsulation (hide data behind methods), Inheritance (reuse/extend a parent), Polymorphism (one call, many behaviors via overriding), and Abstraction (show what, hide how).

If you override equals(), what else must you override?+

hashCode(). The contract requires equal objects to have equal hash codes; otherwise HashSet/HashMap misbehave. Records generate both correctly.

Composition vs. inheritance - which to prefer?+

Favor composition (has-a) over inheritance (is-a). Composition is more flexible, avoids brittle deep hierarchies, and sidesteps single-inheritance limits. Use inheritance only for genuine, stable is-a relationships.

🚀Collections & Generics

When would you use a List vs. Set vs. Map?+

List for ordered items with duplicates and index access (ArrayList); Set for unique items (HashSet); Map for key-value pairs with fast lookup (HashMap).

What is type erasure?+

Generics are a compile-time feature; type information is largely removed at runtime, so List<String> and List<Integer> are both just List at runtime. It kept generics backward-compatible.

Explain PECS.+

Producer Extends, Consumer Super. Use ? extends when reading values out of a structure, ? super when writing values in - maximizing flexibility while staying type-safe.

Concurrency

What is a race condition?+

When correctness depends on unpredictable thread timing - e.g. two threads running count++ (read-modify-write) can lose updates. Fix with synchronized, atomics, or by avoiding shared mutable state.

What does volatile do?+

It guarantees visibility: reads/writes go to main memory so all threads see the latest value. It does NOT make compound operations atomic - use AtomicInteger or synchronized for that.

What are virtual threads?+

Ultra-lightweight threads (Java 21+) that let you run millions of concurrent tasks with simple blocking-style code, ideal for high-concurrency servers.

🎯Design & Best Practices

Explain the SOLID principles briefly.+

Single Responsibility (one reason to change), Open/Closed (extend, don't modify), Liskov Substitution (subtypes usable as parents), Interface Segregation (small focused interfaces), Dependency Inversion (depend on abstractions).

What is dependency injection and why use it?+

Supplying a class's dependencies from outside rather than creating them internally. It reduces coupling and makes code testable (you can inject mocks). Frameworks like Spring automate it.

How do you prevent SQL injection?+

Use PreparedStatement with parameter placeholders (?) instead of concatenating input into SQL, so data can never alter the query's structure.