Top 30 Java Interview Questions for Freshers | Skill Hives
thumnail

Top 30 Java Interview Questions for Freshers

  • By Devraj

  • 8th October 2026

Starting your career with Java? Interviewers for fresher roles ask basic questions to check if you understand the concepts, not just memorised lines. This guide by Skill Hives explains each of the 30 most common questions in simple words, with a real-life example and code, so you can explain it confidently in front of the interviewer.

How to prepare: Read the explanation, understand the easy example, and then explain it in your own words. Type the code yourself and run it. If you can teach it to a friend, you are ready.

Want structured Java training before your interview?

Skill Hives offers hands-on Java and programming training with real projects, not just theory.

Core Java Basics

1. What is Java and what are its key features?

Java is a programming language created by Sun Microsystems in 1995 (now owned by Oracle). It is used to build Android apps, websites, banking software and large company systems. Its main features are: it is simple to learn, object-oriented (everything is organised into classes and objects), secure, platform independent, and it manages memory automatically through garbage collection.

2. Why is the Java platform independent?

When you write Java code and compile it, Java does not create code for one specific computer. It creates something called bytecode. This bytecode is the same for every computer. Then each computer has its own JVM (Java Virtual Machine) that reads the bytecode and runs it. So you write the program once and run it on Windows, Linux or Mac without changing anything. This is called “Write Once, Run Anywhere”.

3. What is the difference between JDK, JRE and JVM?

JVM (Java Virtual Machine) is the engine that actually runs your bytecode. JRE (Java Runtime Environment) contains the JVM plus the libraries needed to run Java programs. JDK (Java Development Kit) contains the JRE plus the tools to write and compile programs, like the javac compiler. So a normal user who only wants to run a Java app needs the JRE, but a developer like you needs the JDK.

Example: Think of a car. JVM is the engine. JRE is the engine plus fuel and parts so the car can run. JDK is the whole factory that can also build the car. Remember it as JDK contains JRE, and JRE contains JVM.

4. What are the primitive data types in Java?

Primitive types are the basic built-in types that store simple values directly. Java has 8 of them: byte, short, int and long for whole numbers; float and double for decimal numbers; char for a single character; and boolean for true or false. Everything else, like String, arrays and your own classes, is a reference type that stores the address of an object.

Example: Use int for age, double for price, char for grade, boolean for pass or fail.

int age = 20;
double price = 99.5;
char grade = ‘A’;
boolean isPassed = true;

5. What are wrapper classes, autoboxing and unboxing?

A wrapper class turns a primitive value into an object. Each primitive has one: int has Integer, double has Double, boolean has Boolean. We need them because collections like ArrayList can store only objects, not primitives. Autoboxing means Java automatically converts a primitive into its wrapper object, and unboxing means Java automatically converts the wrapper back into a primitive.

Example: You cannot write ArrayList, but you can write ArrayList. It is like putting a small item in a box so it can be stored on a shelf that only holds boxes.

Integer a = 10; // autoboxing
int b = a; // unboxing

6. What is the difference between == and equals()?

The == operator compares references for objects, which means it checks whether both variables point to the very same object in memory. The equals() method compares the actual content. For primitives like int, == simply compares values. Classes like String override equals() so it checks the characters inside.

Example: Two students can have the same name (same content, so equals() is true), but they are still two different persons (so == is false).

String a = new String(“hello”);
String b = new String(“hello”);
System.out.println(a == b); // false
System.out.println(a.equals(b)); // true

7. What is the static keyword?

The static keyword means a variable or method belongs to the class itself, not to any single object. There is only one copy shared by all objects, and you can use it without creating an object. A static method can directly use only other static members. The main() method is static so the JVM can start the program without creating an object first.

Example: In a college, the college name is the same for every student, so it can be static. The roll number is different for each student, so it is not static.

class Student {
static String college = “ABC College”;
int rollNo;
}

8. What is the final keyword?

final is used to stop changes. A final variable becomes a constant and cannot be given a new value. A final method cannot be overridden by a child class. A final class cannot be extended. The String class is a famous final class. Do not confuse it with finally (a block in exception handling) and finalize() (a method called by the garbage collector).

