Courseiva
Primitives, Strings and OperatorsmediumMultiple ChoiceObjective-mapped

1Z0-811 String comparison Practice Question

A developer is implementing a login verification method that compares a user-entered password against a stored hash. The passwords are stored as String objects. Which approach ensures correct comparison?

⚠ Common exam trap

The trap is that compareTo() can return 0 for equal strings, tempting candidates to select it. However, equals() is the expected method for simple equality checks.

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 (enteredPassword.equals(storedHash))

Option B uses equals(), the standard method for comparing string content, correctly checking character sequences. Option A uses compareTo(), which is intended for ordering; although compareTo() == 0 indicates equality, it is not the idiomatic approach and is considered a trap. Option C uses ==, comparing references, not content. Option D uses hashCode(), which may collide and does not guarantee equality.

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 (enteredPassword.compareTo(storedHash) == 0)

    Why it's wrong here

    compareTo() is used for ordering; equality check via compareTo() == 0 is unconventional and not recommended for simple equality.

  • if (enteredPassword.equals(storedHash))

    Why this is correct

    equals() is the standard method for comparing string content.

  • if (enteredPassword == storedHash)

    Why it's wrong here

    == compares object references, not the actual characters.

  • if (enteredPassword.hashCode() == storedHash.hashCode())

    Why it's wrong here

    hashCode() can produce collisions; equal hash codes do not guarantee equal 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.