Courseiva
Primitives, Strings and OperatorseasyMultiple SelectObjective-mapped

1Z0-811 Primitives, Strings and Operators Practice Question

Which two of the following are valid ways to check if two String objects contain the same characters? (Assume s1 and s2 are non-null String references.)

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

if (s1.compareTo(s2) == 0) { ... }

The correct ways to compare two String objects for character equality are using the equals() method (option E) or compareTo() method returning 0 (option C). Option A (equalsIgnoreCase) ignores case, so it would treat strings with different cases as equal. Option B (==) compares object references, not content. Option D compares s1 with the interned version of s2; this only works if s1 is also interned, which is not guaranteed, making it unreliable.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • if (s1.equalsIgnoreCase(s2)) { ... }

    Why it's wrong here

    equalsIgnoreCase() compares strings ignoring case differences, so it does not verify exact character equality.

  • if (s1 == s2) { ... }

    Why it's wrong here

    The == operator compares memory references, not character content. It only returns true if both references point to the same object.

  • if (s1.compareTo(s2) == 0) { ... }

    Why this is correct

    compareTo() returns 0 when the strings are lexicographically equal, indicating identical character content.

  • if (s1 == s2.intern()) { ... }

    Why it's wrong here

    The intern() method returns a canonical representation, but using == is still reference comparison and not a reliable way to check content equality.

  • if (s1.equals(s2)) { ... }

    Why this is correct

    The equals() method is designed to compare the character content of two strings.

About these practice questions

This 1Z0-811 question is part of Courseiva's 481-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This 1Z0-811 practice question is part of Courseiva's free Oracle certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the 1Z0-811 exam.