Bird
Raised Fist0
JUnittesting~20 mins

assertSame and assertNotSame in JUnit - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
JUnit assertSame Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of assertSame with identical objects
What will be the result of this JUnit test method execution?
JUnit
String a = "test";
String b = a;
assertSame(a, b);
ATest throws NullPointerException
BTest fails with AssertionError
CTest passes without error
DCompilation error due to assertSame usage
Attempts:
2 left
💡 Hint
assertSame checks if both references point to the exact same object.
❓ Predict Output
intermediate
2:00remaining
Result of assertNotSame with different objects
What happens when this JUnit assertion runs?
JUnit
String a = new String("hello");
String b = new String("hello");
assertNotSame(a, b);
ATest passes without error
BTest fails with AssertionError
CCompilation error due to assertNotSame usage
DTest throws IllegalArgumentException
Attempts:
2 left
💡 Hint
assertNotSame checks that two references do not point to the same object.
❓ assertion
advanced
2:00remaining
Which assertion fails here?
Given these variables, which assertion will fail when executed?
JUnit
Integer x = 128;
Integer y = 128;
assertSame(x, y);
assertNotSame(x, y);
ABoth assertions pass
BassertNotSame(x, y) fails
CBoth assertions fail
DassertSame(x, y) fails
Attempts:
2 left
💡 Hint
Integer caching affects small values but not 128.
🧠 Conceptual
advanced
2:00remaining
Purpose of assertSame vs assertEquals
Which statement correctly explains the difference between assertSame and assertEquals in JUnit?
AassertSame checks if two references point to the same object; assertEquals checks if two objects are logically equal.
BassertSame checks object content equality; assertEquals checks reference equality.
CassertSame throws exception on nulls; assertEquals ignores nulls.
DassertSame compares primitive values; assertEquals compares object references.
Attempts:
2 left
💡 Hint
Think about identity vs equality.
🔧 Debug
expert
3:00remaining
Why does this assertSame fail unexpectedly?
Consider this test code snippet: String a = "abc"; String s1 = a + "def"; String s2 = "abcdef"; assertSame(s1, s2); Why does assertSame fail here?
ABecause assertSame cannot be used with String objects
BBecause string concatenation at runtime creates new objects, so references differ
CBecause s1 and s2 are different String objects despite having same content
DBecause s2 is null causing NullPointerException
Attempts:
2 left
💡 Hint
Consider when Java creates String objects and how concatenation works.

Practice

(1/5)
1. What does assertSame check in a JUnit test?
easy
A. If two references point to the exact same object
B. If two objects have the same content
C. If two objects are of the same class
D. If two references are both null

Solution

  1. Step 1: Understand the purpose of assertSame

    assertSame checks if both references point to the exact same object in memory, not just equal content.
  2. Step 2: Differentiate from equality checks

    Other assertions like assertEquals check if objects are equal in content, but assertSame checks identity.
  3. Final Answer:

    If two references point to the exact same object -> Option A
  4. Quick Check:

    assertSame = same object reference [OK]
Hint: assertSame checks object identity, not content equality [OK]
Common Mistakes:
  • Confusing assertSame with assertEquals
  • Thinking assertSame checks object content
  • Assuming assertSame works for null references only
2. Which of the following is the correct syntax to assert that two objects are NOT the same in JUnit?
easy
A. assertNotSame(object1, object2);
B. assertNotSame(object1 == object2);
C. assertNotSame(object1.equals(object2));
D. assertNotSame(object1 != object2);

Solution

  1. Step 1: Recall the method signature of assertNotSame

    The correct syntax is assertNotSame(expected, actual), both are objects to compare references.
  2. Step 2: Identify incorrect usage

    Options A, C, and D misuse assertNotSame by passing boolean expressions instead of objects.
  3. Final Answer:

    assertNotSame(object1, object2); -> Option A
  4. Quick Check:

    assertNotSame takes two objects, not booleans [OK]
Hint: Use assertNotSame(obj1, obj2) with two objects, not booleans [OK]
Common Mistakes:
  • Passing boolean expressions instead of objects
  • Using assertNotSame with equals() method
  • Confusing assertNotSame syntax with assertEquals
3. Consider the following code snippet:
String a = "test";
String b = "test";
String c = new String("test");

assertSame(a, b);
assertNotSame(a, c);

What will be the result of running these assertions?
medium
A. First assertion passes, second fails
B. First assertion fails, second passes
C. Both assertions pass
D. Both assertions fail

Solution

  1. Step 1: Analyze references of a and b

    Both a and b point to the same string literal in the string pool, so assertSame(a, b) passes.
  2. Step 2: Analyze reference of c

    c is a new String object created on the heap, different from a, so assertNotSame(a, c) passes.
  3. Final Answer:

    Both assertions pass -> Option C
  4. Quick Check:

    String literals share references, new String creates new object [OK]
Hint: String literals share references; new String creates new object [OK]
Common Mistakes:
  • Assuming new String shares reference with literal
  • Confusing assertSame with assertEquals
  • Thinking assertNotSame fails if content equals
4. You wrote this test:
Object obj1 = new Object();
Object obj2 = obj1;
assertNotSame(obj1, obj2);

The test fails. What is the problem?
medium
A. assertNotSame cannot be used with Object class
B. assertNotSame syntax is incorrect
C. obj1 and obj2 are different objects, so test should pass
D. obj1 and obj2 refer to the same object, so assertNotSame fails

Solution

  1. Step 1: Check references of obj1 and obj2

    Both variables point to the same object because obj2 = obj1 assigns the reference.
  2. Step 2: Understand assertNotSame behavior

    assertNotSame expects different objects; since references are same, assertion fails.
  3. Final Answer:

    obj1 and obj2 refer to the same object, so assertNotSame fails -> Option D
  4. Quick Check:

    Same reference fails assertNotSame [OK]
Hint: assertNotSame fails if references point to same object [OK]
Common Mistakes:
  • Assuming assertNotSame checks content equality
  • Thinking assertNotSame syntax is wrong
  • Believing assertNotSame cannot test Object instances
5. Given the following code:
class Person {
  String name;
  Person(String name) { this.name = name; }
}

Person p1 = new Person("Alice");
Person p2 = new Person("Alice");
Person p3 = p1;

assertSame(p1, p3);
assertNotSame(p1, p2);

Which statement about these assertions is true?
hard
A. assertSame(p1, p3) fails because objects have same content but different references; assertNotSame(p1, p2) passes
B. assertSame(p1, p3) passes because p1 and p3 reference the same object; assertNotSame(p1, p2) passes because p1 and p2 are different objects even if names match
C. assertSame(p1, p3) passes; assertNotSame(p1, p2) fails because names are equal
D. Both assertions fail because Person objects are not immutable

Solution

  1. Step 1: Analyze references of p1 and p3

    p3 is assigned from p1, so both point to the same object; assertSame(p1, p3) passes.
  2. Step 2: Analyze references of p1 and p2

    p2 is a new Person object with same name but different reference; assertNotSame(p1, p2) passes.
  3. Final Answer:

    assertSame(p1, p3) passes; assertNotSame(p1, p2) passes because references differ -> Option B
  4. Quick Check:

    assertSame checks references, not content [OK]
Hint: assertSame checks references; assertNotSame passes if objects differ [OK]
Common Mistakes:
  • Confusing object content equality with reference equality
  • Assuming assertNotSame fails if object fields match
  • Thinking assertSame fails if objects are mutable
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard