Question 509 of 509
Primitives, Strings and OperatorsmediumMultiple ChoiceObjective-mapped

String Value Equality with equals() and ==

This 1Z0-811 practice question tests your understanding of primitives, strings and operators. Read the scenario carefully and evaluate each option against the stated constraints before committing to an answer. A key principle to apply: string comparison. Once you have made your selection, read the full explanation to reinforce the concept and understand why each distractor is designed to mislead on exam day.

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?

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

Both options A and B correctly compare the content of String objects. Option A uses compareTo(), which returns 0 when the strings are equal, and is a valid equality check. Option B uses equals(), which is the standard and recommended approach for comparing string content. Option C uses ==, which compares object references, not content, and is incorrect. Option D uses hashCode(), which may produce collisions and does not guarantee equality; it should not be used for equality checks.

Key principle: String comparison

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 this is correct

    Correct. The compareTo() method returns 0 when the strings are equal, so this approach works. While equals() is more common, compareTo() is also valid for equality comparison.

    Related concept

    String comparison

  • if (enteredPassword.equals(storedHash))

    Why this is correct

    Correct. The equals() method compares the actual character content of the strings and is the standard way to check string equality in Java.

    Related concept

    String comparison

  • if (enteredPassword == storedHash)

    Why it's wrong here

    Incorrect. The == operator compares object references, not the string content. Two different String objects with the same characters would be considered not equal with ==.

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

    Why it's wrong here

    Incorrect. The hashCode() method returns an integer hash code, but different strings can have the same hash code (collisions). It does not guarantee equality and should not be used for comparison.

Common exam traps

Common exam trap: answer the scenario, not the keyword

The trap is that compareTo() can also be used for equality checking, but many candidates dismiss it because equals() is the standard. The question may expect only equals(), but technically both are correct.

Detailed technical explanation

How to think about this question

Treat this as a scenario question. Identify the problem, the constraint, and the best action. Then compare each option against those facts.

KKey Concepts to Remember

  • String comparison
  • compareTo() method
  • hashCode()

TExam Day Tips

  • Watch for words such as best, first, most likely and least administrative effort.
  • Review why wrong options are wrong, not only why the correct option is correct.

Key takeaway

String comparison

Real-world example

How this comes up in practice

A practitioner preparing for the 1Z0-811 exam encounters this exact type of scenario on the job. The correct answer here is not the most general option — it is the best answer for the specific constraint described. String comparison Real exam questions reward reading the full scenario before eliminating options, because the constraint defines which answer fits.

What to study next

Got this wrong? Here's your next step.

Review string comparison, then practise related 1Z0-811 questions on the same topic to reinforce the concept.

Related practice questions

Related 1Z0-811 practice-question pages

Use these pages to review the topic behind this question. This is how one missed question becomes focused revision.

Practice this exam

Start a free 1Z0-811 practice session

Short sessions build daily habit. Longer sessions build exam-day stamina. Try a timed session to simulate real conditions.

FAQ

Questions learners often ask

What does this 1Z0-811 question test?

Primitives, Strings and Operators — This question tests Primitives, Strings and Operators — String comparison.

What is the correct answer to this question?

The correct answer is: if (enteredPassword.compareTo(storedHash) == 0) — Both options A and B correctly compare the content of String objects. Option A uses compareTo(), which returns 0 when the strings are equal, and is a valid equality check. Option B uses equals(), which is the standard and recommended approach for comparing string content. Option C uses ==, which compares object references, not content, and is incorrect. Option D uses hashCode(), which may produce collisions and does not guarantee equality; it should not be used for equality checks.

What should I do if I get this 1Z0-811 question wrong?

Review string comparison, then practise related 1Z0-811 questions on the same topic to reinforce the concept.

What is the key concept behind this question?

String comparison

About these practice questions

Courseiva creates original exam-style practice questions with explanations and wrong-answer analysis. It does not publish real exam questions, exam dumps, or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

Same concept, more angles

2 more ways this is tested on 1Z0-811

These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.

Variation 1. Which operator is used to compare two strings for value equality in Java?

medium
  • A.compareTo()
  • B.=
  • C.==
  • D.equals()

Why D: Option D is correct because the equals() method in Java's String class compares the actual character sequences of two strings for value equality. Unlike the == operator, which checks reference equality (whether two references point to the same object in memory), equals() performs a lexicographic comparison of the string contents, returning true if and only if both strings have the same length and the same characters in the same order.

Variation 2. Which operator is used to compare two values for equality in Java?

easy
  • A.equals()
  • B.==
  • C.!=
  • D.=

Why B: Option B is correct because the == operator in Java is used to compare two primitive values for equality, returning true if they are equal. For reference types, == compares object references (memory addresses), not the actual content. This is a fundamental operator defined in the Java Language Specification (JLS §15.21) for equality testing.

Keep practising

More 1Z0-811 practice questions

Last reviewed: Jun 23, 2026

Question Discussion

Share a tip, memory trick, or ask about the reasoning behind this question. Do not post real exam questions, leaked content, braindumps, or copyrighted exam material. Comments are moderated and may be removed without notice.

Loading comments…

Sign in to join the discussion.

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.