20 Core Java Interview Questions for Freshers (with Answers)

admin6 min read
In this tutorial
  1. 1. What are JDK, JRE and JVM?
  2. 2. Why is Java platform-independent?
  3. 3. What is the difference between == and equals()?
  4. 4. Why are Strings immutable in Java?
  5. 5. What is the difference between String, StringBuilder and StringBuffer?
  6. 6. What are the four pillars of OOP?
  7. 7. What is the difference between method overloading and overriding?
  8. 8. Interface or abstract class?
  9. 9. Is Java pass-by-value or pass-by-reference?
  10. 10. What does the static keyword mean?
  11. 11. What does final mean on a variable, method and class?
  12. 12. What are checked and unchecked exceptions?
  13. 13. What is the difference between final, finally and finalize?
  14. 14. ArrayList or LinkedList?
  15. 15. How does HashMap work internally?
  16. 16. Why must you override hashCode when you override equals?
  17. 17. What is the difference between HashMap and Hashtable?
  18. 18. What is garbage collection?
  19. 19. What is a thread, and how do you create one?
  20. 20. What are some features added in Java 8 and later?
  21. Tips for the interview itself

If you are preparing for your first Java developer interview, the same core questions come up again and again. Interviewers are not looking for memorised definitions; they want to see that you understand why things work the way they do. Below are 20 of the most common questions, with answers short enough to say out loud and examples you can sketch on a whiteboard.

1. What are JDK, JRE and JVM?

  • The JVM (Java Virtual Machine) runs compiled Java bytecode. It is what makes Java “write once, run anywhere”.
  • The JRE (Java Runtime Environment) is the JVM plus the standard libraries needed to run programs.
  • The JDK (Java Development Kit) is the JRE plus development tools such as the compiler (javac) and debugger.

Since Java 11, Oracle no longer ships a separate JRE download. You install a JDK.

2. Why is Java platform-independent?

javac compiles source code into bytecode (.class files), not machine code. Any operating system with a JVM can run that bytecode. The JVM itself is platform-specific; your program is not.

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

== compares primitive values, or for objects, whether two references point to the same object. equals() compares objects’ contents, as defined by the class.

String a = new String("hi");
String b = new String("hi");
a == b;        // false
a.equals(b);   // true

4. Why are Strings immutable in Java?

Security (strings are used for file paths, URLs and class names), thread safety, safe use as HashMap keys (their hash code never changes) and the ability to share literals through the string pool. See our Strings guide.

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

String is immutable. StringBuilder is mutable and fast, but not thread-safe. StringBuffer is mutable and synchronised (thread-safe but slower). Use StringBuilder when building strings in a loop.

6. What are the four pillars of OOP?

  • Encapsulation: hiding internal state behind methods.
  • Inheritance: reusing and specialising a class with extends.
  • Polymorphism: one interface, many implementations; the method that runs depends on the actual object.
  • Abstraction: exposing what an object does while hiding how it does it, using interfaces and abstract classes.

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

  • Overloading: same method name, different parameter lists, in the same class. Resolved at compile time.
  • Overriding: a subclass provides its own version of a parent method with the same signature. Resolved at runtime based on the object’s actual type.
class Printer {
    void print(int x) { }       // overloading
    void print(String s) { }
}
class Animal { String sound() { return "..."; } }
class Dog extends Animal {
    @Override String sound() { return "Woof"; }   // overriding
}

8. Interface or abstract class?

An abstract class can hold state and constructors, and a class can extend only one. An interface defines a contract; a class can implement many, and interfaces can have default methods but no instance state. Full explanation: interface vs abstract class.

9. Is Java pass-by-value or pass-by-reference?

Always pass-by-value. For objects, the value that is copied is the reference, so a method can change the object’s contents but cannot make the caller’s variable point to a new object. See Java methods explained.

10. What does the static keyword mean?

A static member belongs to the class rather than to any object. All instances share static fields, and static methods can be called without creating an object (Math.max). Static methods cannot use this or access instance fields directly.

11. What does final mean on a variable, method and class?

  • final variable: can be assigned only once.
  • final method: cannot be overridden.
  • final class: cannot be extended (String is final).

12. What are checked and unchecked exceptions?

Checked exceptions (such as IOException) must be caught or declared with throws; the compiler enforces this. Unchecked exceptions extend RuntimeException (such as NullPointerException) and usually indicate programming errors. More in checked vs unchecked exceptions.

13. What is the difference between final, finally and finalize?

final is a keyword for constants and preventing overriding or extension. finally is a block that always runs after try/catch. finalize() was a method called by the garbage collector before reclaiming an object; it is deprecated and should not be used. Use try-with-resources for clean-up instead.

14. ArrayList or LinkedList?

ArrayList is backed by an array: fast random access (O(1)) and fast appends, slower inserts in the middle. LinkedList is a doubly linked list: fast inserts and removals at the ends, but O(n) access by index. In practice ArrayList is the right choice almost every time.

15. How does HashMap work internally?

It computes the key’s hashCode(), uses it to choose a bucket in an internal array, and stores the entry there. Collisions are resolved by comparing keys with equals() within the bucket; since Java 8, crowded buckets become balanced trees. The map resizes when it is 75% full. Details in our HashMap tutorial.

16. Why must you override hashCode when you override equals?

Hash-based collections find the bucket using hashCode() and then check equals(). If two equal objects have different hash codes, they land in different buckets and the collection cannot find them. The contract: equal objects must have equal hash codes.

17. What is the difference between HashMap and Hashtable?

Hashtable is a legacy class: every method is synchronised and it allows no null keys or values. HashMap is not synchronised and allows one null key. For thread-safe maps today, use ConcurrentHashMap, not Hashtable.

18. What is garbage collection?

The JVM automatically frees memory used by objects that are no longer reachable from any live reference. You do not call free or delete as in C or C++. You can still leak memory by keeping references you no longer need, for example in a static collection that only grows.

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

A thread is an independent path of execution within a program. You can extend Thread, or better, pass a Runnable to a thread or an ExecutorService:

ExecutorService pool = Executors.newFixedThreadPool(2);
pool.submit(() -> System.out.println("Running in " + Thread.currentThread().getName()));
pool.shutdown();

Mention that modern Java (21+) also has lightweight virtual threads, created with Thread.ofVirtual().start(...) or Executors.newVirtualThreadPerTaskExecutor().

20. What are some features added in Java 8 and later?

  • Java 8: lambdas, the Stream API, Optional, default methods in interfaces and the java.time date API.
  • Java 10: var for local variables.
  • Java 14-17: switch expressions, records, text blocks, pattern matching for instanceof and sealed classes.
  • Java 21: virtual threads, pattern matching in switch and record patterns.

Being able to say which version you use and one feature you like shows you keep up with the language.

Tips for the interview itself

  • Think out loud. Interviewers care about your reasoning as much as the answer.
  • Use small examples. A two-line code sample often explains better than a paragraph.
  • Admit what you don’t know, then say how you would find out. That is a skill employers value.
  • Prepare a project story. Be ready to explain something you built: the problem, your design and what you would do differently.
  • Practise coding questions such as reversing a string, checking a palindrome or binary search.

For broader software engineering questions (REST, SOLID, CI/CD), see our list of software engineering interview questions.

Good luck. You’ve got this!

Ask a question or leave a comment

Your email address will not be published. Required fields are marked *