Example: The value of PI never changes, so we declare it final.

final double PI = 3.14159;
PI = 3.14; // error

Object-Oriented Programming

9. What are the four pillars of OOP?

OOP stands for Object-Oriented Programming, and it has four pillars.

  • Encapsulation means wrapping data and the methods that use it inside one class and hiding the data using private.
  • Inheritance means one class can reuse the properties of another class.
  • Polymorphism means one thing can take many forms, like one method name behaving differently.
  • Abstraction means showing only the necessary details and hiding the complex inner workings.

Example: Encapsulation: a medicine capsule holds the medicine inside. Inheritance: a child gets features from parents. Polymorphism: a person is a student, a son and a friend at the same time. Abstraction: when you drive a car, you use the steering wheel and don’t need to know how the engine works.

10. What is the difference between a class and an object?

A class is a blueprint or template that describes what data (variables) and behaviour (methods) something will have. It does not take memory by itself. An object is a real thing created from that class using the new keyword, and it takes memory. From one class you can create many objects.

Example: Class is the architect’s plan of a house, and objects are the actual houses built from it.

class Car {
String color;
void drive() { System.out.println(“Driving”); }
}
Car myCar = new Car(); // object

11. What is a constructor? Can it be overloaded?

A constructor is a special method that runs automatically at the moment an object is created. It is used to set the starting values of the object. It must have the same name as the class and it has no return type. Yes, constructors can be overloaded, which means you can write several constructors with different parameters. If you do not write any constructor, Java provides a default one.

Example: When you buy a new phone, it comes with the initial setup done. The constructor does that initial setup for an object.class Student {
String name;
Student(String n) { name = n; }
}
Student s = new Student(“Ravi”);

12. What is the difference between method overloading and overriding?

Overloading means having several methods with the same name in the same class but with different parameters (different number or type). It is decided at compile time. Overriding means a child class writes its own version of a method that already exists in the parent class, with the same name and parameters. It is decided at runtime, and it needs inheritance.

Example: Overloading: a calculator’s add button can add two numbers or three numbers. Overriding: a child class Dog changes how the sound() method of the parent class Animal works.

class Animal { void sound(){ System.out.println(“Some sound”); } }
class Dog extends Animal {
@Override
void sound(){ System.out.println(“Bark”); }
}

13. What is the difference between an abstract class and an interface?

An abstract class is a class that cannot be created as an object. It can have both abstract methods (without body) and normal methods, variables and constructors. A class can extend only one abstract class. An interface is a full contract that lists methods a class must implement, and a class can implement many interfaces. Since Java 8, interfaces can also have default and static methods.

Example: Use an abstract class when classes are closely related, like Animal for Dog and Cat. Use an interface for an ability, like Flyable which can be given to Bird, Plane or Drone.

interface Flyable { void fly(); }
class Bird implements Flyable {
public void fly(){ System.out.println(“Bird flies”); }
}

14. Does Java support multiple inheritance?

Java does not support multiple inheritance with classes, which means a class cannot extend two classes. The reason is the diamond problem: if two parent classes have the same method, the child will be confused about which one to use. But Java gives a safe solution, because a class can implement multiple interfaces and so get features from many sources.

Example: A child cannot have two different rulebooks that say opposite things, but it can follow many lists of skills.

class A implements Flyable, Swimmable { … }

15. What are these and super keywords?

The this keyword refers to the current object. It is mostly used when a parameter name is the same as a variable name, and to call another constructor of the same class. The super keyword refers to the parent class. It is used to call the parent’s constructor or the parent’s overridden method.

Example: When you say “my name”, you mean this person. When you say “my father’s name”, it is like super.

class Student {
String name;
Student(String name){ this.name = name; }
}

Practicing OOP concepts on your own?

Build real Java projects with mentor guidance through Skill Hives’ practical training programs.

Message Us on WhatsApp

Strings and Exception Handling

16. Why are Strings immutable in Java?

