Core Java – All 82 Interview Questions with Answers
Q: What is JIT in Java?
A: Just-In-Time compiler converts bytecode into native code at runtime for performance.
Q: Difference between abstract class and interface?
A: Abstract class can have state; interface supports multiple inheritance.
Q: Static vs Default methods?
A: Static belongs to interface; default can be overridden.
Q: Ways to create object in Java?
A: new, reflection, clone, deserialization.
Q: How to create immutable class?
A: Make class final, fields final, no setters.
Q: How to restrict object creation?
A: Private constructor (Singleton).
Q: Custom class loader?
A: Extend ClassLoader and override loadClass().
Q: Can main be without static?
A: No, JVM requires static main.
Q: Diamond problem?
A: Resolved using default methods or explicit override.
Q: Wrapper class?
A: Object representation of primitive types.
Q: How HashMap works internally?
A: Array + linked list/tree using hashCode & equals.
Q: Internal implementation of HashMap?
A: Buckets converted to tree when threshold exceeded.
Q: Eligible classes in resource block?
A: Classes implementing AutoCloseable.
Q: How HashSet works?
A: Backed by HashMap.
Q: HashMap vs Hashtable?
A: Non-synchronized vs synchronized.
Q: HashMap vs LinkedHashMap?
A: LinkedHashMap maintains order.
Q: HashMap vs ConcurrentHashMap?
A: Thread-safe using CAS.
Q: Null key in HashMap/Hashtable?
A: Allowed in HashMap, not in Hashtable.
Q: Immutable Map?
A: [Link]() or [Link]().
Q: Built-in immutable classes?
A: String, Integer, LocalDate, etc.
Q: Index in Java DB?
A: Improves search performance.
Q: CompletableFuture vs Callable?
A: CompletableFuture supports async chaining.
Q: Ways to perform async operations?
A: ExecutorService, CompletableFuture.
Q: Parse XML with JSON?
A: Jackson XML module.
Q: Parse JSON to HashMap?
A: Using Gson or Jackson.
Q: Fail-fast vs Fail-safe?
A: Exception vs no exception.
Q: Object class?
A: Root class of Java hierarchy.
Q: Why clone method?
A: To copy object state.
Q: Parsing libraries?
A: Jackson, Gson, JAXB.
Q: Comparable vs Comparator?
A: Natural vs custom ordering.
Q: Classpath exception?
A: ClassNotFoundException.
Q: Hierarchy of exceptions?
A: Throwable → Exception/Error.
Q: Throw vs Throws vs Throwable?
A: Throw raises, throws declares, Throwable is base.
Q: What is String?
A: Immutable sequence of characters.
Q: Why String immutable?
A: Security, caching, thread safety.
Q: Where new String stored?
A: Heap memory.
Q: Where strings stored?
A: Pool & Heap.
Q: Custom immutable String?
A: Final + defensive copying.
Q: String vs StringBuffer vs StringBuilder?
A: Immutable, synchronized, non-synchronized.
Q: Is StringBuffer synchronized?
A: Yes, methods are synchronized.
Q: Why substrings bad?
A: Memory leaks in older JVMs.
Q: Runtime exception?
A: Unchecked exception at runtime.
Q: Collection hierarchy?
A: Iterable → Collection → List/Set/Queue.
Q: Difference between syntaxes?
A: Depends on generics & declaration.
Q: ArrayList or LinkedList?
A: ArrayList for access, LinkedList for insertion.
Q: Iterator use?
A: Traverse collections.
Q: Default capacity of HashMap?
A: 16.
Q: HashMap max capacity behavior?
A: Rehashing occurs.
Q: Custom object as key?
A: Override equals & hashCode.
Q: Does HashMap maintain order?
A: No.
Q: HashSet vs TreeSet?
A: Unordered vs sorted.
Q: Get values from HashSet?
A: Iterate using iterator.
Q: Which declaration compiles?
A: Generic-safe declaration.
Q: ArrayList vs LinkedList?
A: Access vs insertion performance.
Q: Set vs List?
A: Unique vs ordered.
Q: HashSet vs HashMap?
A: Key-only vs key-value.
Q: Why HashMap unordered?
A: Hash-based indexing.
Q: How LinkedHashMap maintains order?
A: Doubly linked list.
Q: LinkedHashMap vs PriorityQueue?
A: Insertion vs priority order.
Q: hashCode return type?
A: int.
Q: What is ConcurrentHashMap?
A: Thread-safe Map.
Q: Internal implementation of ConcurrentHashMap?
A: CAS + bucket-level locking.
Q: Modify ConcurrentHashMap using iterator?
A: Yes, weakly consistent.
Q: Concurrent collection?
A: Thread-safe collections.
Q: Null in ConcurrentHashMap?
A: Not allowed.
Q: ConcurrentModificationException?
A: Structural modification during iteration.
Q: Serialization?
A: Convert object to byte stream.
Q: Uses of serialization?
A: Persistence, network transfer.
Q: When use ArrayList vs LinkedList?
A: Access vs insertion.
Q: Garbage Collection?
A: Automatic memory management.
Q: [Link]()?
A: Request GC.
Q: GC algorithms?
A: G1, ZGC, CMS.
Q: Fail-fast vs Fail-safe collections?
A: Exception vs no exception.
Q: Static variables stored where?
A: Method area / Metaspace.
Q: Custom exceptions?
A: Extend Exception/RuntimeException.
Q: Class vs Instance variables?
A: Shared vs per object.
Q: Throw vs Throws?
A: Raise vs declare.
Q: try-catch vs throws?
A: Handle vs propagate.
Q: HashMap vs LinkedHashMap?
A: Order vs no order.
Q: == vs equals?
A: Reference vs content.
Q: Exception declared in throws?
A: Propagated to caller.
Q: Inheritance without interface?
A: Using abstract class.