Java Interview Questions and Answers
In this lesson: Most-asked Core Java interview questions for freshers — OOP, JVM, strings, collections, exceptions — short answers. Prepare fast.
How to use this list
These are the questions that come up most often in Java interviews for freshers. Understand the idea behind each, and be ready to give a short, clear answer with a one-line example. Knowing why matters far more to interviewers than memorising.
Basics and OOP
Q1. What is Java? A platform-independent, object-oriented language; code compiles to bytecode that runs on the JVM.
Q2. JDK vs JRE vs JVM? JVM runs bytecode; JRE = JVM + libraries; JDK = JRE + compiler tools.
Q3. What are the four OOP pillars? Encapsulation, inheritance, polymorphism, abstraction.
Q4. Overloading vs overriding? Overloading = same name, different parameters (compile time); overriding = child redefines a parent method (runtime).
Q5. Abstract class vs interface? Abstract has code and is extended (one); interface is a contract and can be implemented many.
Strings and types
Q6. == vs equals()? == compares references; equals() compares content.
Q7. Why are strings immutable? For safety, caching in the string pool, and predictable behaviour.
Q8. String vs StringBuilder? String is immutable; StringBuilder is mutable and fast for building text.
Q9. What is autoboxing? Java automatically converting a primitive (int) to its wrapper (Integer) and back.
Q10. final keyword? Makes a variable constant, a method non-overridable, or a class non-extendable.
Collections
Q11. List vs Set vs Map? List keeps order and duplicates; Set stores unique items; Map stores key-value pairs.
Q12. ArrayList vs LinkedList? ArrayList = fast index access; LinkedList = fast middle insert/remove.
Q13. HashMap vs Hashtable? HashMap is fast and not thread-safe; Hashtable is thread-safe but slower and legacy.
Q14. HashSet vs TreeSet? HashSet has no order and is fast; TreeSet keeps items sorted.
Q15. Comparable vs Comparator? Comparable is one natural order (compareTo); Comparator gives custom orders (compare).
Exceptions and more
Q16. Checked vs unchecked exceptions? Checked must be caught or declared; unchecked (RuntimeException) need not be.
Q17. final vs finally vs finalize? final = keyword; finally = block that always runs; finalize = deprecated method.
Q18. What is a thread? A separate path of execution; multithreading runs several at once.
Q19. What is the Singleton pattern? A class with only one object, via a private constructor and getInstance().
Q20. What is garbage collection? Java automatically freeing memory of objects no longer referenced.
Interview tips
- Explain the why. Say not just what a feature does, but when and why to use it.
- Give a one-line example. A tiny snippet shows real understanding.
- Mention OOP and collections. They are the core of most Java interviews.
- Be honest. If unsure, say how you would find out — that is a good sign too.
Summary
- Know the core comparisons:
==vsequals(), overloading vs overriding. - Explain the four OOP pillars clearly with examples.
- Be comfortable with collections: List, Set, Map and their classes.
- Understand exceptions, threads, Singleton and garbage collection.
- Answer with the "why" plus a short example, not just definitions.
Revise the topics in inheritance and collections before your interview.
Frequently Asked Questions
What is the difference between == and equals() in Java?
== compares references (whether two variables point to the same object), while equals() compares the actual content. For strings and objects, use equals() to compare values.