Java Interview Questions and Answers for Freshers (2026) with Examples

Java is still the most common language in fresher interviews at service companies like TCS, Infosys, Wipro, Accenture and Capgemini, and in many product company first rounds. The interviewer usually starts with the basics, moves to OOPs and String, and then checks collections and exceptions. The same 30 to 40 questions come again and again.
This page covers them in simple words, with small examples you can explain on paper. For OOPs theory in detail, also read our OOPs interview questions.
Quick summary
| Topic | What they usually ask | Must know |
|---|---|---|
| Basics | JDK vs JRE vs JVM, platform independence | Bytecode idea |
| OOPs | Overloading vs overriding, abstract class vs interface | One example each |
| String | String vs StringBuilder vs StringBuffer, string pool | Immutability |
| Keywords | static, final, this, super | Where each is used |
| Collections | ArrayList vs LinkedList, HashMap working | hashCode and equals |
| Exceptions | Checked vs unchecked, finally, throw vs throws | Try-catch flow |
| Threads | Thread vs Runnable, synchronized | Life cycle |
| Java 8 | Lambda, Stream, functional interface | One stream example |
Basic Java interview questions
1. What is Java?
Java is an object-oriented, class-based programming language. Code is compiled into bytecode, and the bytecode runs on any machine that has a JVM. That is why Java is called “write once, run anywhere”.
2. What is the difference between JDK, JRE and JVM?
| Part | What it is | Who needs it |
|---|---|---|
| JVM | Runs the bytecode | Every Java program |
| JRE | JVM + standard libraries | Users who only run Java apps |
| JDK | JRE + compiler (javac) + tools | Developers |
Easy line for the interview: JDK contains JRE, and JRE contains JVM.
3. Why is Java platform independent?
The compiler does not make machine code. It makes bytecode (.class file). Each operating system has its own JVM that converts the same bytecode into its own machine code. So the program is platform independent, but the JVM is platform dependent.
4. Why is Java not 100% object oriented?
Because it has primitive types like int, char, boolean and double that are not objects. Wrapper classes (Integer, Character…) exist to treat them as objects.
5. Explain public static void main(String[] args).
- public: JVM can call it from outside the class
- static: JVM calls it without creating an object
- void: it returns nothing
- main: the name the JVM looks for
- String[] args: command line arguments
6. What are the primitive data types in Java?
byte, short, int, long, float, double, char and boolean. Remember: char is 2 bytes in Java (Unicode), not 1 byte like in C.
7. What is the difference between == and equals()?
== compares references (are both variables pointing to the same object). equals() compares content, if the class overrides it, like String does.
String a = new String("hi"); String b = new String("hi");
Here a == b is false, but a.equals(b) is true.
OOPs questions in Java
8. What are the four pillars of OOPs?
- Encapsulation: data and methods kept together, data hidden with private fields and getters/setters
- Inheritance: one class gets properties of another (extends)
- Polymorphism: one name, many forms (overloading and overriding)
- Abstraction: show what an object does, hide how (abstract class, interface)
9. Overloading vs overriding
| Point | Overloading | Overriding |
|---|---|---|
| Where | Same class | Parent and child class |
| Method signature | Different parameters | Same parameters |
| Decided at | Compile time | Run time |
| Also called | Static polymorphism | Dynamic polymorphism |
10. Abstract class vs interface
| Point | Abstract class | Interface |
|---|---|---|
| Methods | Abstract and normal | Abstract, default and static (Java 8+) |
| Variables | Any type | Only public static final |
| Inheritance | extends one class | implements many interfaces |
| Constructor | Yes | No |
Use an interface when you want many unrelated classes to follow one contract.
11. Why does Java not support multiple inheritance with classes?
To avoid the diamond problem. If two parent classes have the same method, the child would not know which one to use. Java allows multiple inheritance through interfaces instead.
12. What is a constructor? Can it be private?
A constructor is a special method with the class name and no return type, called when an object is created. Yes, it can be private. That is used in the Singleton pattern so no outside class can create objects.
13. What is the use of this and super?
this refers to the current object (for example, this.name = name). super refers to the parent class, used to call the parent constructor or a parent method.
String questions
14. Why is String immutable in Java?
Once a String object is created, its value cannot change. Reasons: security (passwords, URLs, file paths), the string pool can safely share objects, and it is thread safe.
15. String vs StringBuilder vs StringBuffer
| Class | Mutable | Thread safe | Speed |
|---|---|---|---|
| String | No | Yes | Slow for many changes |
| StringBuilder | Yes | No | Fastest |
| StringBuffer | Yes | Yes (synchronized) | Slower than StringBuilder |
16. What is the String Constant Pool?
A special memory area in the heap. When you write String s = "java";, Java first checks the pool. If “java” already exists, it reuses it. new String("java") always creates a new object outside the pool.
17. Write a program to reverse a string.
String rev = new StringBuilder("hello").reverse().toString();
Interviewers may also ask you to do it with a loop from the last index to 0. Practice both.
Keywords: static and final
18. What does static mean?
A static variable or method belongs to the class, not to an object. All objects share one copy. A static method can be called with the class name and cannot use this.
19. final, finally and finalize
- final: variable cannot change, method cannot be overridden, class cannot be extended
- finally: block after try-catch that always runs (used to close files and connections)
- finalize(): old method called by the garbage collector before removing an object (deprecated now)
Collections questions
20. What is the Collection Framework?
A set of interfaces and classes to store and handle groups of objects. Main interfaces: List, Set, Queue and Map (Map is part of the framework but does not extend Collection).
21. ArrayList vs LinkedList
| Point | ArrayList | LinkedList |
|---|---|---|
| Inside | Dynamic array | Doubly linked list |
| Get by index | Fast O(1) | Slow O(n) |
| Insert/delete in middle | Slow | Fast |
| Memory | Less | More (stores links) |
22. List vs Set vs Map
List keeps order and allows duplicates. Set does not allow duplicates. Map stores key-value pairs with unique keys.
23. How does HashMap work internally?
HashMap uses an array of buckets. When you put a key, it calls hashCode() to find the bucket. If two keys land in the same bucket (collision), they are stored as a linked list, and from Java 8 the list turns into a balanced tree when it grows beyond 8 entries. equals() is used to find the exact key inside the bucket.
24. Why should you override hashCode() when you override equals()?
Because two equal objects must give the same hash code. If not, HashMap and HashSet will put them in different buckets and treat them as different keys.
25. HashMap vs Hashtable vs ConcurrentHashMap
HashMap is not synchronized and allows one null key. Hashtable is synchronized and old, no null keys. ConcurrentHashMap is the modern thread-safe choice and is faster than Hashtable.
26. Comparable vs Comparator
Comparable gives one natural order inside the class (compareTo method). Comparator is a separate object to sort in many different ways (compare method).
Exception handling questions
27. What is an exception?
An unwanted event during program run that breaks the normal flow, like dividing by zero or opening a missing file.
28. Checked vs unchecked exceptions
| Type | Checked at | Examples |
|---|---|---|
| Checked | Compile time | IOException, SQLException |
| Unchecked | Run time | NullPointerException, ArithmeticException, ArrayIndexOutOfBoundsException |
29. throw vs throws
throw is used inside a method to actually throw one exception. throws is written in the method signature to say which exceptions the method may pass to the caller.
30. Can we have try without catch?
Yes, try with finally, or try-with-resources. But a try block alone is not allowed.
31. Error vs Exception
Errors are serious problems the program should not try to handle, like OutOfMemoryError and StackOverflowError. Exceptions can be caught and handled.
Multithreading questions
32. What is a thread? How do you create one?
A thread is a small unit of a process that runs on its own. Two ways: extend the Thread class, or implement the Runnable interface and pass it to a Thread. Implementing Runnable is better because your class can still extend another class.
33. What is the life cycle of a thread?
New, Runnable, Running, Blocked/Waiting, Terminated.
34. What is synchronization?
It allows only one thread at a time to use a shared resource, using the synchronized keyword on a method or block. It prevents wrong results when many threads change the same data.
35. What is a deadlock?
Two threads each hold one lock and wait for the other’s lock forever. Neither can move. Avoid it by always taking locks in the same order.
Memory and Java 8 questions
36. Stack vs heap memory
Stack stores method calls and local variables, and is cleared when the method ends. Heap stores objects, and is cleaned by the garbage collector.
37. What is garbage collection?
The JVM automatically removes objects that no variable points to anymore, so you do not free memory by hand like in C.
38. What are the main features of Java 8?
Lambda expressions, functional interfaces, Stream API, default and static methods in interfaces, Optional class and the new Date/Time API.
39. What is a functional interface?
An interface with exactly one abstract method, like Runnable or Comparator. A lambda can be used in its place: Runnable r = () -> System.out.println("hi");
40. Give one Stream API example.
Print even numbers from a list:list.stream().filter(n -> n % 2 == 0).forEach(System.out::println);
How to prepare in 3 days
- Day 1: Basics, OOPs and String (questions 1 to 17). Write every answer once in your own words.
- Day 2: Collections and exceptions (18 to 31). Draw the HashMap bucket diagram.
- Day 3: Threads, memory and Java 8 (32 to 40), then write 5 programs on paper: reverse string, palindrome, factorial, Fibonacci, duplicate elements in an array.
Also revise DBMS questions, SQL queries and Operating System questions, because Java rounds often include a few DBMS and OS questions too. For the HR and puzzle round, see our placement puzzles with answers.
Free study material
Get free notes and Unit 1 of every eBook from our study material library, and watch one-shot videos on the Krazy Kaksha YouTube channel.




Doubts? Ask here