Java Developer Fresher Interview Questions with Answers
Core Java - OOPs (In-Depth)
Q: What is Encapsulation in Java?
A: Encapsulation is the wrapping of data and code into a single unit (a class). It helps protect the internal state of an
object from unintended or harmful changes.
Example:
class Person {
private String name;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
}
Q: What is Inheritance in Java?
A: Inheritance allows a class to acquire the properties and methods of another class using the 'extends' keyword.
Example:
class Animal {
void sound() { System.out.println("Animal sound"); }
}
class Dog extends Animal {
void bark() { System.out.println("Dog barks"); }
}
Q: What is Polymorphism in Java?
A: Polymorphism means the ability of an object to take on many forms. It can be compile-time (method overloading) or
runtime (method overriding).
Example (Overloading):
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
Example (Overriding):
class Animal { void sound() { System.out.println("Sound"); } }
class Cat extends Animal { void sound() { System.out.println("Meow"); } }
Q: What is Abstraction in Java?
A: Abstraction is the process of hiding implementation details and showing only the functionality. It is achieved using
abstract classes and interfaces.
Example:
abstract class Shape {
abstract void draw();
}
class Circle extends Shape {
void draw() { System.out.println("Drawing Circle"); }
}
Q: What is the difference between abstract class and interface in Java?
A: An abstract class can have both abstract and concrete methods, while an interface can only have abstract methods
(until Java 8, after which it can have default and static methods).
Abstract class supports constructors, interface does not.
Example:
abstract class Vehicle { abstract void run(); }
interface Movable { void move(); }
Java Developer Fresher Interview Questions with Answers
Core Java - Collections Framework (Basic)
Q: What is the difference between List and Set in Java?
A: List maintains insertion order and allows duplicates. Set does not allow duplicates and does not guarantee order
(unless it's LinkedHashSet).
Example:
List<String> list = new ArrayList<>();
Set<String> set = new HashSet<>();
Q: What is the difference between ArrayList and LinkedList?
A: ArrayList is backed by a dynamic array, good for fast random access. LinkedList is backed by nodes, better for
insert/delete operations.
Example:
List<String> arrayList = new ArrayList<>();
List<String> linkedList = new LinkedList<>();
Q: What is the difference between HashMap and Hashtable?
A: HashMap is non-synchronized and allows one null key. Hashtable is synchronized and doesn't allow null keys.
Example:
Map<String, String> map = new HashMap<>();
Q: What is Iterator in Java Collections?
A: Iterator is an interface used to iterate over collections.
Example:
Iterator<String> it = list.iterator();
while(it.hasNext()) { System.out.println(it.next()); }