Courseiva
Primitives, Strings and OperatorshardMultiple ChoiceObjective-mapped

1Z0-811 Primitives, Strings and Operators Practice Question

A developer is working on a Java application that processes user input. The application reads a string from the console and needs to compare it with a predefined constant string "ADMIN". The developer writes the following code: if (input == "ADMIN") { grantAccess(); }. During testing, the condition sometimes fails even when the user enters ADMIN. The input string is obtained via Scanner.nextLine(). Which is the most likely cause and best fix?

⚠ Common exam trap

Oracle often tests the distinction between reference equality (`==`) and value equality (`equals()`) for strings, exploiting the common misconception that `==` compares string content because it works for primitive types.

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

Use input.equals("ADMIN") instead of ==.

`==` compares object references, not string content. `Scanner.nextLine()` returns a new `String` object, so `input == "ADMIN"` compares references, which are different even if the content matches. Using `input.equals("ADMIN")` compares the actual character sequence, which is the correct way to test string equality in Java.

Answer analysis

Option-by-option breakdown

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

  • Use input.equals("ADMIN") instead of ==.

    Why this is correct

    Correct because equals compares values.

  • Use input.compareTo("ADMIN") == 0.

    Why it's wrong here

    `input.compareTo("ADMIN") == 0` checks if strings are lexicographically identical, returning zero for equality. While this *would* technically work for content comparison, it is not the idiomatic or most robust method for simple equality checks in Java; `equals()` is specifically designed for that. `compareTo` is primarily used for determining the natural ordering of strings, which is essential for sorting collections or when needing to know if one string comes before or after another.

  • Use input == "ADMIN" with intern() on input.

    Why it's wrong here

    Works but less reliable than equals.

  • Convert input to char array and compare.

    Why it's wrong here

    Unnecessary.

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.