Here are some of the most commonly asked Java interview questions and answers, suitable for freshers and candidates with 1–3 years of experience.
Answer:
Java is a high-level, object-oriented, platform-independent programming language developed by Sun Microsystems (now Oracle). It follows the principle “Write Once, Run Anywhere (WORA)” because Java code runs on the Java Virtual Machine (JVM).
Answer:
Object-Oriented
Platform Independent
Secure
Robust
Multithreaded
High Performance (using JIT Compiler)
Distributed
Portable
Dynamic
Answer:
JVM (Java Virtual Machine) is responsible for executing Java bytecode. It provides platform independence by allowing the same bytecode to run on different operating systems.
Answer:
JDK (Java Development Kit) contains:
JVM
JRE
Compiler (javac)
Debugger
Development tools
It is used to develop Java applications.
Answer:
JRE (Java Runtime Environment) contains:
JVM
Core Java libraries
It is used only to run Java applications.
| JDK | JRE | JVM |
|---|---|---|
| Development Kit | Runtime Environment | Virtual Machine |
| Contains JRE | Contains JVM | Executes bytecode |
| Used for development | Used for running applications | Platform dependent |
Answer:
OOP is a programming paradigm based on objects that contain data and methods.
The four pillars are:
Encapsulation
Inheritance
Polymorphism
Abstraction
Answer:
Encapsulation means wrapping data and methods into a single class and restricting direct access using private fields with public getters and setters.
Answer:
Inheritance allows one class to acquire properties and methods of another class using the extends keyword.
Example:
class Animal {
void eat() {
System.out.println("Eating");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Barking");
}
}
Answer:
Polymorphism means one interface with multiple implementations.
Types:
Compile-time (Method Overloading)
Runtime (Method Overriding)
Answer:
Multiple methods having the same name but different parameters.
void add(int a, int b) { }
void add(double a, double b) { }
Answer:
A subclass provides its own implementation of a method already defined in the parent class.
Answer:
Abstraction hides implementation details and exposes only essential functionality.
Achieved using:
Abstract classes
Interfaces
| Abstract Class | Interface |
|---|---|
| Can have constructors | Cannot have constructors |
| Can have instance variables | Only constants |
| Supports partial abstraction | Supports full abstraction (traditionally) |
Uses extends | Uses implements |
Answer:
A constructor initializes an object and has the same name as the class with no return type.
Types:
Default Constructor
Parameterized Constructor
== and .equals()?Answer:
==
Compares object references.
For primitives, compares values.
.equals()
Compares object contents (if implemented).
Answer:
A String is an immutable object representing a sequence of characters.
Answer:
Security
Thread safety
Efficient string pooling
Better performance
| String | StringBuilder | StringBuffer |
|---|---|---|
| Immutable | Mutable | Mutable |
| Slow for modifications | Fast | Thread-safe |
| Thread-safe | No | Yes |
Answer:
Exception handling prevents abnormal program termination.
Keywords:
try
catch
finally
throw
throws
Checked
Checked at compile time.
Example: IOException
Unchecked
Occur at runtime.
Example: NullPointerException
Answer:
Multithreading allows multiple threads to execute simultaneously for better performance.
Answer:
The Java Collections Framework provides classes and interfaces to store and manipulate groups of objects.
Examples:
List
Set
Queue
Map
| ArrayList | LinkedList |
|---|---|
| Dynamic array | Doubly linked list |
| Fast random access | Fast insert/delete |
| Slower insertion | Slower access |
| HashMap | HashSet |
|---|---|
| Stores key-value pairs | Stores unique values |
| Keys are unique | Elements are unique |
| Allows one null key | Allows one null element |
final, finally, and finalize()?final: Prevents modification (variables, methods, classes).
finally: Block that executes after try/catch.
finalize(): Legacy method called before garbage collection (deprecated and not recommended).
Answer:
Garbage Collection automatically frees memory by removing objects that are no longer referenced.
this and super?this
Refers to the current object.
super
Refers to the parent class object.
| Modifier | Same Class | Same Package | Subclass | Other Package |
|---|---|---|---|---|
| public | ✔ | ✔ | ✔ | ✔ |
| protected | ✔ | ✔ | ✔ | ✘* |
| default | ✔ | ✔ | ✘ | ✘ |
| private | ✔ | ✘ | ✘ | ✘ |
*Accessible in subclasses across packages.
break and continue?break
Exits the loop completely.
continue
Skips the current iteration and continues with the next one.
Reverse a String
Check if a number is prime
Fibonacci series
Factorial of a number
Palindrome String
Remove duplicates from an array
Find the largest number in an array
Count vowels in a String
Sort an array
Swap two numbers without a third variable
These questions cover the core Java concepts that are commonly asked in technical interviews. If you’re preparing for a Java developer role, it’s also helpful to practice coding problems and be familiar with Java 8 features (Streams, Lambda expressions), JDBC, Collections, Multithreading, and basic SQL.