Immutable means that once a String object is created, it cannot be changed. If you try to change it, Java creates a new String instead. This is done for security (things like passwords and file names stay safe), for thread safety, for use as HashMap keys, and for saving memory because Java keeps a String pool and reuses the same text.

Example: It is like writing with a pen on paper. You cannot edit it, you can only write a new page.

String s = “Java”;
s.concat(” World”);
System.out.println(s); // still Java

17. What is the difference between String, StringBuilder and StringBuffer?

String is immutable, so every change creates a new object, which is slow if you do it many times. StringBuilder is mutable, which means it changes the same object, and it is fast but not thread-safe. StringBuffer is also mutable and it is thread-safe because its methods are synchronized, but it is slower. In most programs, use StringBuilder when joining text in loops.

Example: String is a printed page, and StringBuilder is a whiteboard where you can erase and write again.

StringBuilder sb = new StringBuilder(“Hello”);
sb.append(” World”);
System.out.println(sb); // Hello World

18. How do you reverse a String in Java?

The easiest way is to put the String into a StringBuilder and call its reverse() method. Interviewers also like the manual way, where you loop from the last character to the first and build a new String. Be ready to show both.

Example: Reversing “Java” gives “avaJ”. The same logic is used to check a palindrome.

String s = “Java”;
String rev = new StringBuilder(s).reverse().toString();

// manual way
String r = “”;
for (int i = s.length() – 1; i >= 0; i–) {
r += s.charAt(i);
}

19. What is the difference between checked and unchecked exceptions?

An exception is an error that happens while the program is running. Checked exceptions are checked by the compiler at compile time, so you must handle them with try-catch or declare them with throws. Examples are IOException and SQLException. Unchecked exceptions are subclasses of RuntimeException, they happen at runtime and the compiler does not force you to handle them. Examples are NullPointerException and ArithmeticException.

Example: Checked: a weather warning you must prepare for in advance, like taking an umbrella. Unchecked: a surprise mistake like slipping on the road.

try {
int x = 10 / 0;
} catch (ArithmeticException e) {
System.out.println(“Cannot divide by zero”);
}

20. What is the difference between throw and throws?

throw is a keyword used inside a method body to actually create and throw one exception object. throws is used in the method signature to declare that the method might pass an exception to the code that calls it. So throw is the action, and throws is the warning.

Example: throw: you raise your hand and say there is a problem. throws: you put a sign on the door saying danger may occur here.

void check(int age) throws Exception {
if (age < 18) throw new Exception(“Not eligible”);
}

21. What is the finally block?

The finally block is written after try and catch, and it always runs, whether an exception happened or not. It is used for cleanup work, like closing a file, database connection or scanner. It will not run only in rare cases, like when System.exit() is called. A modern alternative is try-with-resources, which closes resources automatically.

Example: After eating at a restaurant you always pay the bill, whether the food was good or bad.

try {
// risky code
} catch (Exception e) {
// handle error
} finally {
System.out.println(“Always runs”);
}

Collections Framework

22. What is the Java Collections Framework?

The Collections Framework is a set of ready-made interfaces and classes in Java for storing and handling groups of objects. The main interfaces are List (ordered, allows duplicates), Set (no duplicates) and Map (key-value pairs), and Queue. It saves your time because you do not have to write data structures like lists and maps from scratch.

Example: A List is like a class attendance register, a Set is like a list of unique roll numbers, and a Map is like a phone book with name and number.

List names = new ArrayList<>();
names.add(“Ravi”);
names.add(“Sita”);

23. What is the difference between ArrayList and LinkedList?

ArrayList uses a dynamic array inside. It is very fast to get an element by its index, but slow to add or remove in the middle because other elements must shift. LinkedList uses nodes connected to each other. It is fast to add or remove elements when you are at that position, but slow to find an element by index because it must walk through the list.

Example: ArrayList is like seats in a cinema with numbers, so you can find seat 25 quickly. LinkedList is like a train where each coach is connected to the next.

24. What is the difference between HashMap and HashSet?

HashMap stores data as key-value pairs, and keys must be unique. HashSet stores only single values and does not allow duplicates. Internally, a HashSet actually uses a HashMap to store its elements. Neither of them guarantees the order of elements.

