Question 494 of 481
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
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 →
Last reviewed: Jun 30, 2026
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.
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.
Sign in to join the discussion.