Bird
0
0

What will be the output of the following Java code?

medium📝 Predict Output Q5 of 15
Java - Strings and String Handling
What will be the output of the following Java code?
String str1 = new String("code");
String str2 = new String("code");
System.out.println(str1.equals(str2));
Atrue
Bfalse
CCompilation error
DRuntime exception
Step-by-Step Solution
Solution:
  1. Step 1: Understand equals() method

    The equals() method compares the content of strings, not their references.
  2. Step 2: Analyze the code

    Both str1 and str2 contain the same sequence "code".
  3. Final Answer:

    true -> Option A
  4. Quick Check:

    equals() compares content, not references [OK]
Quick Trick: equals() checks content equality, not reference [OK]
Common Mistakes:
  • Using == to compare strings expecting content equality
  • Confusing equals() with == operator
  • Assuming new String() creates identical references

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes