What are the core OOP principles in Java?
4 core principles
Encapsulation: Bundling data (fields) and methods in a class; restrict access using access modifiers.
Inheritance: A class can inherit fields and methods from another class (using extends).
Polymorphism: Objects can take many forms; methods can be overridden or overloaded.
Abstraction: Hiding complex implementation details; expose only necessary parts (using abstract classes/interfaces).
What is the difference between method overriding and overloading?
Overriding
* Subclass redefines a method from its superclass with the same signature.
* Used for runtime polymorphism.
Overloading
* Multiple methods in the same class with the same name but different parameters (type/number/order).
* Used for compile-time polymorphism.
What is the difference between List and Set in Java?
List
* Ordered collection (elements have a defined sequence).
* Allows duplicate elements.
* Examples: ArrayList, LinkedList.
Set
* Unordered collection (no guaranteed order).
* Does not allow duplicates.
* Examples: HashSet, TreeSet.
What is the difference between HashMap and TreeMap?
HashMap
* No guaranteed order of keys.
* Allows one null key and multiple null values.
* Faster (constant-time operations on average).
TreeMap
* Keys are sorted in natural order or by a custom comparator.
* Does not allow null keys (but allows null values).
* Slower (logarithmic-time operations).
What is the difference between an abstract class and an interface, and when would you use each?
Abstract Class
* Can have method implementations and state (fields).
* Supports constructors.
* Use when classes share common code/behavior.
Interface
* Only method signatures (Java 8+ allows default/static methods).
* No constructors or state (except static/final fields).
* Use for defining a contract or when multiple inheritance is needed.
How does the==operator differ from the.equals()method?
== operator
* Compares object references (memory addresses), not content.
* For primitives, compares actual values.
.equals() method
* Compares object contents (can be overridden for custom comparison).
* Used for logical equality.
What is the order of initialization in a Java class?
This order applies from superclass to subclass.
What are access modifiers in Java?
public: Accessible from anywhere.
protected: Accessible within the same package and subclasses.
default (no modifier): Accessible within the same package.
private: Accessible only within the same class.
What is the difference between checked and unchecked exceptions?
Checked exceptions:
* Must be declared or handled with try-catch.
* Subclasses of Exception (except RuntimeException).
* Example: IOException.
Unchecked exceptions:
* Do not need to be declared or handled.
* Subclasses of RuntimeException.
* Example: NullPointerException.
What is the difference between a stack and a queue?
Stack:
* Follows LIFO (Last-In, First-Out) order.
* Example operations: push, pop.
Queue:
* Follows FIFO (First-In, First-Out) order.
* Example operations: offer/add, poll/remove.
What is the difference between Optional.of and Optional.ofNullable?
Optional.of
* Throws NullPointerException if the value is null.
* Use when you are sure the value is not null.
Optional.ofNullable
* Returns an empty Optional if the value is null.
* Use when the value might be null.
What is the difference between shallow copy and deep copy?
Shallow copy
* Copies object references, not the actual nested objects.
* Changes to nested objects affect both copies.
Deep copy
* Copies the object and all nested objects (full duplication).
* Changes to nested objects do not affect the other copy.
What are marker interfaces?
What is the purpose of the @FunctionalInterface annotation?
What are the inheritance rules in Java?
What is the difference between HashMap and HashTable?
HashMap
* Not synchronized (not thread-safe).
* Allows one null key and multiple null values.
* Generally faster.
Hashtable
* Synchronized (thread-safe).
* Does not allow any null keys or values.
* Considered legacy; rarely used in new code.
What is the difference between object-level and class-level locks?
Object-level lock
* Acquired on an instance (using synchronized on instance methods or blocks).
* Only one thread can access synchronized code for that specific object at a time.
Class-level lock
* Acquired on the Class object (using static synchronized methods or synchronized(ClassName.class) blocks).
* Only one thread can access synchronized static code for the entire class at a time.
What is the difference between Runnable and Callable?
Runnable
* Has a run() method.
* Cannot return a result or throw checked exceptions.
Callable
* Has a call() method.
* Can return a result and throw checked exceptions.
What is the difference between Stream.of() and Arrays.stream()?
Stream.of()
* Creates a stream from a variable number of arguments or an array (as a single element if not varargs).
* Example: Stream.of(1, 2, 3) or Stream.of(array).
Arrays.stream()
* Creates a stream from the elements of an array (not as a single element).
* Example: Arrays.stream(array).
What is the difference between map and flatMap in streams?
map
* Transforms each element using a function; returns a stream of the same structure (e.g., Stream<T> to Stream<R>).</R></T>
flatMap
* Transforms each element into a stream and flattens the result into a single stream (e.g., Stream<Stream<R>> to Stream<R>).</R></R>
What is a method reference in Java?
What are streams in Java and what types of operators do they have?
Streams
* Abstractions for processing sequences of elements (from collections, arrays, etc.) in a functional style.
* Support operations like filtering, mapping, and reducing data.
Operators
* Intermediate operators: Return a new stream (e.g., filter, map, flatMap, sorted, distinct).
* Terminal operators: Produce a result or side effect (e.g., collect, forEach, reduce, count, anyMatch).
What enhancements were made to HashMap in Java 8?
What is the difference between fail-fast and fail-safe collections?
Fail-fast
* Throw ConcurrentModificationException if the collection is modified while iterating.
* Examples: ArrayList, HashMap.
Fail-safe
* Do not throw exceptions on modification during iteration; work on a copy of the collection.
* Examples: CopyOnWriteArrayList, ConcurrentHashMap.