Challenge - 5 Problems
AssertEquals Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ assertion
intermediate2:00remaining
Understanding assertEquals with primitive types
What will be the result of this JUnit assertion?
assertEquals(5, 2 + 3);
JUnit
import static org.junit.jupiter.api.Assertions.assertEquals; public class TestClass { @org.junit.jupiter.api.Test void testSum() { assertEquals(5, 2 + 3); } }
Attempts:
2 left
💡 Hint
assertEquals compares expected and actual values for equality.
✗ Incorrect
The assertion compares the expected value 5 with the actual result of 2 + 3, which is also 5, so the test passes.
❓ assertion
intermediate2:00remaining
assertEquals with String objects
What will happen when this assertion runs?
assertEquals("hello", "he" + "llo");JUnit
import static org.junit.jupiter.api.Assertions.assertEquals; public class TestClass { @org.junit.jupiter.api.Test void testStringConcat() { assertEquals("hello", "he" + "llo"); } }
Attempts:
2 left
💡 Hint
assertEquals compares string content, not object references.
✗ Incorrect
assertEquals checks if the string contents are equal, which they are, so the test passes.
❓ assertion
advanced2:00remaining
assertEquals with floating point numbers and delta
What is the result of this assertion?
assertEquals(0.3, 0.1 + 0.2, 0.0001);
JUnit
import static org.junit.jupiter.api.Assertions.assertEquals; public class TestClass { @org.junit.jupiter.api.Test void testFloatingPoint() { assertEquals(0.3, 0.1 + 0.2, 0.0001); } }
Attempts:
2 left
💡 Hint
Floating point arithmetic can have small precision errors; delta allows tolerance.
✗ Incorrect
The sum 0.1 + 0.2 is approximately 0.30000000000000004, which is within the delta 0.0001, so the test passes.
❓ assertion
advanced2:00remaining
assertEquals with arrays
What will happen when this assertion runs?
assertEquals(new int[]{1, 2}, new int[]{1, 2});JUnit
import static org.junit.jupiter.api.Assertions.assertEquals; public class TestClass { @org.junit.jupiter.api.Test void testArrayEquals() { assertEquals(new int[]{1, 2}, new int[]{1, 2}); } }
Attempts:
2 left
💡 Hint
assertEquals on arrays compares references, not contents.
✗ Incorrect
assertEquals compares object references for arrays, so two different arrays with same contents fail the test.
❓ assertion
expert3:00remaining
assertEquals with custom objects and equals method
Given this class:
What will happen when this assertion runs?
public class Person {
String name;
public Person(String name) { this.name = name; }
}What will happen when this assertion runs?
assertEquals(new Person("Alice"), new Person("Alice"));JUnit
import static org.junit.jupiter.api.Assertions.assertEquals; public class TestClass { public static class Person { String name; public Person(String name) { this.name = name; } } @org.junit.jupiter.api.Test void testPersonEquals() { assertEquals(new Person("Alice"), new Person("Alice")); } }
Attempts:
2 left
💡 Hint
Default equals compares object references unless overridden.
✗ Incorrect
Without overriding equals, two different Person objects are not equal even if their fields match, so the test fails.