Bird
0
0

Find the problem in this code:

medium📝 Debug Q7 of 15
Java - Strings and String Handling
Find the problem in this code:
String s1 = null;
String s2 = "hello";
if (s1.equals(s2)) {
System.out.println("Match");
} else {
System.out.println("No Match");
}
ANo problem, prints No Match
BCompilation error due to null string
CThrows NullPointerException
DAlways prints Match
Step-by-Step Solution
Solution:
  1. Step 1: Check method call on null reference

    Calling equals() on s1 which is null causes NullPointerException at runtime.
  2. Step 2: Understand program behavior

    Program crashes before printing anything due to exception.
  3. Final Answer:

    Throws NullPointerException -> Option C
  4. Quick Check:

    Calling method on null reference causes NullPointerException [OK]
Quick Trick: Never call methods on null strings to avoid exceptions [OK]
Common Mistakes:
  • Assuming safe equals() call on null
  • Expecting No Match output
  • Confusing compile and runtime errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes