Java debugging exercises
Java powers enterprise backends, but classic pitfalls cause production outages: comparing strings with == checks object references instead of content, removing elements inside an enhanced for-loop throws ConcurrentModificationException, and unboxing a null Integer triggers an unexpected NullPointerException.
These exercises give you deliberate practice fixing real Java defects across core collections, generics, streams, and concurrency.
Practise by topic
How to debug a Java function
- Use .equals() for object comparison. In Java, == compares memory addresses; always use .equals() for String, Integer (outside -128..127), and custom objects.
- Never mutate a collection in an enhanced for-loop. Use Iterator.remove(), removeIf(), or stream filters to avoid ConcurrentModificationException.
- Beware automatic unboxing. Passing a null Integer to an int parameter causes an immediate NullPointerException.
- Implement hashCode() when overriding equals(). Storing objects in a HashMap or HashSet fails silently if both methods are not synchronized.
- Check integer division truncation. Dividing two ints truncates decimals to zero before assignment to a float or double; cast operands first.
The exercises
Easy
- String equality comparison with == operatorLogic
- NullPointerException on unboxing null IntegerNull and undefined
- Integer division truncates percentage calculationType
- String substring endIndex out of bounds exceptionLogic
- Regex split on literal dot splits every characterLogic
- Optional get() called without isPresent checkNull and undefined
- Missing break statement in switch causes fallthroughLogic
- Double precision floating point currency roundingLogic
- StringBuffer synchronization overhead in loopLogic
Medium
- String comparison fails in user authentication systemLogic
- Unexpected runtime error during list removal in iterationAsync and race conditions
- Unexpected NullPointerException during sum calculationNull and undefined
- HashMap Key Lookup Fails Due to Incorrect hashCode/equalsLogic
- Unexpected Mutation of Nested ArrayListsLogic
- Unexpected Behavior in Traffic Light State TransitionsLogic
- Inconsistent date formatting results in concurrent requestsAsync and race conditions
- List size issues when adding elements in JavaType
- Equality Check Fails for Certain Integer ValuesLogic
- Unexpected rounding error in financial calculationLogic
- Thread hangs unexpectedly after external service callAsync and race conditions
- Unexpected null return instead of expected exceptionLogic
- Array Conversion Error in Generic Data ProcessingType
- Task results lost when ExecutorService tasks completeAsync and race conditions
- Cloned list elements exhibit unexpected shared stateLogic
- Unexpected NoSuchElementException during data processingNull and undefined
- Unexpected string split behavior with regex charactersLogic
- Unexpected null values during base class initializationNull and undefined
- Unintended Object Retention Causing Memory LeakScope and closure
- Unexpected Boolean Comparison Behavior in JavaType
- Unexpected Resource Behavior in Nested Try-With-ResourcesScope and closure
- ConcurrentModificationException in ArrayList loopLogic
- HashMap lookup fails due to missing hashCodeLogic
- Stream toList produces unmodifiable list on mutationLogic
- Boxed Long equality comparison outside cache rangeLogic
- Midpoint integer overflow in binary searchLogic
- ArrayStoreException with covariant array assignmentType
- BigDecimal divide without rounding mode throws exceptionLogic
- Resource leak due to missing try-with-resourcesScope and closure
- PriorityQueue comparator violates transitivityLogic
- Array clone produces shallow copy of 2D matrixLogic
- ExecutorService thread pool prevents JVM shutdownAsync and race conditions
- Raw type wildcard warning in generic list castingType
Hard
- Singleton Instance Inconsistency in Multi-threaded EnvironmentAsync and race conditions
- Gradual memory increase in multi-threaded web serverScope and closure
- Shallow clone of object with mutable collectionLogic
- Static field retention causes application memory leakScope and closure
- Infinite recursion in toString cyclic referencesInfinite loops
- ThreadLocal memory leak in thread pool environmentScope and closure
What you will practise
Questions
Which Java version is targeted?
Modern Java (Java 17 & Java 21 LTS) using modern collection APIs, streams, records, and pattern matching.
Do I need a JDK installed?
No. All exercises execute and verify tests directly through BugHunt.