Core Java & OOP
What are the four pillars of OOP?
Encapsulation (bundling data with the methods that use it and hiding internal state), inheritance (a class reusing a parent's behaviour), polymorphism (one interface, many implementations), and abstraction (exposing what an object does while hiding how). Give a one-line example of each.
Abstract class vs interface - when do you use each?
Use an abstract class for an 'is-a' relationship with shared state or partial implementation; use an interface for a 'can-do' capability that many unrelated classes implement. Since Java 8 interfaces can have default methods, but only an abstract class can hold instance state and constructors.
Method overloading vs overriding?
Overloading is the same method name with different parameters, resolved at compile time (static polymorphism). Overriding is a subclass redefining a superclass method with the same signature, resolved at runtime (dynamic polymorphism).
JVM vs JRE vs JDK?
The JVM executes bytecode. The JRE is the JVM plus the core libraries needed to run applications. The JDK is the JRE plus the compiler and developer tools needed to build them.
Why is String immutable, and what is the String pool?
Immutability makes strings safe to share and cache, safe as HashMap keys (their hash never changes), and more secure. The String pool reuses identical string literals to save memory.
Collections Framework
ArrayList vs LinkedList?
ArrayList is backed by a resizable array - O(1) random access but costly inserts/removals in the middle. LinkedList is a doubly linked list - O(1) insert/remove at a known node but O(n) access. Default to ArrayList unless you insert/delete heavily at the ends.
HashMap vs Hashtable vs ConcurrentHashMap?
HashMap is unsynchronised and allows one null key. Hashtable is a legacy, fully synchronised (and slow) class. ConcurrentHashMap gives thread safety with fine-grained bucket-level locking, so it scales far better under concurrency.
How does HashMap work internally?
A key's hashCode is mapped to a bucket. Entries in the same bucket chain in a linked list that converts to a balanced tree past a threshold (Java 8+). This gives O(1) average lookups and O(log n) worst case.
Why must you override equals() and hashCode() together?
The contract says equal objects must return equal hash codes. If you override only equals(), hash-based collections put 'equal' objects in different buckets and lookups fail. Always override both for objects used as keys.
Fail-fast vs fail-safe iterators?
Fail-fast iterators (ArrayList, HashMap) throw ConcurrentModificationException if the collection is structurally modified during iteration. Fail-safe iterators (CopyOnWriteArrayList, ConcurrentHashMap) iterate over a snapshot and don't throw.
Exceptions & error handling
Checked vs unchecked exceptions?
Checked exceptions (e.g. IOException) are checked at compile time and must be declared or handled - use them for recoverable conditions. Unchecked exceptions (RuntimeException subclasses like NullPointerException) signal programming bugs and aren't forced on callers.
Why prefer try-with-resources over finally?
try-with-resources auto-closes anything implementing AutoCloseable when the block exits, even on exception. It removes verbose, error-prone finally blocks and prevents resource leaks.
throw vs throws, and what if you return in finally?
throw actually raises an exception instance; throws declares that a method might throw one. A return or exception in a finally block overrides one from try - avoid it, because it silently swallows the original error.
Multithreading (the section that separates candidates)
Process vs thread, and how do you create a thread in Java?
A process has its own memory; threads within a process share memory but have their own stack, so they're cheaper to create and switch. Create a thread by extending Thread, implementing Runnable, or - preferred - submitting tasks to an ExecutorService.
What does synchronized do?
It enforces mutual exclusion: only one thread at a time holds a given monitor, and it also guarantees visibility of changes across threads. A synchronized method locks 'this' (or the Class for static methods); a synchronized block locks an object you choose.
What is a deadlock and how do you avoid it?
A deadlock is two or more threads each holding a lock the other needs, so none proceeds. Avoid it by acquiring locks in a consistent global order, using timeouts (tryLock), or minimising the scope of locking.
volatile vs synchronized?
volatile guarantees visibility of a single variable across threads but not atomicity of compound actions (like i++). synchronized guarantees both visibility and atomicity, at the cost of locking. Use volatile for simple flags, synchronized for compound updates.
How to actually get ready
Reading these answers builds recognition, but interviews test recall under pressure. Practice retrieving, not re-reading: take timed MCQ rounds on exactly these areas, read the explanation for anything you miss, and revisit weak spots the next day until the answers come out automatically.
A good weekly rhythm is one core-Java round, one collections-and-multithreading round, and one mixed round that also touches data structures and DBMS - because real interviews jump across topics without warning. If you're heading into campus placements, interleave aptitude and reasoning too, since almost every company gates the technical round behind an aptitude test.
Practice this now
