What method is used to find if a key exists in a Map?
map.containsKey(key)
What method is used to get a section of a String by index?
str.substring(start, end)
In the method substring(start, end), is the end index included or excluded?
excluded
How do I get the number of keys in a Map?
map.size()
What does map.getKeys() do in Java?
This method doesn’t exist. The actual method is map.keySet() which returns Set<K> containing all keys in the map.</K>
How to get all the values from a Map?
map.values()
What is the difference between JDK, JRE, and JVM?
JDK (Java Development Kit): - Contains tools for developing Java applications (compiler, debugger, etc.).
JRE (Java Runtime Environment):
- Provides libraries and JVM to run Java programs.
JVM (Java Virtual Machine): - Executes compiled Java bytecode. Loads .class files.
What is the difference between == and .equals() in Java?
== Compares references (memory addresses) when used with objects.
For primitives, it compares values directly.
.equals() compares values (object content), unless overridden.
What are primitive data types in Java?
8 primitives: byte, short, int, long, float, double, char, boolean.
What is autoboxing and unboxing?
Autoboxing: automatic conversion of primitive → wrapper (e.g., int → Integer).
Unboxing (automatic): wrapper → primitive (e.g., Integer → int).
What is the difference between String, StringBuilder, and StringBuffer?
String → immutable. Any modification (e.g., concatenation) creates a new String object in memory. Safe to use in multithreaded environments. Inefficient for frequent modifications (like loops or concatenations).
StringBuilder → mutable, not thread-safe (faster). Not synchronized, so faster than StringBuffer. Best for Single-threaded operations.
StringBuffer → mutable, thread-safe (slower). Synchronized, meaning thread-safe, but slower due to locking overhead. Best for Multi-threaded environments where multiple threads modify the same string.
What are the four pillars of OOP?
Encapsulation
Inheritance
Polymorphism
Abstraction
What is method overloading vs method overriding?
Method overloading happens when multiple methods in the same class have the same name but different parameters (different number, type, or order of parameters). It is resolved at compile time — hence called compile-time polymorphism.
Method overriding happens when a subclass provides its own implementation of a method that already exists in its parent class with the same name, parameters, and return type. It is resolved at runtime — hence called runtime polymorphism.
Can you override a static method in Java?
No — static methods are class-level and not polymorphic. You can hide them, not override them. Static methods belong to the class, not to instances (objects).
Overriding is a runtime concept — it depends on the actual object type.
Static methods are resolved at compile time, based on the reference type, not the object type.
What is the difference between an abstract class and an interface?
Abstract Class:
- Can contain both abstract and concrete methods.
Interface:
- Methods are implicitly abstract and public by default.
What is the difference between ArrayList and LinkedList?
ArrayList:
- Backed by a dynamic array.
LinkedList:
- Implemented as a doubly linked list.
What is the difference between HashMap, LinkedHashMap, and TreeMap?
HashMap → Unordered; entries are stored based on hash codes of keys.
LinkedHashMap → Maintains insertion order (or access order if configured).
TreeMap → Maintains sorted order of keys according to their natural ordering or a custom Comparator.
What happens if you put a duplicate key in a HashMap?
The new value replaces the old value for that key.
What is the difference between HashSet and TreeSet?
HashSet → Unordered; based on a HashMap; offers constant-time operations (O(1) average).
TreeSet → Elements are sorted by their natural order or a custom Comparator; based on a TreeMap; operations are O(log n).
What is the difference between checked and unchecked exceptions?
Checked exceptions → Represent recoverable conditions; must be declared in the method signature or handled with try-catch.
Examples: IOException, SQLException, FileNotFoundException.
Unchecked exceptions → Represent programming errors; subclasses of RuntimeException; not required to be declared or handled.
Examples: NullPointerException, IndexOutOfBoundsException, ArithmeticException.
What does finally do in a try-catch-finally block?
The finally block executes always, even if an exception occurs — typically used to close resources.
What is garbage collection in Java?
Automatic memory management — the JVM removes objects that are no longer reachable from any references.
How is ArrayList implemented internally in Java?
ArrayList uses a dynamic array internally (Object[] elementData).
It automatically resizes when capacity is exceeded.
Random access (get, set) is O(1), but insertion/removal (except at the end) is O(n) due to shifting.
How is LinkedList implemented internally in Java?
LinkedList is a doubly-linked list of nodes.
Each node stores: data, next, and prev references.
No random access — traversal is O(n).
Insertions/removals at ends are O(1).