๐ Top Java Interview Questions and Answers for Freshers (2025 Guide)
Java is one of the most popular programming languages for software development and is often a key topic in fresher interviews. Whether youโre preparing for your first job or a campus placement, understanding Java basics is essential.
This article covers the most commonly asked Java interview questions and simple answers โ written in a clear, easy-to-understand way.
๐น 1. What is Java?
Answer:
Java is an object-oriented, platform-independent, and secure programming language developed by James Gosling at Sun Microsystems in 1995.
Itโs used to build applications ranging from mobile apps to enterprise systems.
Example:
You write Java code once and can run it anywhere that has a JVM (Java Virtual Machine) โ this is called โWrite Once, Run Anywhere.โ
๐น 2. What are the main features of Java?
Answer: Some important features of Java are:
- Simple: Easy to learn and similar to C/C++.
- Object-Oriented: Everything is treated as an object.
- Platform-Independent: Runs on any operating system using JVM.
- Secure: Java has built-in security features.
- Robust: Handles errors and memory automatically.
- Multithreaded: Allows multiple tasks to run simultaneously.
๐น 3. What is the difference between JDK, JRE, and JVM?
| Term | Full Form | Description |
|---|---|---|
| JDK | Java Development Kit | Used for developing Java programs. Includes JRE + tools. |
| JRE | Java Runtime Environment | Used to run Java programs. Contains JVM and libraries. |
| JVM | Java Virtual Machine | Executes Java bytecode on any platform. |
๐น 4. What is the difference between Java and C++?
| Java | C++ |
|---|---|
| Platform-independent | Platform-dependent |
| Has automatic garbage collection | Manual memory management |
| Purely object-oriented (except primitives) | Supports both procedural and OOP |
| No pointers | Uses pointers |
๐น 5. What is an object in Java?
Answer:
An object is a real-world entity that has state (data) and behavior (methods).
For example, a Car object can have attributes like color and brand, and behaviors like start() or stop().
๐น 6. What is a class in Java?
Answer:
A class is a blueprint for creating objects. It defines properties (variables) and methods (functions) that an object will have.
Example:
class Car {
String color = "Red";
void start() {
System.out.println("Car is starting...");
}
}
๐น 7. What is the difference between primitive and non-primitive data types?
| Primitive | Non-Primitive |
|---|---|
| Stores simple values like int, char, float | Stores objects like String, Arrays, Classes |
| Fixed size | Variable size |
| Example: int a = 10; | Example: String s = “Hello”; |
๐น 8. What is the main() method in Java?
Answer:
The main() method is the entry point of every Java program.
Syntax:
public static void main(String[] args) {
System.out.println("Hello World!");
}
- public: Accessible everywhere
- static: Can run without creating an object
- void: No return value
- String[] args: Stores command-line arguments
๐น 9. What is an object-oriented programming concept (OOP)?
Answer:
OOP is a programming style based on real-world entities.
It focuses on classes, objects, inheritance, polymorphism, encapsulation, and abstraction.
๐น 10. What are the four main OOP principles?
- Encapsulation โ Binding data and methods into a single unit (class).
- Inheritance โ Reusing code from another class.
- Polymorphism โ One action, many forms (method overloading and overriding).
- Abstraction โ Hiding internal details and showing only essential features.
๐น 11. What is inheritance in Java?
Answer:
Inheritance allows one class to use 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..."); }
}
Here, Dog inherits behavior from Animal.
๐น 12. What is polymorphism?
Answer:
Polymorphism means โmany forms.โ It allows a single method to behave differently based on the object.
Two types:
- Compile-time (Method Overloading)
- Runtime (Method Overriding)
Example:
class Shape {
void draw() { System.out.println("Drawing shape"); }
}
class Circle extends Shape {
void draw() { System.out.println("Drawing circle"); }
}
๐น 13. What is method overloading?
Answer:
When multiple methods have the same name but different parameters, itโs called method overloading.
Example:
void add(int a, int b) { }
void add(int a, int b, int c) { }
๐น 14. What is method overriding?
Answer:
When a subclass provides a specific implementation of a method already defined in its parent class.
Example:
class Animal {
void sound() { System.out.println("Animal sound"); }
}
class Dog extends Animal {
void sound() { System.out.println("Bark"); }
}
๐น 15. What is encapsulation?
Answer:
Encapsulation is the concept of wrapping data and code together and keeping data safe from outside interference using private variables and getter/setter methods.
Example:
class Student {
private String name;
public void setName(String n) { name = n; }
public String getName() { return name; }
}
๐น 16. What is abstraction?
Answer:
Abstraction hides complex details and only shows essential features.
It can be achieved using abstract classes and interfaces.
Example:
abstract class Animal {
abstract void sound();
}
class Dog extends Animal {
void sound() { System.out.println("Bark"); }
}
๐น 17. What is an interface?
Answer:
An interface is a blueprint of a class that contains abstract methods.
A class implements an interface using the implements keyword.
Example:
interface Animal {
void eat();
}
class Dog implements Animal {
public void eat() { System.out.println("Dog is eating"); }
}
๐น 18. What is the difference between abstract class and interface?
| Abstract Class | Interface |
|---|---|
| Can have abstract and non-abstract methods | Only abstract methods (till Java 7) |
| Supports inheritance | Supports multiple implementations |
Uses extends keyword | Uses implements keyword |
๐น 19. What is a constructor in Java?
Answer:
A constructor is a special method used to initialize objects. It has the same name as the class and no return type.
Example:
class Car {
Car() {
System.out.println("Car object created");
}
}
๐น 20. What is the difference between constructor and method?
| Constructor | Method |
|---|---|
| Used to initialize objects | Used to perform operations |
| Same name as class | Any name allowed |
| No return type | Has a return type |
๐น 21. What is static in Java?
Answer:
The static keyword means that a member belongs to the class, not an instance.
Example:
class Test {
static int count = 0;
}
You can access it as Test.count.
๐น 22. What is the final keyword?
Answer:
The final keyword makes a variable, method, or class unchangeable.
- final variable โ cannot be reassigned
- final method โ cannot be overridden
- final class โ cannot be inherited
๐น 23. What is the difference between == and equals()?
| == | equals() |
|---|---|
| Compares memory address | Compares content of objects |
| Used for primitives | Used for objects like Strings |
Example:
String s1 = new String("Hi");
String s2 = new String("Hi");
System.out.println(s1 == s2); // false
System.out.println(s1.equals(s2)); // true
๐น 24. What is String in Java?
Answer:
A String is a sequence of characters.
Strings are immutable in Java, meaning once created, they canโt be changed.
Example:
String name = "Glimsy";
System.out.println(name.length());
๐น 25. What is the difference between StringBuffer and StringBuilder?
| StringBuffer | StringBuilder |
|---|---|
| Thread-safe | Not thread-safe |
| Slower | Faster |
| Introduced earlier | Introduced in Java 5 |
๐น 26. What is exception handling?
Answer:
Exception handling helps to handle runtime errors gracefully using try, catch, throw, and finally blocks.
Example:
try {
int a = 10 / 0;
} catch (Exception e) {
System.out.println("Error: " + e);
}
๐น 27. What is garbage collection?
Answer:
Garbage Collection automatically deletes unused objects to free up memory.
You can request it manually using:
System.gc();
๐น 28. What are access modifiers in Java?
Answer:
Access modifiers control visibility of classes and methods.
| Modifier | Scope |
|---|---|
| public | Everywhere |
| private | Within the same class |
| protected | Same package + subclasses |
| default | Only within package |
๐น 29. What is a package?
Answer:
A package is a group of related classes and interfaces.
It helps to organize code and avoid name conflicts.
Example:
package mypack;
public class Demo { }
๐น 30. What is multithreading in Java?
Answer:
Multithreading allows multiple tasks to run simultaneously, improving performance.
Example:
class MyThread extends Thread {
public void run() {
System.out.println("Thread running...");
}
}
๐ข Final Tips for Freshers
- Understand OOP concepts deeply โ they are asked in almost every interview.
- Practice basic programs like Fibonacci, Palindrome, and Factorial.
- Revise String, Array, and Exception Handling topics.
- Be confident in explaining your logic even if you donโt know the exact syntax.