Java OCA OCP Practice Question 989

Question

Which one statement is true about the following code?

1. String s1 = "abc" + "def"; 
2. String s2 = new String(s1); 
3. if (s1 == s2) 
4.     System.out.println("== succeeded"); 
5. if (s1.equals(s2)) 
6.     System.out.println(".equals() succeeded"); 
  • A. Lines 4 and 6 both execute.
  • B. Line 4 executes and line 6 does not.
  • C. Line 6 executes and line 4 does not.
  • D. Neither line 4 nor line 6 executes.


C.

Note

Because s1 and s2 are references to two different objects, the == test fails.

However, the strings contained within the two String objects are identical, so the equals() test passes.




PreviousNext

Related