MCQ Single Best Answer Moderate

Q
[Arrays, Strings]
Passwords are stored as two Strings:
  • StringA → Password entered by the user
  • StringB → Password stored in the system
If StringA must be checked with StringB for validity, which one of the following String functions should be used?

ID: #24940 Competency focused Practice Questions ISC Class XII Computer Science 8 views
Question Info
#24940Q ID
ModerateDifficulty
Competency focused Practice Questions ISC Class XII Computer ScienceTopic

Choose the Best Option

Click any option to instantly check if you're correct.

  • A charAt()
  • B equals()
  • C equalsIgnoreCase()
  • D startsWith()
Correct Answer: Option B

Explanation

[Arrays, Strings]
Passwords are stored as two Strings:
  • StringA → Password entered by the user
  • StringB → Password stored in the system
If StringA must be checked with StringB for validity, which one of the following String functions should be used?
(a) charAt()
(b) equals()
(c) equalsIgnoreCase()
(d) startsWith()
Correct Answer: (b) equals()

Explanation:

In Java, passwords are usually stored as Strings. To verify a password, the system must compare:

  • User entered password → StringA
  • Stored password → StringB

Password comparison must be:

  • Exact
  • Case-sensitive
  • Character-by-character identical

Why equals() is Correct?

The equals() method compares the complete contents of two strings exactly.

StringA.equals(StringB)

It returns:

  • true → if both strings are exactly same
  • false → if there is any difference

Example:

StringA = "Admin123"
StringB = "Admin123"

Result = true
StringA = "admin123"
StringB = "Admin123"

Result = false

Why Other Options are Wrong?

Method Reason
charAt() Returns only a single character at a specific position. It cannot compare complete passwords.
equalsIgnoreCase() Ignores uppercase and lowercase differences. Passwords should normally be case-sensitive for security.
startsWith() Checks only whether a string starts with another string. It does not compare the complete password.

Conclusion:

Since password validation requires exact matching, the correct method is:

equals()

Therefore, the correct answer is: (b) equals()

Share This Question

Challenge a friend or share with your study group.