Java Interview Guide: Key Concepts Explained
Java Interview Guide: Key Concepts Explained
TT EES TSE RT E R S
Interview Guide
Java
Cheatsheet
vol.4
Important Differences in Java
@fundootesters
@fundootesters
150K+ Community
150K+ Community
Fun Doo
TT EES TSE RT E R S
1. List VS Set
Feature List Set
An ordered collection that allows An unordered collection that doesn’t allow duplicate
Definition
duplicate elements. elements.
Key Interfaces [Link] (e.g., ArrayList, LinkedList) [Link] (e.g., HashSet, TreeSet, LinkedHashSet)
Access Elements By index using get(int index) By iterator or enhanced for loop
Allows multiple null values (in some Allows only one null value (in implementations like
Null Elements
implementations like ArrayList). HashSet).
Common Use Use when duplicates are allowed or Use when duplicates need to be avoided or for fast
Cases when ordering is important. membership checks.
[Link] VS ArrayList
Feature Array ArrayList
A fixed-size, contiguous memory data
Definition A resizable, dynamic array implementation in Java.
structure.
Size Fixed at the time of declaration. Dynamic and can grow or shrink as needed.
Can only store objects (autoboxing handles
Type Can store primitives and objects.
primitives).
Faster due to no overhead of resizing or Slightly slower due to resizing and boxing/unboxing
Performance
boxing/unboxing. of primitives.
Uses less memory since no extra features Consumes more memory due to dynamic resizing
Memory Usage
are provided. and internal operations.
Length/Size Use [Link] to get the size. Use [Link]() to get the size.
Allowed; no restrictions on the count of
Null Elements Allowed; can store multiple null values.
nulls.
Multi- Supports multi-dimensional arrays (e.g., Does not directly support multi-dimensional
Dimensional int[][]). structures.
Best for fixed-size collections or
Usage Ideal for dynamic collections where size can change.
performance-critical operations.
[Link] package as part of the Java Collections
Belongs To
@fundootesters
@fundootesters
java package as a core feature.
150K+ Community
150K+ Community
Framework.
Fun Doo
TT EES TSE RT E R S
A contract specifying what a class must A class that provides partial implementation with
Definition
implement. abstract methods.
Keyword Declared using the interface keyword. Declared using the abstract keyword.
A class can implement multiple interfaces A class can extend only one abstract class (single
Inheritance
(multiple inheritance supported). inheritance).
Access Modifiers Methods are public by default. Methods can have any access modifier.
Used to define a contract or behavior that Used to define a base class with shared
Usage
multiple classes can implement. functionality for related classes.
Default Only default and static methods can have Can provide concrete methods with complete
Implementation bodies (Java 8+). implementation.
Slightly slower as methods are abstract by Faster as some methods may already be
Performance
nature. implemented.
4. Super() Vs this()
Feature super() this()
Constructor Call Calls the parent class constructor. Calls another constructor of the same class.
Accessing Parent Can be used to invoke parent class Cannot be used to call parent class methods
Methods methods or fields. directly; it’s used within the same class.
Constructor Can be used with the parent class Can be used with overloaded constructors in the
Overloading constructor when overloading is involved. same class to avoid code duplication.
First Statement in Must be the first statement in the Must be the first statement in the constructor, if
Constructor constructor. used.
super(); (calls the parent class default this(10); (calls another constructor in the same
Example
constructor). class with argument 10).
Used to create a chain between parent Used to create a constructor chain within the
Constructor Chain
and child class constructors. current class.
@fundootesters
@fundootesters
150K+ Community
150K+ Community
Fun Doo
TT EES TSE RT E R S
Immutable (cannot be Mutable (can be modified after Mutable (can be modified after
Immutability
modified after creation). creation). creation).
Thread-safe (synchronized
Thread Safety Not thread-safe. Not thread-safe.
methods).
Methods must have different parameter lists Method must have the same parameter list
Parameters
(type, number, or order). as in the parent class.
Can have different return types as long as Must have the same return type (or a
Return Type
method signatures differ. covariant return type).
Not related to inheritance; works within the Requires inheritance (between parent and
Inheritance
same class. child classes).
Overloaded methods can throw different Overriding methods cannot throw broader
Exceptions
exceptions. exceptions than the parent method.
@fundootesters
@fundootesters
150K+ Community
150K+ Community
Fun Doo
TT EES TSE RT E R S
Instance Association Does not belong to any particular instance. Belongs to a specific object instance.
Can Access Non-static Cannot directly access non-static members Can access both static and non-static
Members (fields or methods). members.
Inherited by all instances of the class, but Inherited and can be overridden in a
Inheritance
cannot be overridden. subclass.
Used for class-level methods, variables, and Used for instance-specific methods and
Usage
constants. variables.
8. Collection Vs Collections
Feature Collection Collections
Used to define the base interface for all Used to provide methods to manipulate or
Purpose
collection classes (List, Set, Queue, etc.). query collections (sorting, searching, etc.).
Defines basic collection operations like Provides utility methods like sort(),
Methods
add(), remove(), size(), etc. reverse(), shuffle(), max(), min(), etc.
Extends Iterable and implemented by Does not extend any class and is a final
Extends/Implements
collection classes like List, Set, etc. class.
Used when defining a collection type (e.g., Used when performing operations on a
Use Case
List, Set). collection (sorting, synchronizing, etc.).
@fundootesters
@fundootesters
150K+ Community
150K+ Community
Fun Doo
TT EES TSE RT E R S
Definition Belongs to the class, not instances of the class. Belongs to an instance of the class.
Keyword Declared with the static keyword. Declared without the static keyword.
Can be accessed without creating an instance of the Can only be accessed through an instance of the
Access
class. class.
Typically used for operations that are independent of Typically used for operations that depend on object
Usage
object state. state.
Allocated once when the class is loaded into Allocated each time an instance of the class is
Memory Allocation
memory. created.
Access to Instance Cannot directly access instance variables or Can access both instance and static variables/
Members methods. methods.
Inheritance Inherited by subclasses, but cannot be overridden. Inherited and can be overridden in subclasses.
Constructor Call Cannot be invoked from constructors. Can be invoked within constructors.
Not inherently thread-safe, but no synchronization Instance methods can be synchronized for thread
Thread Safety
needed for instance-specific data. safety.
Instance Dependency Does not depend on instance state. Depends on instance state (object variables).
A keyword used to define constants, prevent method A block of code used for exception handling, which
Definition
overriding, or prevent inheritance. always executes after a try-catch block.
Used in exception handling to ensure that the block
Usage Can be applied to variables, methods, and classes. of code is executed regardless of whether an
exception is thrown or not.
Scope Can be applied to: Can only be used within a try-catch block structure.
Can be applied to Variables, Methods, Classes. Only a block of code after a try or catch.
@fundootesters
@fundootesters
150K+ Community
150K+ Community
Fun Doo
TT EES TSE RT E R S
The container object owns the contained The container object does not own the contained
Ownership object. If the container is destroyed, the object. The contained object can exist outside the
contained object is also destroyed. container.
Contained objects are created and Contained objects can exist independently and may
Life Cycle
destroyed with the container object. outlive the container object.
class Library { private Book book; } (Book class Department { private Professor professor; }
Example Code
cannot exist without Library). (Professor can exist outside the Department).
Stronger relationship, as the contained Weaker relationship, as the contained object can exist
Strength of Relationship
object cannot exist independently. independently.
If the House is deleted, the Room is If a Team is deleted, the Player may still exist in
Example of Independence
deleted. another Team.
Slower for insertion/deletion (O(n)) due to Faster for insertion/deletion (O(1)) if at the beginning or
Insertion/Deletion Time
shifting elements (except at the end). end, but can be slower in the middle due to traversal.
Lower memory overhead as it uses a Higher memory overhead due to storing references/
Memory Overhead
contiguous block of memory. pointers in each node.
Resizes when the array is full, which can be No resizing required, as it dynamically allocates
Resizing
an expensive operation. memory as needed.
Best suited for scenarios with frequent Best suited for scenarios where frequent insertions or
Use Case
random access operations. deletions occur (especially at the beginning or middle).
Iteration Performance Faster for iteration (due to array structure). Slower iteration (due to traversal of nodes).
More memory efficient, as it stores only the Less memory efficient, as it stores pointers in addition
Memory Efficiency
actual data. to data.
Not thread-safe (can be made thread-safe Not thread-safe (can be made thread-safe using
Thread Safety
using [Link]()). [Link]()).
Example Code ArrayList<String> list = new ArrayList<>(); LinkedList<String> list = new LinkedList<>();
@fundootesters
@fundootesters
150K+ Community
150K+ Community
Fun Doo
TT EES TSE RT E R S
Provides basic methods: hasNext(), Extends Iterator and adds methods like
Method Availability
next(), and remove(). hasPrevious(), previous(), add(), and set().
Can only traverse in the forward Can traverse in both forward and backward
Traversal Direction
direction. directions.
Allows modification of the collection Allows modification of the collection through add(),
Modification
through remove(). set(), and remove().
Primarily used to iterate over any Primarily used for iterating over List
Used For
collection (List, Set). implementations (e.g., ArrayList, LinkedList).
Can only move forward and doesn't Can move both forward and backward, and can
Positioning
allow going backward. manipulate the cursor position.
Does not support adding elements Supports adding elements via add() method
Add Elements
during iteration. during iteration.
Does not support setting elements Supports setting elements via set() method during
Set Elements
during iteration. iteration.
Works with any collection, but typically More efficient for List traversal, as it has additional
Performance
slower than ListIterator for List. capabilities.
Part of the modern Java collection Part of the legacy collection framework
Legacy
framework (introduced in Java 1.2). (introduced in Java 1.0).
Preferred in most scenarios where Mostly used in older applications, or when thread-
Usage
thread-safety is not a concern. safety is a priority.
Can be resized as required using resize() Resizing is done using rehash() internally, which
Size/Capacity
method when threshold is met. may cause performance issues.
@fundootesters
@fundootesters
150K+ Community
150K+ Community
Fun Doo
TT EES TSE RT E R S
The class must implement Comparable, No need to modify the class. Can create multiple
Modification of Class
meaning its natural ordering is fixed. comparators.
Defines a single sorting logic (e.g., Can define multiple sorting logics, like sorting by
Sorting Logic
ascending or descending). name, age, etc.
The comparison logic must handle null Can also handle null values as per custom logic in the
Null Handling
values (if needed). compare() method.
Less flexible since the sorting criteria are More flexible as you can define different comparators
Flexibility
fixed in the class itself. for different sorting orders.
Used when you want a default or natural Used when you want different ways of sorting objects
Use Case
ordering for objects of a class. or need custom sorting logic.
Exceptions that are checked at compile- Exceptions that are not checked at compile-time
Definition
time. (runtime exceptions).
NullPointerException,
IOException, SQLException,
Example ArrayIndexOutOfBoundsException,
FileNotFoundException.
ArithmeticException.
Usually caused by external factors (e.g., file Typically caused by bugs or logical errors in the code
Cause
I/O, network). (e.g., accessing null, array bounds).
The program will not compile if a checked The program can run and throw these exceptions
Runtime Behavior
exception is not handled. during runtime.
@fundootesters
@fundootesters
150K+ Community
150K+ Community
Fun Doo
TT EES TSE RT E R S
@fundootesters
@fundootesters
150K+ Community
150K+ Community
Fun Doo
TT EES TSE RT E R S
18 PriorityQueue vs TreeSet
Feature PriorityQueue TreeSet
Package [Link] [Link]
Implements the Queue
Interface Implements the Set interface.
interface.
Elements are ordered based on Elements are ordered based on
their natural ordering or by a their natural ordering or by a
Ordering
custom comparator provided custom comparator provided at
at the time of creation. the time of creation.
Does not allow duplicate
Duplicates Allows duplicate elements.
elements (only unique elements).
Null Elements Does not allow null elements. Does not allow null elements.
Implements a priority queue,
Implements a navigable set,
which is an unbounded,
Type of Collection which is a sorted set without
thread-safe collection with
duplicates.
priority-based ordering.
Automatically sorts elements Elements are stored in a sorted
Sorting based on priority (using natural order (ascending or as defined by
ordering or comparator). a comparator).
Useful when elements need to
Useful when a set of unique
be processed in a specific order
Use Case elements needs to be stored and
based on priority (e.g., task
retrieved in a sorted order.
scheduling, event simulation).
Provides O(log(n)) time Provides O(log(n)) time
Performance complexity for insertions and complexity for insertions,
removals. removals, and lookups.
Not thread-safe (must be
Not thread-safe (must be
externally synchronized if used
Thread Safety externally synchronized if used in
in a multi-threaded
a multi-threaded environment).
environment).
Implementation
PriorityQueue TreeSet
Classes
Iterates in ascending order of the
Iterates in order of priority, not
Iterator elements (natural order or
necessarily sorted order.
comparator-defined order).
PriorityQueue<Integer> pq = TreeSet<Integer> ts = new
Example Code
new PriorityQueue<>(); TreeSet<>();
An empty queue has no
elements and throws An empty set simply returns null
Empty Queue/Set
NoSuchElementException on for methods like first() or last().
poll().
Throws NullPointerException if Throws NullPointerException if
Null Handling
null is added. null is added.
@fundootesters
@fundootesters
150K+ Community
150K+ Community
Fun Doo
TT EES TSE RT E R S
@fundootesters
@fundootesters
150K+ Community
150K+ Community
Fun Doo
TT EES TSE RT E R S
@fundootesters
@fundootesters
150K+ Community
150K+ Community
Fun Doo
TT EES TSE RT E R S
@fundootesters
@fundootesters
150K+ Community
150K+ Community
Fun Doo
TT EES TSE RT E R S
Latest Videos
Free Tutorials
In English In Hindi
@fundootesters
@fundootesters
150K+ Community
150K+ Community
Fun Doo
TT EES TSE RT E R S
@fundootesters
@fundootesters
150K+ Community
150K+ Community