This guide synthesizes the top 120+ most frequently asked Java interview and BCA viva questions into categorized, high-yield conceptual answers with technical depth expected by senior tech interviewers.
Focus on the "Why" and "Under the hood" mechanics rather than simple dictionary definitions. Interviewers want to see that you understand JVM memory, concurrency guarantees, and algorithmic complexity.
JVM (Java Virtual Machine) is the runtime engine that executes compiled bytecode. JRE (Java Runtime Environment) bundles the JVM with standard core runtime libraries (rt.jar / java.base). JDK (Java Development Kit) is the full superset containing the JRE plus development tools like the compiler (javac), debugger (jdb), and archiver (jar).
In Java, variables storing objects actually hold a copy of the reference address. When passed into a method, that 32-bit/64-bit address value is copied into the local stack frame. Changing the attributes of the referenced object modifies the shared heap instance, but reassigning the parameter reference variable (ref = new Object()) leaves the caller's original reference completely untouched.
public: Accessible globally by the JVM execution launcher outside the package.static: Can be invoked without requiring the JVM to instantiate an object of the class first.void: Does not return any exit status (exit codes are reported via System.exit(n)).String[] args: Accepts command-line arguments passed at program invocation.| Feature | Method Overloading (Compile-Time) | Method Overriding (Runtime) |
|---|---|---|
| Location | Occurs within the same class (or across hierarchy) | Occurs strictly between Superclass and Subclass |
| Signature | Same name, different parameter list | Exact same name and parameter list |
| Return Type | Can vary freely | Must be identical or covariant (subtype) |
| Resolution | Static Binding resolved at compile time | Dynamic Dispatch via Virtual Method Table (vtable) |
No. static methods belong to the class, not instances, and resolve at compile time via Method Hiding. private methods are not visible to subclasses, so defining a method with the same signature in a child class creates an entirely independent new method.
Multiple class inheritance causes ambiguity when two parent classes define the same method with different implementations. Java forbids multiple class inheritance. However, with Java 8 default methods in interfaces, if a class implements two interfaces sharing the identical default method signature, the compiler forces the implementing class to explicitly resolve the conflict via InterfaceName.super.methodName().
hash is calculated once lazily and cached, providing maximum performance in HashMaps.| Class | Mutability | Thread Safety | Performance |
|---|---|---|---|
String | Immutable | Thread-Safe (by immutability) | Slow for repeated concatenations |
StringBuilder | Mutable | Not Thread-Safe | Fastest (best for single-thread loops) |
StringBuffer | Mutable | Thread-Safe (synchronized methods) | Slower due to lock acquisition overhead |
HashMap stores entries in an array of Node<K,V> buckets. Key hashes are mapped to index via (n - 1) & hash. When collisions occur, entries are chained in a LinkedList. Java 8 Optimization: Once a bucket reaches 8 entries (TREEIFY_THRESHOLD) and total capacity >= 64, the linked list transforms into a balanced Red-Black Tree (TreeNode), reducing search complexity from $O(n)$ to $O(log n)$.
Comparable<T>: Defined in java.lang. Implemented by the class itself via compareTo(T o) to establish the Natural Ordering (e.g., sorting Numbers or Strings).Comparator<T>: Defined in java.util. External sorting strategy implemented via compare(T o1, T o2), permitting custom sorting on multiple criteria (e.g., Sort by Age, then by Name).| Feature | wait() | Thread.sleep() |
|---|---|---|
| Source Class | java.lang.Object | java.lang.Thread |
| Lock Holding | Releases the monitor lock during wait | Retains all acquired monitor locks |
| Invocation Context | Must be inside a synchronized block | Can be invoked from anywhere |
| Wakeup Condition | Via notify(), notifyAll(), or timeout | Strictly when timer expires or interrupt |
volatile guarantees Visibility and establishes a Happens-Before relationship. Writes to a volatile variable are immediately flushed to main RAM, and subsequent reads by other threads bypass CPU L1/L2 caches to fetch directly from main RAM. It prevents CPU instruction reordering, but does not guarantee atomicity for compound operations like count++.
When asked: "Can an abstract class have a constructor if it cannot be instantiated with new?"
Answer: YES! An abstract class has constructors to initialize its own member fields. They are invoked by subclass constructors via super() during subclass instantiation.
Accelerate your Java mastery by studying these tightly connected core tutorials:
Code tracing and tricky output drills.
Syntax and keywords reference guide.
Core collection hierarchies and complexity.
Execution engine and classloader deep dive.