Java Interview Preparation
# CHAPTER 28
Java Interview Preparation
1. Introduction
This chapter is your Java interview survival kit. We've compiled the 50 most frequently asked Java interview questions across Core Java, OOP, Collections, and Exception Handling — plus 20 coding challenges that interviewers love to ask.2. Core Java Interview Questions
Q1: What is the difference between JDK, JRE, and JVM? JDK = Development Kit (compiler + tools + JRE). JRE = Runtime Environment (JVM + libraries). JVM = Virtual Machine that executes bytecode.
Q2: Why is Java platform-independent? Java compiles to bytecode (.class files) which runs on any JVM, regardless of the underlying OS.
Q3: What is the difference between == and .equals()?
== compares memory addresses (reference equality). .equals() compares content (value equality). For Strings, always use .equals().
Q4: What is autoboxing and unboxing?
Autoboxing: primitive → wrapper (int → Integer). Unboxing: wrapper → primitive (Integer → int).
Q5: What is the String Pool? A special memory area in the Heap where String literals are stored and reused to save memory.
Q6: Why are Strings immutable in Java? Security (passwords), thread safety, and performance (String Pool caching).
Q7: What is the difference between final, finally, and finalize()?
final = constant/prevent override. finally = always-execute block. finalize() = garbage collection hook (deprecated).
Q8: Can we override a static method? No. Static methods are resolved at compile time (method hiding, not overriding).
Q9: What is the transient keyword?
Marks a field to be excluded from serialization.
Q10: What is the difference between throw and throws?
throw creates/throws an exception. throws declares that a method might throw an exception.
3. OOP Interview Questions
Q11: What are the four pillars of OOP? Encapsulation, Inheritance, Polymorphism, Abstraction.
Q12: What is the difference between an abstract class and an interface? Abstract class can have constructors, instance variables, and concrete methods. Interface defines a contract with abstract methods (Java 8+ allows default/static methods). A class can implement multiple interfaces but extend only one class.
Q13: What is the diamond problem? When a class inherits from two classes that have the same method, creating ambiguity. Java avoids this by not allowing multiple class inheritance.
Q14: What is method overloading vs overriding? Overloading: same name, different parameters (compile-time). Overriding: child redefines parent method (runtime).
Q15: What is the super keyword used for?
Access parent class members (methods, fields, constructors) from a child class.
Q16: Can a constructor be inherited?
No. Constructors are not inherited, but a child can call super() to invoke the parent constructor.
Q17: What is a Singleton pattern? A design pattern ensuring a class has only one instance. Achieved with a private constructor and a static getInstance() method.
Q18: What is the difference between composition and inheritance? Inheritance = IS-A (Dog IS-A Animal). Composition = HAS-A (Car HAS-A Engine). Prefer composition over inheritance.
4. Collections Interview Questions
Q19: ArrayList vs LinkedList? ArrayList: fast random access O(1), slow insert/delete O(n). LinkedList: slow access O(n), fast insert/delete O(1).
Q20: HashMap vs TreeMap vs LinkedHashMap? HashMap: O(1) lookup, no order. TreeMap: O(log n), sorted by key. LinkedHashMap: O(1), maintains insertion order.
Q21: How does HashMap work internally? Uses hashing. Key's hashCode() determines the bucket index. Collisions are resolved using linked lists (or trees in Java 8+).
Q22: What is the difference between HashSet and TreeSet? HashSet: O(1) operations, no order. TreeSet: O(log n), elements are sorted.
Q23: What is the fail-fast iterator? An iterator that throws ConcurrentModificationException if the collection is modified during iteration.
Q24: How to make a collection thread-safe?
Use Collections.synchronizedList(), ConcurrentHashMap, or CopyOnWriteArrayList.
5. Exception Handling Questions
Q25: Checked vs Unchecked exceptions? Checked: must handle at compile time (IOException). Unchecked: runtime errors (NullPointerException).
Q26: Can we have try without catch?
Yes, with finally or try-with-resources.
Q27: What is the order of catch blocks? Most specific exception first, most general last.
6. Top 20 Coding Challenges
C1: Reverse a String
C2: Check Palindrome
C3: Fibonacci Series
C4: Check Prime Number
C5: Find Largest in Array
C6: Factorial
C7: Count Vowels
C8: Remove Duplicates from Array
C9: Check Anagram
C10: FizzBuzz
C11: Swap Two Numbers Without Temp Variable
C12: Check Armstrong Number
C13: Binary Search
C14: Bubble Sort
C15: Count Words in a String
C16: Find Second Largest
C17: String to Integer (Without parseInt)
C18: Matrix Transpose
C19: Power Without Math.pow
C20: Find Missing Number in Array (1 to n)