Bird
0
0

What will be the output of the following code?

medium📝 Predict Output Q4 of 15
Java - Strings and String Handling
What will be the output of the following code?
String s1 = "Hello";
String s2 = new String("Hello");
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
Afalse false
Btrue true
Cfalse true
Dtrue false
Step-by-Step Solution
Solution:
  1. Step 1: Understand == vs equals() for strings

    == compares references (memory addresses), equals() compares content.
  2. Step 2: Analyze given strings

    s1 is a string literal, s2 is a new String object with same content. So s1 == s2 is false, but s1.equals(s2) is true.
  3. Final Answer:

    false true -> Option C
  4. Quick Check:

    == compares references, equals() compares content = false true [OK]
Quick Trick: == checks memory, equals() checks string content [OK]
Common Mistakes:
  • Assuming == returns true for same content
  • Confusing equals() with == operator
  • Not understanding new String() creates new object

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes