Java Interview Questions for Freshers (with Answers)
Java remains the most common language in Indian IT services and banking projects, so core Java rounds are thorough. Expect questions on strings, the collections framework and exceptions in almost every interview, followed by a small coding exercise.
Language basics
What is the difference between JDK, JRE and JVM?
The JVM runs bytecode and makes Java platform-independent. The JRE is the JVM plus the standard libraries needed to run programs. The JDK is the JRE plus development tools such as the compiler (javac) and debugger.
Why is String immutable in Java?
Once created, a String’s value cannot change. That makes strings safe to share between threads, safe as HashMap keys (the hash code never changes), and allows the string pool to reuse one object for equal literals.
What is the difference between == and equals()?
== compares references for objects (whether both variables point to the same object) and values for primitives. equals() compares content when the class overrides it, as String does.
String, StringBuilder and StringBuffer: what is the difference?
String is immutable, so every change creates a new object. StringBuilder is mutable and fast but not thread-safe. StringBuffer is mutable and synchronised, so it is thread-safe but slower. Use StringBuilder for building strings in a loop.
What do final, finally and finalize mean?
final is a keyword: a final variable cannot be reassigned, a final method cannot be overridden, a final class cannot be extended. finally is a block that runs after try/catch whether or not an exception occurred. finalize() was a method the garbage collector called before reclaiming an object; it is deprecated and should not be used.
What is the difference between an abstract class and an interface in Java?
An abstract class can have fields, constructors and both abstract and concrete methods; a class extends only one. An interface declares a contract; since Java 8 it can have default and static methods, but no instance state, and a class can implement many.
Collections
ArrayList or LinkedList?
ArrayList is backed by a resizable array: O(1) access by index, slower inserts in the middle. LinkedList is a doubly linked list: O(1) insert or remove at the ends, O(n) access by index. ArrayList is the right default in almost every case.
How does HashMap work internally?
It keeps an array of buckets. put() computes the key’s hashCode, maps it to a bucket, and stores the entry there; keys in the same bucket are chained in a linked list that becomes a balanced tree when it grows long. get() finds the bucket by hash and then uses equals() to pick the entry. When the map passes its load factor (0.75 by default) it doubles and rehashes.
Why must you override hashCode() when you override equals()?
Hash-based collections find the bucket with hashCode() and then confirm with equals(). If two equal objects return different hash codes they land in different buckets, so a HashSet could hold duplicates and a HashMap lookup would miss.
HashMap, LinkedHashMap, TreeMap: when would you use each?
HashMap has no ordering and the fastest operations. LinkedHashMap keeps insertion order. TreeMap keeps keys sorted and gives O(log n) operations. Pick by whether you need order.
What is the difference between HashMap and ConcurrentHashMap?
HashMap is not thread-safe. ConcurrentHashMap allows safe concurrent reads and updates by locking only a small part of the map per write, and does not permit null keys or values.
Exceptions, threads and Java 8
Checked versus unchecked exceptions?
Checked exceptions (such as IOException) must be caught or declared with throws; the compiler enforces it. Unchecked exceptions extend RuntimeException (NullPointerException, ArrayIndexOutOfBoundsException) and usually signal a programming bug.
How do you create a thread, and which way is preferred?
Extend Thread, or implement Runnable (or Callable) and pass it to a Thread or an ExecutorService. Implementing Runnable is preferred because it leaves the class free to extend something else, and in real code you submit tasks to an executor rather than create threads by hand.
What does synchronized do?
It lets only one thread at a time run a block or method guarded by a given lock, which prevents two threads from corrupting shared data. The cost is contention, so you keep synchronised sections short.
What is a lambda expression and a functional interface?
A functional interface has exactly one abstract method (Runnable, Comparator, Function). A lambda is a short way to provide that method’s implementation inline, for example (a, b) -> a - b for a Comparator.
What is the Stream API?
A way to process collections as a pipeline of operations such as filter, map and collect, without writing the loop yourself. Streams are lazy: nothing runs until a terminal operation like collect() or forEach() is called.
How does garbage collection work?
The JVM automatically frees objects that are no longer reachable from any live reference. You cannot force it; System.gc() is only a request. Memory leaks in Java come from references you forgot to release, such as objects left in a static collection.
Programs you should be able to write
- Reverse a string and check for a palindrome without library reverse methods.
- Count the frequency of each character or word using a HashMap.
- Find the second largest number in an array in one pass.
- Remove duplicates from a list while keeping order.
- Print the Fibonacci series and check for a prime number.
- Sort a list of objects by a field using Comparator.