Java Interviews – Top Questions & Answers for Web Developers
Java is an object-oriented, platform-independent programming language used to build scalable, secure, and portable applications.
Because Java code is compiled into bytecode, which runs on the JVM, not directly on the OS.
JVM (Java Virtual Machine) executes Java bytecode. It provides memory management and OS abstraction.
JDK is a development kit containing JVM + JRE + compiler + tools for building Java applications.
JRE provides the environment required to run Java apps (JVM + libraries), but not compile them.
A platform-neutral intermediate code executed by the JVM.
Object-Oriented Programming organizes code using objects, enabling reusability and modular design.
- Encapsulation – hiding internal details
- Inheritance – reusing parent properties
- Polymorphism – many forms (method overloading/overriding)
- Abstraction – exposing only essentials
Binding data and methods together while restricting direct access using private fields and getters/setters.
Acquiring properties of a parent class using extends.
Ability of the same method to behave differently. Types: compile-time (overloading) and runtime (overriding).
Hiding complexity with abstract classes or interfaces.
| Feature | Abstract Class | Interface |
|---|---|---|
| Methods | abstract + concrete | abstract + default/static |
| Multiple Inheritance | No | Yes |
| Constructor | Yes | No |
Same method name, different parameters.
Same method signature in child class, providing new behavior.
A special method that initializes objects when created.
A no-argument constructor provided by Java if no other constructors exist.
Yes. Used in singletons and factory patterns.
Used to access parent class methods/constructors.
Refers to the current object; resolves naming conflicts.
A method belonging to the class, not objects.
No. They can be hidden, not overridden.
A constant whose value cannot change.
A class that cannot be extended.
A method that cannot be overridden.
Automatic memory cleanup for unused objects.
System.gc() (only a suggestion to JVM).
==compares references.equals()compares values
A namespace for organizing Java classes.
Unnamed package when no package is declared.
An event that interrupts normal program execution.
- Checked
- Unchecked
- Errors
Exceptions checked at compile time. Example: IOException.
Runtime exceptions like NullPointerException.
Block used to handle exceptions gracefully.
A block that always executes, even if exceptions occur.
User-defined exception extending Exception class.
Executing multiple tasks concurrently within a program.
A lightweight subprocess managed by JVM.
- Extending
Thread - Implementing
Runnable
Mechanism to prevent simultaneous access to shared resources.
Ensures only one thread executes within the block.
New → Runnable → Running → Blocked/Waiting → Terminated
Two threads wait on each other’s resources and never finish.
Background service thread (e.g., garbage collector).
Object whose state cannot change after creation (e.g., String).
- Mark class final
- Make fields private
- Provide getters only
- No setters
For security, caching, and thread-safety.
| Type | Mutable | Thread Safe | Speed |
|---|---|---|---|
| String | No | Yes | Slow |
| StringBuilder | Yes | No | Fast |
| StringBuffer | Yes | Yes | Slower |
JVM-managed area storing string literals to save memory.
A set of classes/interfaces for storing and processing data.
- List: ordered, allows duplicates
- Set: unordered, no duplicates
| Feature | ArrayList | LinkedList |
|---|---|---|
| Structure | Dynamic array | Doubly linked list |
| Speed | Fast read | Fast insert/delete |
- HashSet: unordered
- TreeSet: sorted
- HashMap: unordered, faster
- TreeMap: sorted keys
A collection storing key-value pairs.
No. Only one value per key.
Used to iterate collections safely.
Collection throws error when modified during iteration.
Iterator works on a copy and avoids exceptions.
Functional-style operations on collections.
A short way to write anonymous functions.
Interface with exactly one abstract method (e.g., Runnable).
A container to avoid NullPointerException.
Shortcut for calling methods using :: operator.
parallelStream()uses multiple threadsstream()is sequential
Java Database Connectivity for connecting Java apps with databases.
- Load driver
- Open connection
- Create statement
- Execute query
- Close resources
Reusing database connections to improve performance.
Java class handling web requests on a server.
Java Server Pages for embedding Java in HTML.
Server-side user information stored across requests.
Small piece of client-side data stored by the browser.
A lightweight architectural style using HTTP for communication.
Data exchange format used widely in REST APIs.
A modern framework for building enterprise Java applications.
Spring with auto-configuration and fast setup.
Injecting required objects instead of creating them manually.
Framework manages object creation instead of the program.
Object managed by the Spring container.
Pre-configured dependencies for faster development.
Simplifies database access using repository interfaces.
ORM tool mapping Java objects to database tables.
Loading related data only when accessed.
Loading related data immediately.
- Atomicity
- Consistency
- Isolation
- Durability
Ensures reliable database transactions.
Converting an object into a byte stream.
Reconstructing object from byte stream.
Prevents a variable from being serialized.
Inspecting classes, methods, fields at runtime.
Metadata providing instructions to compiler/JVM.
Type-safe programming avoiding casting issues.
Automatic conversion between primitives and wrapper classes.
Reverse of autoboxing.
Collections only store objects, not primitives.
Ensures variable visibility across threads.
Prevents concurrent access to a block or method.
Loads Java classes into JVM memory.
- Heap: stores objects
- Stack: stores method calls, local variables
Because it’s stable, secure, scalable, portable, and has a massive ecosystem.