Java: 51 Interview Questions with Brief Answers
Q: What is JVM, JRE, and JDK?
A: JVM: Runtime environment interpreting bytecode.
JRE: JVM + libraries needed at runtime.
JDK: JRE + development tools like compiler (javac).
Q: What is Java bytecode?
A: Platform-independent code generated by the compiler, executed by the JVM.
Q: What are access modifiers?
A: public, protected, package-private (default), and private: control visibility.
Q: Class vs Object?
A: Class: blueprint. Object: an instance in memory.
Q: Types of constructors?
A: Default, parameterized, and copy (via others or patterns).
Q: Method overloading?
A: Same method name, different parameter lists.
Q: Method overriding?
A: Subclass provides specific implementation of inherited method.
Q: Polymorphism?
A: Ability to treat objects of different classes through the same interface.
Q: Inheritance?
A: Mechanism where a class derives from another, inheriting attributes and methods.
Q: Abstract class vs Interface?
A: Abstract class: can have implemented methods and fields; supports single inheritance.
Interface: Java 8+ can have default/static methods; supports multiple implementation.
Q: What is encapsulation?
A: Wrapping data (fields) and code (methods) together, restricting direct access.
Q: final, finally, finalize?
A: final: prevents redefinition.
finally: runs after try/catch.
finalize(): deprecated cleanup method in Object.
Q: == vs .equals()?
A: ==: compares references.
.equals(): compares object content (for many classes).
Q: Static method?
A: Belongs to class, not instance.
Q: Static vs Non-static?
A: Static members are shared; non-static are per-object.
Q: Java package?
A: Namespace to organize classes and interfaces.
Q: Exception types?
A: Checked: must be handled/declared.
Unchecked: runtime errors, e.g., NullPointerException.
Q: try-catch-finally?
A: try block runs code; catch handles exceptions; finally executes regardless of exception.
Q: throw vs throws?
A: throw: manually throw an exception.
throws: declare possible exceptions in method signature.
Q: Garbage collection?
A: Automatic memory cleanup of unreachable objects.
Q: Multithreading?
A: Concurrent threads of execution; managed by JVM and OS.
Q: Thread lifecycle?
A: New - Runnable - Running - Blocked/Waiting - Terminated.
Q: wait() vs sleep()?
A: wait(): releases lock, used in synchronization.
sleep(): does not release lock, just pauses thread.
Q: Synchronization?
A: Keyword/tool to ensure atomic access to shared resources.
Q: Deadlock?
A: Two or more threads waiting indefinitely for each other-s locks.
Q: List vs Set vs Map?
A: List: ordered collection with duplicates.
Set: unordered, no duplicates.
Map: key-value associates, keys unique.
Q: ArrayList vs LinkedList?
A: ArrayList: fast random-access, slower insert/delete.
LinkedList: slower indexing, fast insertion/deletion.
Q: HashMap vs Hashtable?
A: HashMap: non-synchronized, allows null keys.
Hashtable: synchronized, no nulls allowed.
Q: ConcurrentHashMap?
A: Thread-safe, partitioned locks allow concurrent access.
Q: super keyword?
A: Refers to parent class instance/methods/constructors.
Q: this keyword?
A: Refers to current instance.
Q: String vs StringBuilder vs StringBuffer?
A: String: immutable.
StringBuilder: mutable, not synchronized.
StringBuffer: synchronized, slower.
Q: Transient keyword?
A: Excludes field from serialization.
Q: Constructor chaining?
A: Using this() or super() to call another constructor.
Q: Lambda expression?
A: Concise way to represent single-method interface: (args) -> expression.
Q: Functional interface?
A: Interface with single abstract method, e.g., Comparator, Runnable.
Q: Default method in interface?
A: Java-8+: interfaces can have methods with body marked default.
Q: Stream API?
A: Functional-style operations on collections: map, filter, reduce.
Q: Optional class?
A: Java-8+: container to avoid null checks, know presence/absence of value.
Q: Memory management?
A: Heap: objects live here; stack: method calls/local variables. GC cleans heap.
Q: Classloader?
A: Breaks tasks: Boot, Extension, and Application classloaders load classes into JVM.
Q: Heap vs Stack memory?
A: Heap: dynamic allocation, shared.
Stack: method-local, faster, per-thread.
Q: Wrapper classes?
A: Object versions of primitives: Integer, Double.
Q: Autoboxing/unboxing?
A: Automatic conversion between primitives and wrapper types.
Q: Annotation?
A: Metadata, e.g., @Override, @Deprecated, @SuppressWarnings.
Q: Singleton class?
A: Only one instance exists, often implemented via private constructor and getInstance().
Q: Platform independence?
A: Write Once, Run Anywhere: compile into bytecode run on any JVM.
Q: Java 8 features?
A: Lambdas, Stream API, Optional, default/static interface methods.
Q: Record (Java 14+)?
A: Compact syntax for immutable data carriers: record Person(String name, int age);
Q: Reflection?
A: Inspect and manipulate loaded classes, methods, fields dynamically.
Q: Difference between String pooling and new String()?
A: Literals go to String pool for reuse; new String() always creates a new object.