Java OCA OCP Practice Question 3221

Question

Which expressions will evaluate to true if preceded by the following code?

String a = "hello";
String b = new String(a);
String c = a;
char[] d = { 'h', 'e', 'l', 'l', 'o' };

Select the two correct answers.

  • (a) (a == "Hello")
  • (b) (a == b)
  • (c) (a == c)
  • (d) a.equals(b)
  • (e) a.equals(d)


(c) and (d)

Note

String objects can have identical sequences of characters.

The == operator, when used on String object references, will just compare the references and will only return true when both references denote the same object (i.e., are aliases).

The equals() method will return true whenever the contents of the String objects are identical.

An array of char and a String are two totally different types and cannot be compared using the equals() method of the String class.




PreviousNext

Related