Example: HashMap: student roll number to student name. HashSet: only a list of unique roll numbers.

Map<Integer,String> map = new HashMap<>();
map.put(1, “Ravi”);
Set set = new HashSet<>();
set.add(1); set.add(1); // stored once

25. How does HashMap work internally?

When you put a key and a value, HashMap calls the hashCode() of the key to calculate a number, and uses it to decide which bucket (position) to store the entry in. If two keys go to the same bucket, it is called a collision, and they are kept together in a linked list (changed to a tree in Java 8 when it becomes big). When you get a value, it finds the bucket by hashCode() and then uses equals() to find the exact key.

Example: Think of a library with shelves. The hash code tells which shelf to go to, and equals() helps you find the exact book on that shelf.

26. What is the difference between HashMap and Hashtable?

HashMap is not synchronized, so it is faster, and it allows one null key and many null values. Hashtable is synchronized (thread-safe), but slower, and it does not allow any null key or value. Hashtable is an old class, so in modern programs we use HashMap, or ConcurrentHashMap when many threads are involved.

Example: HashMap is a free-entry hall, and Hashtable is a hall where only one person enters at a time through a gate.

Want to practice Collections and build real logic, not just memorize them?

Skill Hives’ programming training includes hands-on exercises with Java fundamentals, OOP, and collections.

Message Us on WhatsApp

Multithreading and Memory

27. What is a thread and how do you create one?

A thread is a small unit of work that runs independently inside a program, so multithreading lets a program do many tasks at the same time. There are two ways to create a thread: extend the Thread class, or implement the Runnable interface. Implementing Runnable is better because Java allows you to extend only one class. Always call start() and not run(), because start() creates a new thread.

Example: On your phone, you listen to music while chatting. Each is like a separate thread.

class MyTask implements Runnable {
public void run(){ System.out.println(“Running”); }
}
new Thread(new MyTask()).start();

28. What does the synchronized keyword do?

When many threads use the same data, they can disturb each other and give wrong results. This problem is called a race condition. The synchronized keyword allows only one thread at a time to run a method or block of code. Other threads must wait until the first thread finishes. This keeps shared data safe.

Example: A single ATM cabin: only one person enters at a time, and others wait outside in the queue.

synchronized void withdraw(int amt) {
// only one thread at a time
}

29. What is garbage collection in Java?

Garbage collection is the process where the JVM automatically finds objects that are no longer used by the program and removes them to free memory. The programmer does not need to delete objects manually, unlike in C or C++. You can request it using System.gc(), but there is no guarantee it will run immediately.

Example: Like a cleaner in a hostel who removes the empty bottles and trash that nobody needs anymore.

Student s = new Student();
s = null; // object now eligible for GC

30. What is the difference between stack and heap memory?

Stack memory stores method calls, local variables and references. It is small and very fast, and each thread has its own stack. Heap memory stores all the objects created with new. It is larger, shared by all threads, and cleaned by the garbage collector. If the stack gets full you get StackOverflowError, and if the heap gets full you get OutOfMemoryError.

Example: Stack is like your desk with the book you are using right now. Heap is the big storeroom where all the items are kept.

int x = 10; // x in stack
Student s = new Student(); // s in stack, object in heap

Last-Minute Interview Tips

  • Start your answer with a simple definition, then give a small example.
  • Practise coding string reverse, palindrome, Fibonacci and sorting by hand.
  • Know your own projects well.
  • If you do not know an answer, say so honestly and tell how you would find it.
  • Stay calm and speak slowly.

Keep Learning with Skill Hives

Revise this list before every interview, practise daily, and share it with a friend who is preparing too. If you want structured, hands-on guidance to build real Java projects alongside this kind of interview preparation, Skill Hives offers practical programming training designed for freshers getting ready for their first developer role.

Ready to turn this prep into a job-ready Java skill set?

Join Skill Hives for structured Java training, real projects, and interview preparation support.

Message Us on WhatsApp

avatar
Written By
Devraj
calendar 8th October 2026
Spread the love

Your Vision, Our Expertise -
Let's Create Smart, Scalable Solutions Together