Exam 1z0-808 All QuestionsBrowse all questions from this exam
Question 205

Given:

and the code fragment:

What is the result?

    Correct Answer: C

    C

Discussion
7df49fbOption: A

There is a problem with the question, i think the answer is AC. because (s1 == "Moon") && (s2.equals("Moon")) evaluate to true.

cocobotOption: C

Answer is C, as although it looks like the OraString is overriding the equals method. But its actually just a method as the Objects equals method required Object as the parameter. Hence the second statement of if((s1 == "Moon") && (s2.equals("Moon"))) evaluates to false.

carlosworkOption: C

Answer is C. To test: class OraString { String s; public boolean equals (OraString str) { return this.s.equalsIgnoreCase(str.toString()); } OraString(String s){ this.s = s; } } public class Test { public static void main(String[] args) { String s1 = "Moon"; OraString s2 = new OraString("Moon"); if((s1 == "Moon") && (s2.equals("Moon"))) { System.out.println("A"); } else { System.out.println("B"); } if (s1.equalsIgnoreCase(s2.s)) { System.out.println("C"); } else { System.out.println("D"); } } }

iSnoverOption: C

The answer is the letter C, even though I try the same content, strings are different if they are parsed as "==" or something like that because they are not primitive types. The correct way to compare the contents of String and different objects is equals and its variegated. Right in the first if of false (B) and in the second if of true (C), printing BC.